how to save and print email attachments
Posted by philastokes
I was recently asked for a script that would automatically save and print all the pdfs from one particular client’s emails. Since this is quite a common use case (think invoices from a particular supplier, for example) and involves a fair bit of complexity, I thought I’d share the answer for any others out there that have the same need.
We’re going to need to do three things; install a script, set up a mail rule, and set up folder actions. Here we go!
Part 1: Install Script
Copy and paste the script you see below from here:
Save this script with a name like ‘CopyAttachments’ in
~/Library/Application Scripts/com.apple.mail
(note: ~/Library means your user library. You can find it by triple-clicking the path above, then control-click on the highlighted text and choose Services > Reveal in Finder)
Part 2: Set up Mail Rule
Open Mail.app. Click on your Inbox in the sidebar. Click ‘Mail > Preferences… > Rules > Add Rule’
Under ‘Description’ give the rule a name (e.g., ‘Copy attachments’)
Set ‘If ANY of the following conditions are met’:
to
‘From contains’
and the email address of the person whose attachments you want to target.
(note: You can add more than one person’s email if you wish, but you do so by hitting the ‘+’ key and adding a new condition, not by adding more than one address in the text field. Each text field must contain only one condition, i.e., email address or keyword).
Next, set ‘Perform the following actions:’
to
‘Run AppleScript’
Click the ‘No Script Selected’ button and choose ‘CopyAttachments’
Click ‘OK” and in the following dialog click ‘Apply’.
3. Create & Set up a Folder Action
Open Automator.app. From the open panel choose ‘Folder Action’.
In the large, empty panel at the top you’ll see
‘Folder Action receives files and folders added to’ Choose folder
Click the Choose folder menu, choose Other. Select the folder you want the attachments to be saved in.
In the filter/search bar on the left of the Automator window, type ‘print images’. Drag the ‘Print Images’ selection from the results list into the middle of the empty workflow and release.
You can set some options here if you like (‘scale to fit’ might be useful).
You can choose either ‘Default Printer’ or click to select your actual printer. If your actual printer is the default, it won’t make any difference.
Press ‘command-S’ on your keyboard to save. Supply a name (e.g. Print PDFs) and hit ‘OK’. You do not choose a save location.
Quit Automator.
Open a Finder window and navigate to the folder where the attachments are going to be saved.
Hold down the ‘control’ key on your keyboard and click the attachments folder. From the contextual menu, go to Services > Folder Actions Setup… and click to open the dialog box.
Navigate down the list of scripts till you see the name of the Automator action you saved above and select it. Click ‘Attach’.
In the parent dialog box, check the box at the top that says ‘Enable Folder Actions’ and ensure that in the list on the left the attachments folder is listed and checked. Check that on the right, the ‘Print PDFs.workflow’ is checked.
If all is in order, close the dialog box. The procedure is complete and the workflow is installed.
Testing
It’d be wise to test the script as soon as possible. If it fails to work, double check that you’ve entered the correct path in the AppleScript as that’s the most likely point of failure. Let’s suppose your hard disk is called ‘Macintosh HD’, your user name is “Mack” and the folder you want to save the attachments in is “Invoices From Acme”, then the set attachmentsFolder line should look like this:
set attachmentsFolder to "Volumes:Macintosh HD:Users:Mack:Invoices From Acme:" as rich text
You must ensure you’ve already created the folder ‘Invoices From Acme’ before running the script. Also, be sure you don’t forget the trailing colon at the end of the path and that you have a matching pair of opening and closing quotes around the path name.
Any problems, drop me a comment below and I’ll try to help you out. Good luck and enjoy! 🙂
About philastokes
Independent Software Developer, Technical Writer and Researcher at SentinelOne. Explaining the unexplainable with images, video and text. Scripting anything imaginable in AppleScript, Bash, Python and Swift.Posted on May 25, 2014, in AppleScript, Automator, Mail and tagged applescript, attachments, folder actions, Mail, print, using terms from. Bookmark the permalink. 8 Comments.
Hi Phil,
I set it up and it works great, except it only prints the most recent one. Is there a way to make it print any older ones as well? This may be because all the attachments have identical names, but I’m not sure.
Any help would be appreciated,
thanks,
Eli
Hi Phil,
This is working beautifully for me with a bunch of .pch and .plist file attachments, but when I try the exact same setup for a bunch of .xml attachments (about 384 KB each), nothing happens. There’s no error; it just doesn’t save the files anywhere. Any suggestions? I’m using El Capitan.
Thanks for the share. The script is running fine but my files end up one folder up in the directory. For example, if I would set “Volumes:Macintosh HD:Users:Mack:Invoices From Acme:” all my files are stored in “Volumes:Macintosh HD:Users:Mack:” at this location. How can I solve this?
Best,
Robin
Thank you Phil,
Now I get the message:
„Mail“ hat einen Fehler erhalten: „selection of message viewer 1“ kann nicht gelesen werden.
(“Mail” has received an error: “selection of message viewer 1” cannot be read)
Now I have installed IOS 10.10.2
Best
Wolfgang
Hi Phil,
thank you for your answer.
I’ve tried but I get now the message: “selection of message viewer 1“ cannot be read.
Best
Wolfgang
Hello,
I have installed Yosemite yesterday.
Now I get the message:
” error in the apple-event-routine”
The error is in this line:
“repeat with theMessage in selectedMessages” (I have tested it step by step)
Can you help?
thank you
Wolfgang
Hi Wolfgang,
Yeah, it appears to be a bug in Yosemite. There’s a bit of chatter on the AppleScript mailing lists about this. I’ll post an update when I know more.
Wolfgang, I don’t have this workflow set up on my machine at the moment, but one suggestions I’ve seen that reportedly works on Yosemite is to add the following line immediately after the tell Mail statement:
tell application "Mail"
tell message viewer 1 #added
and then to add another ‘end tell’ after the ‘end try’ statement:
end try
end tell #added
I’d be interested to know if that works for you.
Best
Phil