I successfully use this AppleScript for extracting selected messages in Mail.app into a desktop directory and giving them an unique filename with date:
on perform_mail_action(theData)
set outputPath to (path to desktop as Unicode text) & "Incoming OCR:"
set theseMessages to |SelectedMessages| of theData
tell application id "com.apple.mail"
repeat with thisMessage in theseMessages
repeat with thisItem in mail attachments of thisMessage
try
if MIME type of thisItem is "application/pdf" then
tell ((date received of thisMessage) as «class isot» as string) to ¬
tell ((date received of thisMessage) as «class isot» as string) to ¬
text 1 thru 4 & "-" & text 6 thru 7 & "-" & text 9 thru 10 & "-" & ¬
text 12 thru 13 & text 15 thru 16 & text 18 thru 19
save thisItem in (outputPath & result & "-" & id of thisMessage & "-" & id of thisItem & ".pdf")
end if
end try
end repeat
end repeat
end tell
end perform_mail_action
For this to work, I select all the messages in Mail.app and I execute a rule which executes this script. It means that this works perfectly, but for batch processing only that I do every weekend.
Now I would like to create a Mail.app rule starting a script each time a mail with a PDF attachment arrives into my e-mail. This rule would automatically run each time that kind of mail arrives, without my intervention.
Unfortunately, this above AppleScript does not execute correctly.
Being a novice in AppleScript, could someone tell me what needs to be modified in this script, so that each incoming message can be treated by a rule executing this script and thzn would extract all the appended PDF’s into my Desktop directory ?
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
set dt to path to desktop
set outputPath to (dt as text) & "Incoming OCR:"
repeat with thisMessage in theMessages
repeat with thisItem in (get mail attachments of thisMessage)
try
if MIME type of thisItem is "application/pdf" then
set timeStamp to do shell script "/bin/date +%Y%m%d_%H%M%S"
save thisItem in (outputPath & id of thisMessage & "_" & timeStamp & ".pdf")
end if
end try
end repeat
end repeat
end perform mail action with messages
end using terms from
Merci stefan, I have indeed made sure that the rule is properly executed when an appended PDF arrives : together with your script execution, the rule also plays a different noticable sound.
Stefan, exactly like my script, your script works when I select the message(s) in the mailbox, and then I right click and ask “Apply Rules”. Then I hear the special sound, and I find a file in ‘Incoming OCR’
But it does NOT work automatically when an incoming mail with a PDF is coming in. I hear the special sound, but no file is in the directory.
I remember having a similar problem some time ago (on Mac OS X 10.5/4 ?). It was caused by the fact that Mail.app seems to assign the IDs of the eMail messages AFTER executing the rules. That’s why I couldn’t use the ID to search for the corresponding emlx file on the client’s hard drive.
So I (wildly) guess the following line causes the problem:
save thisItem in (outputPath & id of thisMessage & "_" & timeStamp & ".pdf")
Try using the following code instead and see if that works:
set randnum to random number from 1000 to 9999
save thisItem in (outputPath & randnum & "_" & timeStamp & ".pdf")
Sure it is, but if there is no id of thisMessage yet, the code will silently quit (try…end try) without saving the PDF, which is exactly what currently happens.
I just created a Mail rule with the following script and I always get ‘MAIL ID ERROR’ in the Console
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
repeat with thisMessage in theMessages
try
do shell script "logger MAIL ID: " & quoted form of ((id of thisMessage) as text)
on error
do shell script "logger MAIL ID ERROR"
end try
end repeat
end perform mail action with messages
end using terms from
So using the id is not possible with incoming eMail messages.
In addition to using the random variable, and in order to differentiate multiple pdf documents attached in one e-mail, can you show me how to add an incremented variable embedded in repeat for all attached files, and put that variable into the filename like xxxxxx-1.pdf xxxxxx-2.pdf … ?
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
set dt to path to desktop
set outputPath to (dt as text) & "Incoming OCR:"
repeat with thisMessage in theMessages
tell mail attachments of thisMessage to set {attachs, cnt} to {it, count it}
set x to 1
repeat with thisItem in attachs
try
if MIME type of thisItem is "application/pdf" then
set timeStamp to ((random number from 1000 to 9999) as text) & "-" & (do shell script "/bin/date +%Y%m%d_%H%M%S")
if cnt > 1 then
save thisItem in (outputPath & timeStamp & "-" & x & ".pdf")
set x to x + 1
else
save thisItem in (outputPath & timeStamp & ".pdf")
end if
end if
end try
end repeat
end repeat
end perform mail action with messages
end using terms from