Help with mac mail automation

Hey guys, with the help of this site I’ve managed to decrypt attachments using Hazel script, it works very well, the thing is I have to download those PDF attachments from mail manually, I received these emails every month, I’ve already created a mac mail rule that it will copy messages from the sender to a specific folder I’ve created on mail, so can someone help me to create something like ’ when the email arrives it will automatically get downloaded ’ to a specific folder on my mac which is monitors by Hazel? Thank you very much in advance.

Have you read through the solutions in these discussion threads?

I believe SaneBox also supports automatic saving of attachments to folders in cloud services.

1 Like

Sorry, I did not, I will check it now, thank you for the link.

using terms from application "Mail"
	
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			
			repeat with oneMessage in theMessages
				
				set {mail attachment:theAttachments} to oneMessage
				set subj to subject of oneMessage
				
				tell me
					set subj to do shell script "echo " & subj & " | sed 's/:/–/g'"
				end tell
				
				repeat with oneAttachment in mail attachments of oneMessage
					
					save oneAttachment in ("Macintosh HD:Users:xxxxx:Downloads:") & subj & ".pdf"
					
				end repeat
				
			end repeat
			
		end tell
	end perform mail action with messages
	
end using terms from

This is working for me. You can just download to the download folder. This was restricted by Apple some time ago.

1 Like

I used to do this with AppleScript quite a long while back. Changes in the system broke my script, but I didn’t really need it anymore so I never got around to fixing it (probably back around 10.9-10.10?) but I am 99% sure this still is the best way to do this, save the pdfs into the folder Hazel is watching and it will be entirely seamless. If you are really clever, you might be able to add the message: URL to the finder comments for the PDF so you can pull up the actual email :smiley:

OK, here’s the basic bits to add to the above script. Should work, but I would have to wait to get a pdf to be sure.

repeat with oneAttachment in mail attachments of oneMessage
	set savePath to (get path to downloads folder) & ":" & subj & ".pdf"
	save oneAttachment in savePath
	tell application "Finder" to set comment of savePath to result of getMURL
end repeat

(* at end of script, before end using *)
	on getMURL with oneMessage
		set myID to content of every header of oneMessage whose name is "message-id"
		set myID to "message:" & myID
		return myID

get path to is a lot better than hard coding the path. There’s a lot of paths you can get this way, and it supports any macOS language

1 Like

welp, this does not work, and the above script does not work. At least not on … What OS is this 12.1? Yeah, Monterey 12.1. I assume it is some permission issue? Nothing gets written anywhere.

Even adapting the script to just run on the selected message nothing gets saved. And since it’s not something I need to do, I am not promising I’m going to take the time to solve it.

1 Like

I will try this mate, thanks a lot.

Thank you for taking the time to help, so then it’s not gonna work for me either, I’ve tried the IFTTT dropbox method and suddenly it stopped working.