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:
(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’:
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:’
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.
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.
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! 🙂