Flagged Mac Mail to PDF

I am attempting to pull the message ID and a PDF of an email if it is or if it becomes flagged in Mac Mail. there may be a better way of doing this, and I hope there is because this is not proving to be reliable. I am having trouble getting the proper email message selected in the mail app before it attempts to export it as a pdf. Any help would be greatly appreciated. I was planning on using this as a mac mail rule, hoping that is possible.

Thank you!


set thePDFPath to (path to desktop folder as text)
set posixFolderPath to POSIX path of thePDFPath

tell application "Mail"
	set theMessages to (messages of first message viewer whose flag index is equal to 0)
	set i to 1
	repeat with Each_Message in theMessages
		set x to item i of theMessages
		--Export the Email as a PDF for later use in OmniFocus
		--Select the current message in Mail
		activate
		set selected messages of first message viewer to x
		activate
		delay 0.02
		tell application "System Events"
			activate application "Mail"
			tell process "Mail"
				click menu item "Export as PDF…" of menu 1 of menu bar item "File" of menu bar 1
				delay 5
				keystroke "g" using {command down, shift down}
				delay 5
				keystroke return
				delay 2
				keystroke return
			end tell
		end tell
		--Get the Message ID for use in OmniFocus
		set Message_ID to do shell script "/usr/bin/python -c 'import sys, urllib; print urllib.quote(sys.argv[1])' " & (message id of x)
		set Message_ID to "message://%3C" & (Message_ID) & "%3E"
		--Remove the flag
		set flagged status of x to false
		--Move the Message to the "OmniFocus" Email Folder
		set mailbox of x to mailbox "OmniFocus" of account id "02B41959-A90D-4FBB-B51D-4F9857B06F22"
		delay 0.05
		--Create OmniFocus Item with the Message_ID & the PDF File that was just created
		--Do stuff
		set i to i + 1
	end repeat
end tell

I found it…

https://macscripter.net/viewtopic.php?id=43790%202019/01/27

Thank you Yvan Koenig!!

@Yvan_Koenig and @maweir … I, too, am interested in printing flagged emails to PDF, but this link appears to be broken: https://macscripter.net/viewtopic.php?id=43790%202019/01/27

Can you share a new link or your solution?

Thanks, but I’m specifically looking at how to act on only the flagged emails.

The script above shows how to do that in a basic way.

This terminology work on macOS 10.14.6 Mojave:

tell application "Mail"
   set mailboxRef to inbox
   set flaggedMessageList to messages of mailboxRef whose flagged status is true
   set flaggedMessageIndexList to flag index of messages of mailboxRef whose flagged status is true
   set greenFlaggedMessageList to messages of mailboxRef whose flag index is 3
end tell

Looking for that old URL didn’t return much, and it wasn’t on the Wayback Machine.

Search results for 'Jan 27, '19 @Yvan_Koenig mail order:latest' - MacScripter

1 Like

Thank you. I always forget about the Wayback Machine.

1 Like