Extracting PDF files from Mail.app and renaming them with current date

Hello

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 ?

Thanks in advance

Hi,

try this



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

i have created a rule initiating your script each time an email with an attachment ending with PDF, is received.

But no effect. The directory Incoming PDF remains empty when such a mail is received.

I’ve tested the script successfully by commenting out the event handler and set the variable theMessages to the selected items.

Is there a rule before that rule which has a “full stop” command in it?

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.

Hello.

Can you please telll us the criteria of your rule? :slight_smile:

Best Regards

McUsr

Rule is :

If ANY attachment Name ends with pdf

then :

  • play a sound “Glass”
  • Run AppleScript : test.scpt (the one that StefanK gave me)

When an incoming mail arrives with an appended PDF, i hear the sound Glass, but there is nothing in the Incoming OCR directory.

If I manually select the new incoming mail, and then “Apply rules”, I hear the sound Glass, and I find a corresponding file in Incoming OCR

I’m wondering if you have removed the old rule?

Best Regards

McUsr

Yes I can confirm, there is ABSOLUTELY no other rule.

I’m sorry that I can’t help you because mail at this moment seem to strip off the attachments before I send them!

Best Regards

McUsr

Do you manage to send an email with an attachment to yourself?

Can you see that the attachment is there when you receive it back?

-Odd questions but …

-I think I have enabled something that automatically strips of the attachments and puts them in my download folder.

Best Regards

McUsr

Hi,

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")

Best regards,

Martin

I don’t think so. The message id plus the timestamp including seconds is indeed an unique file name

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.

Aaaaaaaaaahhhh!

Got it :slight_smile:

Hi Stefan,

I just created a Mail rule with the following script and I always get ‘MAIL ID ERROR’ in the Console :smiley:


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.

Best regards,

Martin

Speaking of attachments:

I send a mail from my Mac to my Imap account … I get two - 2 emails back and the attachment as become invisible.

the two mail may come of some rule I guess, but the invisible attachments???

I’ll plunder with this until it is solved.
It is really a great evening any way, testing out the new Safari 5! :slight_smile:

Best Regards

McUsr

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 … ?

Thanks in advance.

try this



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