Save attachments from Mail

Hello.

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

In Apple Mail.app 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

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:

Users/Shared/Mailfiles

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

/ Fredrik

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 mail.app.

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

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

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.

Tack!

/ Fredrik

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?
1.
Macintosh HD/Users/Fredrik/Hotfolder
2.
External HD/Automator/Dropfolder
3.
(Mounted afp-volume 192.192.192.192 user:myself passw:pass123)
Server/Examples/Infolder

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