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

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

Stefan, great job, this is working perfect.

In fact the problem was with the message ID that seems to be not assigned when processing rules.

Last question. How can I include code in your script, so that only pdf files larger than 100 kb are to be processed ?

replace

if MIME type of thisItem is "application/pdf" then

with


if MIME type of thisItem is "application/pdf" and file size of thisItem > 100000 then

Stefan,

I don’t know if you are still online on this forum.

I have upgraded OSX Lion and your script does not work anymore.

Do you have any ideas ?

Thanks.

The save line throws always an error.
I tried to put the Scripting Additions commands out of the scope of Mail and also specified the path as POSIX path.
No success.

It seems to be a bug

Yes, there has to be a bug…

Should I refer this bug to Apple ?

Any help would be appreciated :slight_smile: