Mail attachments from Mail automate download to folder

I’m looking for working AppleScript which will download from incoming e-mails all attachments to exact folders.
Anybody can share working AppleScript for that job ?

HI, @Krzysztof,

Apple Mail stores all the mail attachments at ~/Library/Containers/ Downloads/ So a simple shell script, executed via terminal, could grab these and make copies to a folder of your choice.

Something like this might be a good starting point:

cd ~/Library/Containers/ Downloads/
cp */* ~/Documents/

This would go to the mail attachment folder and then copy all attachments in each subfolder ("*/*") to a folder of your choice in your Documents folder.


Thanks for this work around !

Please check the path again because I dont have Containers folder in Library.

Anyway please anybody can share AppleScript to be connected to Mail App ?

You didn’t ask, but I like SaneBox for this feature.

What version of macOS are you on? I’m on High Sierra. When I googled it for older versions, I found the following possible locations:

  • Users/Username/Library/Mail Downloads
  • Users/Username/Library/Mail/Mail Downloads
  • ~/Library/Containers/ Downloads
  • Users/Username/Library/Containers/ Downloads

One of these should work.

If you locate the folder with your mail attachments I think you should be able to use Hazel to set up rules to move items to specified folders.

Thanks, I have also High Sierra …

I’m trying to find solution in Apple Script.
Think that I foud quite easy AppleScript but in my case is not working.

Code should be fine and Mail rule should be fine too.

Could anybody check if this is script if this is working with Mail?

using terms from application "Mail"
	on perform_mail_action with messagestheMessages
		set attachmentsFolder to "Volumes:Macintosh HD:Users:kp:Documents:Attachments" as rich text
		tell application "Mail"
			set selectedMessages to theMessages
				repeat with theMessage in selectedMessages
					set counter to 1 -- added
					repeat with theAttachment in theMessage's mail attachments
						set subjectText to (get theMessage's subject) as rich text
						set originalName to ((every word of subjectText) as rich text)
						set savePath to attachmentsFolder & originalName & (counter as rich text) --modified
						set counter to counter + 1 -- added
						save theAttachment in file savePath
					end repeat
				end repeat
			end try
		end tell
	end perform_mail_action
end using terms from

This is all great! What if you wanted to separate them by personal/work? Would there be a way with Hazel or something else that would pull these into a folder, then mark them based on the email address they came from?

Hazel will be my second tool to sort this out.
My current problem is that this should work, but is not working with Mail.
I don’t know why ? :frowning:
Could anybody test this ?

Do you have to use the script? Could you not just have Hazel move the files from the Library file?

I would like get this files from AppleScript instead Library search.
It should be working, but in my case is not.
Can anybody check this out ?

Is there a particular reason for wanting to use a more complex looking AppleScript solution over what appears to be a more simple file move?

  • Are you getting an error message?
  • Does it simply do nothing at all - presumably you do have a mail selection with attachments?
  • Have you tried stepping through the code - e.g. with a debugger (such as the one by Late Night Software, or using say display dialogs of key data items)? Did that show anything up?

Also, as an aside, is there a particular reason for using rich text rather than plain text everywhere in the script? Nnot something I’ve seen done before for what look like plain text file paths.

All attachements are located in FOLDERs like that “E2966A3B-DF9E-47A0-9352-87217643A361”.
How to search all of them and connect to messages (from) ?

It should work on each incomming email with attachements from selected recipients.
After incomming mail nothing happend. I’m getting nothing, no error message…

Thanks for this tool.
I tried check this script using this debuger, but again I’m getting nothing :frowning:

So when you stepped through with the debugger you could see it was definitely going through the selected messages and you could see those messages being processed in the script and the attahments on those messages being processed.

Specifically, you were able to see the counter variable incrementing, the subjectText variable being set/changing, the attachments originalName variable being set/changing, and the file savePath variable being set/changing. But when it gets to the line that saves, no attachment is saved to the file system.


This is working.

-- Tested on macOS 10.13.6

use AppleScript version "2.4"
use scripting additions

using terms from application "Mail"
	on perform mail action with messages messageList in mailboxes mbox for rule aRule
		--	Save on DesktopPath 	
		set desktopPath to (path to desktop) as string
		tell application "Mail"
			set selectedMessages to the selection
			repeat with aMessage in selectedMessages
				repeat with anAttachment in mail attachments of aMessage
					set attachmentName to name of anAttachment
					save anAttachment in file (desktopPath & attachmentName)
				end repeat
			end repeat
		end tell
	end perform mail action with messages
end using terms from

hi krzysztof

your script works like a charm, thank you, exactly what i needed.

one issue i have is that the rule i created with this does not apply automatically, rather i have to click “apply rule” from the dropdown menue.

did you encounter that issue?


Try adding the ‘every message’ condition and a condition that specifies the account you want the rule to apply to. I was having the same problem as you, but once I added these two conditions the rules started applying automatically for me. Make sure that the all condition is selected as well otherwise, this change will literally cause every message to be affected.

Anyone figure out a way for this to work without saving to desktop. I would rather it save to a particular folder on my hard drive.

You should just be able to set the desktopPath variable to whatever destination path you want. I’d also rename the variable to something more generic too. Maybe destinationPath for example.