Applescript to download attachments in Airmail

Good morning all. I was hoping for a little help troubleshooting my script to save downloads from Airmail. I’m running Airmail 3 on a Mac Mini server at home to help automate things while I’m on the road. I’d like it to automatically grab certain attachments and toss them into a folder for Hazel to do the rest. I’m trying to move away from using my inbox as a todo list. So ultimately, I’ll want the Hazel action to put a task in Omnifocus where it belongs.

Here’s the script.

property DOWNLOADS : "~Library:Mobile Documents:iCloud~it~bloop~aimail2:Documents"

on processMessage(theMessage)
	try
		tell application "Airmail 3"
			repeat with anAttach in mail attachments of theMessage
				set aFilename to filename of anAttach
				set aFile to quoted form of aFilename
				set fldr to quoted form of DOWNLOADS		
				do shell script "cp " & (aFile as text) & " " & (fldr as text) & ""
			end repeat
		end tell
	end try
end processMessage

Questions (this is my first real applescript, and some was harvested from various web sources.)

How do I save this (Compiled or Text) for use in an Airmail Rule?
Do I need to reference the object mail attachments, or just attachments? For my education, how would I normally decide that?

The rule doesn’t work. Nothing ever appears in my iCloud folder. Any help would be appreciated. Perhaps I’m fundamentally making an incorrect assumption.

Also - I’ve seen many folks who use the Apple Mail app on the home server for this task. Are there distinct advantages to that route?

Thanks again.

Unfortunately I can’t help with the AppleScript, but I have two other possible solutions for you:

  1. OmniFocus Mail Drop which will let you forward emails right to OmniFocus

  2. SendToDropbox which will let you send emails to a secret email address and have the attachments saved to a specific folder in your Dropbox (~/Dropbox/Apps/Attachments/ I think) which you could then watch with Hazel.

The benefit of these two is that you will select which messages to send to them, so it won’t end up cluttering up your OmniFocus with every attachment that you ever receive (some of which include things like graphics files in people’s .sig)

1 Like

I tried this a few years ago without success. My solution was to forward all email attachments to my gmail account and then use an IFTTT recipe to save the attachments to my downloads.

Another option is SaneBox, who I have used for years and definitely recommend.

They have a feature called “SaneAttachments” which might come in very handy:

Thanks for all the ideas. The primary problem is that I get a LOT of attachments, 90% of them I don’t want cluttering up my system. Our internal systems produce certain financial reports and other items that have a very standard name, and come in a canned email. I was hoping to catch that email when it came in and download the attachment to the folder of my favorite reader on my iPad Pro, then create an Omnifocus task to review the financials. Ideally, Hazel would also remove outdated versions that are not my responsibility to archive.

I am a Sanebox subscriber at this point, but was looking to move away from the service. I almost feel like the time to manually adjust rules helps me stay in touch with what is happening in the background. I’ll look at the options surrounding the attachments service. Perhaps it would fulfill a need.

Thanks again. Any help with the Applescript would still be appreciated.

For what its worth - I thought I’d post a solution I found:

Note the script code posted on May 22.

The script appears to download all attachments consistently to Downloads folder. According to some of the comments online, sandboxing may interfere with trying to use a different folder without user input. Also, it seems as though the author had to make several tweaks to the code based on the OS and the Airmail version being used. I think it is likely that the code will break eventually with updates.

Again - I hope this helps someone else as well.

2 Likes

Here is my solution that I use for downloading attachments to DevonThink. You could modify it to download the attachments to a Finder folder instead. Personally I have not found that the rules in Airmail work. I trigger the script manually.

http://eggleton.co.nz/geekposts/2017-02-12-Sending-email-from-AirMail-to-DevonThink/