How to use a regular expression to trash files in the Downloads folder?

I’m using a macro with an AppleScript to save the attachments in a message in Apple Mail (Version 16.0 (3826.700.81.1.8)). Clients like to embed their company logo and related images in their messages. I’d like to remove these images from the Downloads folder.

How can I trash files like ~/Downloads/image001.png or ~/Downloads/image002.jpg?

The names of these files vary.

Edit: I’ve only just seen that you link to a thread about Apple Mail, so apologies if this reply is not relevant within the wider context!

So you want to trash all png and jpg files in the Downloads folder? If so…

Trash jpg and png files..kmmacros (3.1 KB)

Note the | character between jpg and png. This means “or”. Therefore, if you wanted to trash tiff files too, you could add |tiff.

1 Like

Are you actually using ~/Downloads, or a subfolder in ~/Downloads as Chris suggested? A subfolder makes things quicker since the macro won't have to pointless iterate over every "normal" download to see if it's an image.

If you are using ~/Downloads you could get cute and only trash images from Mail -- those will include kMDItemWhereFroms email metadata, so you should be able to leverage mdfind. But using a subfolder to separate them is much easier!

Keeping downloaded image attachments you want will be trickier. You might be able to play off the unwanted images being (generally) unnamed so being downloaded as "imagennn" as in your examples -- but I don't don't know how reliable that is.

1 Like

Thank you @kevinb! With your help I was able to modify Chris’s original macro to this:

Save Attachments of Selected Message v1.10 - Modified.kmmacros (14.5 KB)

2 Likes