This script does work with Mail 4 but not with Mail 5 (Lion)

I used an AppleScript as a Mail rule under Snow Leopard, and now that I have upgraded to Lion, this AppleScript does not run anymore with Mail.app 5.

This script is run as a Mail Rule, each time there is an incoming message with one or multiple PDF or JPG as attachment(s).

It extracts the attachment(s) to a directory and gives an unique file name based on the date/time.

It seems to be a problem with compatibility between Mail.app 4 and Mail.app 5 so it should not be difficult to resolve.

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 rich text) & "Incoming OCR:"
		set x to 1
		repeat with thisMessage in theMessages
			tell mail attachments of thisMessage to set {attachs, cnt} to {it, count it}
			set y to 1
			repeat with thisItem in attachs
				try
					if MIME type of thisItem is "application/pdf" and file size of thisItem > 11000 then
						tell ((date received of thisMessage) as «class isot» as string) to ¬
							tell ((date received of thisMessage) as «class isot» as string) to ¬
								rich text 1 thru 4 & "-" & rich text 6 thru 7 & "-" & rich text 9 thru 10 & "-" & ¬
								rich text 12 thru 13 & "." & rich text 15 thru 16 & "." & rich text 18 thru 19
						
						
						save thisItem in (outputPath & result & "-#" & x & "-" & y & ".pdf")
						set y to y + 1
					end if
					
					
					
					if MIME type of thisItem is "image/jpeg" and file size of thisItem > 11000 then
						tell ((date received of thisMessage) as «class isot» as string) to ¬
							tell ((date received of thisMessage) as «class isot» as string) to ¬
								rich text 1 thru 4 & "-" & rich text 6 thru 7 & "-" & rich text 9 thru 10 & "-" & ¬
								rich text 12 thru 13 & "." & rich text 15 thru 16 & "." & rich text 18 thru 19
						
						
						save thisItem in (outputPath & result & "-#" & x & "-" & y & ".jpg")
						set y to y + 1
					end if
				end try
			end repeat
			set x to x + 1
		end repeat
	end perform mail action with messages
end using terms from

Model: iMac
AppleScript: 2.4
Browser: Safari 534.48.3
Operating System: Mac OS X (10.7)

Hi,

this script uses also the save [attachments] command of Mail.app, which is broken in Lion

Stefan

This script seems to be unusable with Mountain Lion.

In the log, i get a : sandboxd: deny-file-write-create

Any hint for forcing sandbox to allow Mail script to create the file ?

Thanks

I have no idea, but I guess there is no way, because the sandbox is very restrictive

Hmm, that’s bad. Thanks tho :slight_smile:

Update: It seems that the saving mail attachments bug in Mail.app is fixed on 10.7.4

Update (thanks to Andreas Amann): the following change in the code works :

save thisItem in (POSIX file (outputPath & result & "-#" & x & "-" & y & ".pdf"))