Complex email automation (MailRules, Save Attachment to Downloads, Hazel, Omnifocus, Airmail)

Hey Automators :slight_smile:

I’m so happy, that Automators.fm was born - Great Podcast and so many great ideas to go further in my own Level of Automation! Thanks a lot to Rosemary and David! You’re doing a great work!!!

I use MailRules on a separate MacMini 24/7…like Rosemary…and i love it.
(I’m also using KeyboardMaestro, Hazel, Alfred, Airmail, Omnifocus…)

One Rule i cannot find out how to set up…because 2 Steps are missing:

I get some Stock-Reports or Newsletters with PDF-Attachments regulary. Each time i get these i have to do these steps manually - and i want to automate this. Some of the steps work OK for me (that’s why there is an “OK” at the Beginning of these Steps. The Steps i couldn’t find a solution for get a “??” in Front.

OK - My MacMiniServer (Apple MailRules) should recognize these mails.
?? - Then it should save the pdf attachment to the Downloads-Folder
OK - Hazel renames the files an moves them to another folder (on Dropbox)
OK - MailRules should mark the email as “read” and “archive” it
?? - Mail should make a new Omnifocus-Task with
subject (but adding a special Suffix or Prefix to this Subjectname based on the Date and the Name of the stock-Report, a special Project, a context/Tag with a link to the email to “AirMailClient” (because i use Airmail on iOS and Mac for “consuming and reading emails”.

I suspect, that the step “Saving Attachment” will only work with AppleScript…
I found many scripts which seem to do this job - but no one did work for me…and i’m a neebie to applescript…i’m sorry!
I also had the idea to solve this with “automator” - but i also didn’t get this running
I’m stuck in this problem…Would you pleas help me?

For the second problem (Omnifocus Task) i suspect this could have to do something with “url-Codes” - but i’m also a newbie there…and for making a link to the mail in Airmail i also have no idea how to solve this. Because Airmail cannot “read” (as far as i know) links to AppleMail emails…and vice versa)…

Another Way would be: Put a link in the Omnifocus Task to the saved PDF-File on Dropbox. This would have the advantage, that i could mark or comment things within the PDF’s.
But it should then be possible to open these PDF over the link on the Mac AND iOS…

If you have a completely other idea /workaround for this workflow…great! Please tell me :slight_smile:

Thanks for your help!

Thomas

2 Likes

For the Step with the omnifocus task I setup rules in gmail to forward the messages to my mail drop address. This way it happens server side and I don’t have to deal with it. Just my $.02 - Chuck

1 Like

I have IFTTT save all pdfs to my folder that hazel then processes.

I use AppleScript to put the pdf into OF as notes. The pdf file name is the OF task title. Used macsparky script as basis for this.

2 Likes

I suspect this works only with gmail adresses? I have an “icloud”-adress…but this reminded me to look for a solution with the “omnifocus-email-adress”…i’tm trying it out and will be back when i’ve got a solution…or need help ;-). Thank you!

I read two times, that IFTTT only has the full functionality with gmail-Adresses…Does this work for you with an icloud-adress?

With the applescript i’ll start again with David’s script to adjust it to my workflow…if i get “stuck” (or have a solution) i will post it. Thank you!

Yes, make an Airmaiy rule to forward email with PDFs to your gmail account to use IFTTT. ( I’ll post my modified macSparky script if you need it when I am back at my Mac next week. )

1 Like

OK - i tried IFTTT with an applet for saving to googleDrive and to dropbox. Both of them worked after some “trials” at least once…but not the second time.

I sent multiple emails with attachments in the same way from other accounts (in this case icloud adresses/aliases and an outlook-adress) to my gmail-account…now it’s 4 hours since i sent them…but they don’t arrive on my Dropbox-Folder. Might be, that i’m doin’ something wrong or should be more patient at the beginnig of an applet…but that’s no fun until now :frowning: . But if it should work many people write, that the rules take about 15 minutes to run usually … for monitoring thousands or millions of user-accounts this might be quite fast…but i think my macMini would be faster :wink:

Then i searched again for an applescript to save the attachments within a mail rule. This one seems to be perfect:

using terms from application "Mail"

on perform mail action with messages theMessages
	set attachmentsFolder to "Macintosh HD:Users:<<myusername>>:Downloads:" as rich text
-- please change <<myusername>> to your username (without the << and >>)
	
	tell application "Mail"
		set selectedMessages to theMessages
		try
			repeat with theMessage in selectedMessages
				
				repeat with theAttachment in theMessage's mail attachments
					set originalName to name of theAttachment
					set savePath to attachmentsFolder & originalName
					
					display dialog "MessageBox before saving"
					save theAttachment in filesavePath
					display dialog "MessageBox before saving"
				end repeat
			end repeat
		end try
		
	end tell
	
end perform mail action with messages
end using terms from

After seting up a Mail-Rule…:

the Script gives no Error-Message (Compiling works well).
The first MessageBox(DisplayDialog) appears…but the second doesn’t appear.
After that the email is marked grey (as espected)…and nothing happens.
The File isn’t saved in the Downloads-Folder…and nowhere else on my harddisk…i searched for it with emails that have specific numbers in it over the whole system.

Where’s the Bug? How could i get this working?

I would be really very happy to solve this…!

I’d guess it is a mismatched use of savePath and filesavePath :wink:, but if not…

Try putting the filename of the attachment (originalName) and the actual file path being used in the next step into the first dialog or at least logging them before you action them. Find out what that subsequent line is actually trying to do.

Always take a look at what you are passing in exactly when things start going wrong and backtrack from there.

1 Like

This definitely works with iCloud. I do this to make sure I check my App Store receipts.

Thanks a lot - you’r re right: it was a mismatch of SavePath and filesavePath (cannot understand, why i didn’t see that…!)

Since then the script worked once…but then never again…i’m using now script debugger (because i want to do more with AppleScript in the Future) and had to do some scripting within Applescript, that debugging through Apple Mail Rules still works…but although that i didn’t get it to work - well i won’t give up - but now’ i’m going on “holiday” :slight_smile: --> I’ll be back for this topic when i learned more of AppleScript-Basics…

1 Like

I found this post and wanted to share the working script as the one shown doesn’t work as mentioned.

using terms from application "Mail"
	on perform mail action with messages theMessages
		set attachmentsFolder to "Macintosh HD:Users:<<myusername>>:Downloads:" as rich text
		-- please change <<myusername>> to your username (without the << and >>)
		tell application "Mail"
			set selectedMessages to theMessages
			try
				repeat with theMessage in selectedMessages
					repeat with theAttachment in theMessage's mail attachments
						set originalName to name of theAttachment
						set savePath to attachmentsFolder & originalName
						save theAttachment in savePath
					end repeat
				end repeat
			end try
		end tell
	end perform mail action with messages
end using terms from