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