Save attachments from Mail


I have searched the web for a long time. I look for a (what I can see) simple script.

In Apple you can set up a rule that will trigger a script. I would like this script to save the files attached in the mail (that is current in my rule) into a specified folder on my Macintosh (in Finder).

This looks like a very simple script, but I can not get it to work, and I can not find any example. Please help! :slight_smile:

For information, I will then let this folder be a HotFolder that will take the file saved into it, and process it or print it or…

/ Fredrik

Thanks Jaques, but I have already tryed that.
I spent a lot of time trying to find my simple answer inside the scripts you informed me about. I really thought they could give me the hint, but sorry to say they did not. I’m not sure why they don’t work for me :frowning:
I think that the examples are based on some special demands, and I just want the simplest solution. Even if I take the scripts and try to clean them into simplicity I get no luck.
Perhaps i should have some real knowledge about advanced scripting :-).
When i try the examples in their current state or in my cleaned tryouts, the only thing that happen is that i see a quick “blink” on my screen, but nothing really happens.

Can someone please help some more me with my issue?

/ Fredrik


No problem. Do you have a particular folder in mind? If you want all attachments of emails that trigger the script to be placed in one folder, that would be easy, and I would be happy to modify my script to make it work for your situation. All I need is the name and location of the folder, and we are set to go.

Thanks Craig!

Yes. I would like every file to end up in the same folder. I guess (and hope) that if a file with the same name already is in the folder, it will be overwritten?

The folder could for example be:


I guess I can find out the folderpath and modify it later to fit my needs :slight_smile:

/ Fredrik


Try this script:

--Saves any SINGLE attachment to the 'Mailfiles' folder of the Shared folder

using terms from application "Mail"
	on perform mail action with messages The_Messages
		set Save_folder to SaveFolder()
		tell application "Mail" to repeat with This_Message in The_Messages
			set save_file to (Save_folder & (the name of first mail attachment of This_Message))
			save first mail attachment of This_Message in save_file
		end repeat
	end perform mail action with messages
end using terms from
on SaveFolder()
	set a to ((path to shared documents as Unicode text) & "Mailfiles:")
end SaveFolder

It works with a single attachment only, and yes, it SHOULD overwrite any other attachment of the same name. If not, please come back with a report. If there are multiple attachments, it will certainly require more work, but I have no time to pursue that today.

From the bottom of my heart - Thanks!

I have som problems with my mail-account for the moment, but I will try this script as soon as it is up and running again.
I tryed to mark a mail already in my inbox containing a single attach, and then choosed the script from the scriptmenu, but nothing happened. I guess it’s because I started it manually instead of by a rule in

I will come back and tell you if it did work when running as intended.

If you have a chance, I would be more than happy to try a script from you with support for multiple attachments in the same mail.

For now - Thanks.
Greetings from a snow-white-cold Sweden.

/ Fredrik


Du är Svensk! Mycket bra. Kommer ihåg at du måste ha en folder med nam Mailfiles redan i din Shared folder, annars scripten ska inte funkar. Scripten ska inte göra folder, du måste göra det först.

Var bå dig? Jag bådde i Lund, Hisingen (Göteborg), Halmstad, och Landskrona från Jan 1981 till Juni 1982.

Förlåt min dålig Svenska, det var länge sedan.

Vi ses,

Way to go Craig!!!

Din svenska är helt okej. Det känns som att du skriver svenska bättre än jag skriver engelska.
I live in Jönköping by the way :-))

I have now tryed your script, and it works very well. I thank you for that!
If you by any chance can make a script based on exactly the same rules as before, but also that has the possibility to manage multiple files attached in the same mail, I would be dancing in the snow :slight_smile:

And now some swedish:

Vad gjorde du i Sverige? Var du här och jobbade, eller var du student?
Jag har aldrig besökt den Amerikanska västkusten, sÃ¥ jag har aldrig varit där du bor. Däremot tog jag ett besök med Google Earth för att se var du fanns. Om du har Google Earth kan du söka pÃ¥ “Jonkoping, Sweden” sÃ¥ fÃ¥r du se mitt hem.

/ Fredrik


This will save all attachments to the messages that trigger the script in your ‘Mailfiles’ folder located in the Shared folder:

--Saves all attachments to the 'Mailfiles' folder of the Shared folder

