Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

dangerfish

macrumors 6502a
Original poster
Aug 28, 2007
584
133
I am looking to set up an Applescript/Automator action that will automatically save any pdf email attachments to my desktop (or other folder). I believe the first step is to set up an email rule as shown below. But I don't know anything about Applescript. Is there a simple script that would run and save pdf email attachments to my desktop? This would be on 10.9.

TIA
 

Attachments

  • Screen Shot 2014-05-05 at 3.24.56 PM.png
    Screen Shot 2014-05-05 at 3.24.56 PM.png
    73.2 KB · Views: 367
Try this :

Code:
using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		set theOutputFolder to "LeopardFirewire:Users:test:Desktop:Stuff:"
		tell application "Mail"
			repeat with theMessage in theMessages
				if theMessage's mail attachments is not {} then
					repeat with theAttachment in theMessage's mail attachments
						set theFileName to theOutputFolder & theAttachment's name
						try
							save theAttachment in theFileName
						on error errnum
						end try
					end repeat
				end if
			end repeat
		end tell
	end perform mail action with messages
end using terms from

Note : You need to change theOutputFolder to your desktop or other folder. To get the right path to your desktop folder run this in another script editor window and paste your result into the script.
 

Attachments

  • Picture 2.png
    Picture 2.png
    46.5 KB · Views: 183
To dangerfish :

you will need to save the script into : ~/Library/Application scripts/com.apple.mail in order to have it show up in the drop down menu when you create the rule in Mail.
 
Thank you everyone! I have it working. I couldn't get Bruno's to work but Kryten's did.
Now for the bonus, is there a way to automatically print the pdf to my default printer once it is saved to the desktop?
 
I can't have this working, nothing happens. Could you please help ?

The Mail rule is : If Attachment = picture ----> Run Applescript "mail2"
Name of the Rule : "images" (=pictures).

The script "mail2" is saved in ~/Library/Application Scripts/com.apple.mail

Thanks in advance.

Code:
using terms from application "Mail"
	on perform mail action with messages theMessages for rule images
		set theOutputFolder to "MBPHD:Users:bruno:Documents:Attachments:"
		tell application "Mail"
			repeat with theMessage in theMessages
				if theMessage's mail attachments is not {} then
					repeat with theAttachment in theMessage's mail attachments
						set theFileName to theOutputFolder & theAttachment's name
						try
							save theAttachment in theFileName
						on error errnum
						end try
					end repeat
				end if
			end repeat
		end tell
	end perform mail action with messages
end using terms from
 
Try :

Code:
on perform mail action with messages theMessages for rule theRule

or without the for rule argument.

Code:
on perform mail action with messages theMessages
 
Thank you for your help.

Tried both and still no luck.

