Automatically save every attachment from emails received in Apple Mail to Dropbox using Applescript.
Until recently, I used an IFTTT rule to save all attachments from emails received by my Gmail account to Dropbox. Unfortunately, Google changed the Gmail API, so all Gmail triggers were removed from IFTTT. This meant that my rule to save attachments stopped working.
I briefly considered changing email providers, but decided instead to see if I
could automate the saving of email attachments myself. After some Googling,
I found that in 2013 someone had managed to create an Apple Mail rule which saved all email attachments.
Of course, the Applescript attached in that post didn’t work right out
of the box on
macOS 10.14.5, so I needed to make a few edits (my version is linked
at the bottom of this post). Using a local
Mail.app rule instead of IFTTT has
the disadvantage that this automation only works when my Mac is turned on, but
it’s better than the alternative of having no automation at all.
Apart from setting
~/Dropbox/mail as the destination folder, there’s some semantic
differences as to how to access the Mail messages and the sender of each message.
As well, use of shell commands to create directories has been removed in favour of a
pure Applescript implementation which uses
tell application "Finder".
For each email that comes in, if the email has at least one attachment, the script
will create the directory
~/Dropbox/mail/$SENDER/ if it doesn’t exist already.
$SENDER is set to be the exact address of the sender. The script
then saves every attachment to
In order to get this script to run on every email, we need to create a rule in Mail.app:
I don’t really want to save attachments from every email, so I use a whitelist, but this rule can be easily tweaked to run on every single email you receive.
In order to run Applescripts in
Mail.app rules, we need to save the Applescript
in the application scripts folder. I use a hardlink from my scripts repo so I
don’t have to worry about remembering to keep the one hidden in ~/Library updated:
$ ln attachment-saver.applescript ~/Library/Application Scripts/com.apple.mail/attachments
Here’s the script which works on
macOS 10.14.5, if you’re interested:
Cataloging my email like this is good for injesting into folders that have Hazel rules run on them, but that’s a story for another time.