using terms from application "Mail"
	on perform mail action with messages The_Messages
		set Save_folder to SaveFolder()
		tell application "Mail" to repeat with This_Message in The_Messages
			repeat with ma from 1 to (count every mail attachment of This_Message)
				set save_file to (Save_folder & (the name of mail attachment ma of This_Message))
				save mail attachment ma of This_Message in save_file
			end repeat
		end repeat
	end perform mail action with messages
end using terms from
on SaveFolder()
	set a to ((path to shared documents as Unicode text) & "Mailfiles:")
end SaveFolder

Jag hoppas at du tycker om det. Tack för dina vänliga ord om min Svenska. Fructe inte, du skriver bra Engelska.
Jag var i Sverige som missionär för kyrkan (Jesu Krist Kyrka av Sista Dagars Heliga) och besökt bara västkusten! Jag vill åka till Uppsala och besöka veterinärskola (jar är veterinär och hade en Svensk lärare). Tack för info på Google Earth, jag ska göra det.

Thanks Craig.

I will try your new script 1:st thing tomorrow.
Believe me when I’m saying that you have helped me a lot with these two scripts.
I will absolutely read into these scripts and try to figure out how and why…

Hoppas du får chans att besöka Uppsala någon gång. Välkommen tillbaka till Sverige.


/ Fredrik


I do not believe that the files of the same name are being overwritten with new files when they arrive. Is there any way you can test that?

Thanks for the advice Craig :slight_smile:
Yes, I will try that and see what will happen.
I’ll get back…

/ Fredrik

You were right Craig.

If a file with the same name as a file already in the save folder were processed from a new mail, nothing happens.
This script does not overwrite files with the same name. In fact if multiple files are sent in, a name-conflict will stop all files in that mail from being saved.

In my case this won´t be a big problem. I will make sure that the folder in which the fil/files are saved, automatically is empty becase a file in the folder will be processed and moved, so this bug is not a problem.

A thing that would be useful though, is an explanation of the searchpath to the folder in whitch the files being saved.
In this case the folder is: Users/Shared/Mailfiles, and in the script this path is written as:
set a to ((path to shared documents as Unicode text) & “Mailfiles:”)

How for example would the scripted searchpath to the following three destinations look like?
Macintosh HD/Users/Fredrik/Hotfolder
External HD/Automator/Dropfolder
(Mounted afp-volume user:myself passw:pass123)

And now some Swedish :-))

Har du tittat på Jönköping i Google Earth?
Jag står här och vinkar så att du kan se mig :wink:

/ Fredrik

This method:

set a to ((path to shared documents as Unicode text) & "Mailfiles:")

uses a nice system on the Mac to automatically get the path to the Shared folder, regardless of the drive name or the user name. It makes it very easy to write code that will work properly on any machine. There are only a limited number of [path to] options, like path to documents folder, path to desktop, path to music folder, etc. You will find a good list in the Applescript dictionary for Standard Additions, under the heading File Commands.

If you have other places you want to save attachments, like external drives or folders not on the [path to] list, you will need to set the path variable yourself, like this:

set a to "Macintosh HD:Users:Fredrik:Hotfolder:"
set a to "External HD:Automator:Dropfolder:"

When you do it this way, you MUST have the pathway written correctly, so it would be a good idea to test it out first. Also, be sure to put the colon [:] at the end, so that when you add on the filename, it will be correctly written to the desired folder.

If you are having troubles, be sure to come back here for assistance. There are many scripters that check in to help people learn, and that is why they come, so do not be shy about asking, just be polite, and thank those that assist you.

Jag kan se dig på stranden, är du inte kalt? Du behöver en jacka, kanske två. Här är mitt hus-1306 163rd st east, tacoma, wa, usa 98445. Vi bår nästa skolan, mina barn har en liten swimmingpool i träder. Huset är i skuggor. Finns det svenska ord för download?

Once again, Thanks.

Now I see it very clear. Perhaps i´m really beginning to get an understanding to this logic :slight_smile:

I did see your house. It was lockated on a rather short street. I did see two houses with small pools in the front, and I thought that one has to be yours :-))

Download in swedish would be: Ladda ner (Down=Ner Load=Ladda)

If you look at my hometown in Google Earth (search for: jonkoping,sweden), and then look at the absolute last buildings in the western edge of town, there is a round street called “Runnavägen”. I live in the outer side of that cirkle in the house that is located at 5 o clock just above a bicycle-path. Our stret is (as I said) as far as you can go to the west, but there are of course single houses after ours, but the city “block” ends here.

Thanks again.

Best regards, Fredrik