I am certainly missing something :(

MBPHD is the name of your hard disk? Try this with selecting a message in Mail with a picture attachment.

Code:
using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		set theOutputFolder to "MBPHD:Users:bruno:Documents:Attachments:"
		tell application "Mail"
			repeat with theMessage in theMessages
				if theMessage's mail attachments is not {} then
					repeat with theAttachment in theMessage's mail attachments
						set theFileName to theOutputFolder & theAttachment's name
						--try
						save theAttachment in theFileName
						--on error errnum
						--end try
					end repeat
				end if
			end repeat
		end tell
	end perform mail action with messages
end using terms from

(*
Next block is for running the script in Script Editor with some mail messages selected in the Mail application. If everything works remove the entire block and attach script to rule.
*)
-----------------ONLY NEEDED FOR TESTING FROM WITHIN SCRIPT EDITOR--------

tell application "Mail"
	set myMessages to selection
	tell me to perform mail action with messages myMessages
end tell

-----------------REMOVE WHEN TESTING IS DONE !!!!!!-----------------------
 
This one gives me an error :

"Applescript error : parameter "class pmar" missing for "event emalcpma" "

number -1701 partial result «handler «event emalcpma»» from «class pmar»

And yes, MBPHD is my boot disk.

EDIT : after a reboot, the scripts runs but fails to save the attachments.

I selected one mail in Mail, with 5 pictures attached, and ran the script from Applescript editor.

Result :

count every mail attachment of message id 27700 of mailbox "Valérie-Olivier"
--> 5
get name of item 1 of every mail attachment of message id 27700 of mailbox "Valérie-Olivier"
--> "IMG_6833.jpeg"
save item 1 of every mail attachment of message id 27700 of mailbox "Valérie-Olivier" in "MBPHD:Users:bruno:Documents:Attachments:IMG_6833.jpeg"
--> error number -10000
get name of item 2 of every mail attachment of message id 27700 of mailbox "Valérie-Olivier"
--> "IMG_6835.jpeg"
save item 2 of every mail attachment of message id 27700 of mailbox "Valérie-Olivier" in "MBPHD:Users:bruno:Documents:Attachments:IMG_6835.jpeg"
--> error number -10000
get name of item 3 of every mail attachment of message id 27700 of mailbox "Valérie-Olivier"
--> "IMG_6832.jpeg"
save item 3 of every mail attachment of message id 27700 of mailbox "Valérie-Olivier" in "MBPHD:Users:bruno:Documents:Attachments:IMG_6832.jpeg"
--> error number -10000
get name of item 4 of every mail attachment of message id 27700 of mailbox "Valérie-Olivier"
--> "IMG_6840.jpeg"
save item 4 of every mail attachment of message id 27700 of mailbox "Valérie-Olivier" in "MBPHD:Users:bruno:Documents:Attachments:IMG_6840.jpeg"
--> error number -10000
get name of item 5 of every mail attachment of message id 27700 of mailbox "Valérie-Olivier"
--> "IMG_6842.jpeg"
save item 5 of every mail attachment of message id 27700 of mailbox "Valérie-Olivier" in "MBPHD:Users:bruno:Documents:Attachments:IMG_6842.jpeg"
--> error number -10000
end tell
 
Last edited:
Sorry it doesn't work for you. I just ran a quick test and it still works for me however without the for rule argument.

Code:
on perform mail action with messages theMessages
 

Attachments

  • Picture 7.png
    Picture 7.png
    264.5 KB · Views: 227
Still don't work, even without the for rule argument.

Error 10000

count every mail attachment of message id 30757 of mailbox "INBOX" of account "bruno"
--> 1
get name of item 1 of every mail attachment of message id 30757 of mailbox "INBOX" of account "bruno"
--> "IMG_0071.jpeg"
save item 1 of every mail attachment of message id 30757 of mailbox "INBOX" of account "bruno" in "MBPHD:Users:bruno:Documents:Attachments:IMG_0071.jpeg"
--> error number -10000
end tell

@ kryten2 : which OS are you running, please ?
(I am running Mavericks).



EDIT : finally found the answer in an Apple Support Communities thread :

From Mountain Lion (Lion ?) :

"The script works for me. Just needs to be saved to correct spot in library and you can save your files ONLY to Downloads folder (whatever subfolder you want to create must be in Downloads) it will not save it into documents."

Changed the destination folder to Downloads and it worked instantly :D

Thank you for your help.
 
Last edited:
Test was done on Leopard. I'll test on Mavericks too and edit my post with the result.

Edit : On Mavericks you need to replace :

Code:
save theAttachment in theFileName

with

Code:
save theAttachment in file theFileName

Test was done on Mavericks saving to a folder on the Desktop and to a folder in the Documents folder. Both worked.
 

Attachments

  • Screen Shot 2014-05-06 at 23.39.55.png
    Screen Shot 2014-05-06 at 23.39.55.png
    229.4 KB · Views: 202
  • Screen Shot 2014-05-06 at 23.41.00.png
    Screen Shot 2014-05-06 at 23.41.00.png
    229.6 KB · Views: 178
Last edited:
Thanks Kryten,

I too was able to reliably save to the downloads folder but it was sporadic when trying to save to the desktop. Now it saves to the desktop just fine.

Is there something we could add to the script to tell it to print the file to the default printer without the dialog box?

Thanks!
 
I figured out the print syntax myself. Now when an email with a pdf attachment arrives, it is saved to my desktop and printed to my printer. I added the following text to the script:

tell application "Officejet Pro L7600"
print file theFileName
end tell

Thanks for all the help
 
I figured out the print syntax myself. Now when an email with a pdf attachment arrives, it is saved to my desktop and printed to my printer. I added the following text to the script:

tell application "Officejet Pro L7600"
print file theFileName
end tell

Thanks for all the help

You're welcome. Nice of you to figure this one out yourself. I don't have a printer but did a bit of searching and the code you posted came up a few times. Thank you for sharing it back.
 
Edit : On Mavericks you need to replace :
save theAttachment in theFileName

with

save theAttachment in file theFileName
Thank you : this makes the script work indeed, I can save to whatever folder I want, MANUALLY (from Applescript Editor), when using :

tell application "Mail"
set myMessages to selection
tell me to perform mail action with messages myMessages
end tell

However, the Mail rule does not work.

Here is my Mail rule :

"Attachment" is "Image"
"Run Applescript" : "PJ images"



And here is the script :

Code:
using terms from application "Mail"
	on perform mail action with messages theMessages -- for rule theRule
		set theOutputFolder to "MBPHD:Users:bruno:Desktop:"
		tell application "Mail"
			repeat with theMessage in theMessages
				if theMessage's mail attachments is not {} then
					repeat with theAttachment in theMessage's mail attachments
						set theFileName to theOutputFolder & theAttachment's name
						try
							save theAttachment in file theFileName
						on error errnum
						end try
					end repeat
				end if
			end repeat
		end tell
	end perform mail action with messages
end using terms from


EDIT : pfuuuuuuu..... finally figured out what the issue is :

Drum rolls....

DOES NOT work with POP accounts

WORKS FINE with IMAP accounts !!

Thank you for your help.

Note : there is no need to use the disk name :
set theOutputFolder to "Users:bruno:Folder:" works as well.
 
Last edited:
Can someone help me out with a script that notifies me when a PayPal button has been added to a webpage? I have created a script that refreshes the webpage for me but the PayPal button is not at the top of the page it is in the middle, so I am unable to see if the PayPal button has been added. Thanks!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.