Mail (10.8) breaks applescript to save & print pdf-attachments for POP

We receive many many e-mails daily with pdf attachments (every fax is converted to pdf…)

Our goal is to save and print automatically pdf-attachments with growl-integration (display message), for specified e-mail addresses, and then to store the e-mail in a local and/or IMAP-folder (e-mails are later deleted after more then 6 months).

Part I:
The script was working perfect for years an OS Lion (10.7), either for POP and IMAP, see below.

Part II:
The script was updated for Mountain Lion (10.8), due to sandboxing. It works partially, but…

Part I:
everthing works on OS Lion (10.7), the script can be stored wherever you want

Mail-Rule:
IF sender contains "fax@example.ch
THEN move the e-mail to folder “stored fax”, mark it as read and run AppleScript “saveprintpdf”

AppleScript “saveprintpdf”
Code:

-- this path MUST exist!
property savePdfTo : "Hardisk:Users:ebi:folder"

-- do not change this (thats for Growl)
property THIS_APP_NAME : "SaveAndPrintAttachement"

-- main entry point of mail app
on perform_mail_action(ruleData)
	
	-- growl integration
	my registerWithGrowl()
	
	-- The folder to save the attachments in (must already exist)
	set attachmentsFolder to savePdfTo as text
	
	-- Get incoming messages that match the rule
	tell application "Mail"
		set selectedMessages to |SelectedMessages| of ruleData
		repeat with theMessage in selectedMessages
			
			-- Save the attachment
			repeat with theAttachment in theMessage's mail attachments
				set originalName to name of theAttachment
				set savePath to attachmentsFolder & ":" & originalName
				try
					save theAttachment in savePath
					my printfile(savePath)
					my displayGrowlMessage("Fax empfangen", "Ein Fax wurde am " & theMessage's date sent & " empfangen, ausgedruckt und im Ordner 'Arztberichte' abgelegt.")
				end try
			end repeat
		end repeat
	end tell
	
end perform_mail_action

-- Adds leading zeros to date components
on pad(n)
	return text -2 thru -1 of ("00" & n)
end pad

-- I am printing the given file with the chosen application
on printfile(filepath)
	
	-- printing via the command line
	set cmd to "lpr " & quoted form of (POSIX path of filepath)
	set cmd to cmd as «class utf8»
	do shell script cmd
	
end printfile

(*
Growl Function
*)
on isGrowlRunning()
	tell application "System Events"
		(count of (every process whose bundle identifier is "com.Growl.GrowlHelperApp")) > 0
	end tell
end isGrowlRunning

on registerWithGrowl()
	
	if isGrowlRunning() then
		tell application id "com.Growl.GrowlHelperApp"
			-- Make a list of all the notification types 
			-- that this script will ever send:
			set the allNotificationsList to {"Info"}
			
			-- Make a list of the notifications 
			-- that will be enabled by default.      
			-- Those not enabled by default can be enabled later 
			-- in the 'Applications' tab of the growl prefpane.
			copy the allNotificationsList to the enabledNotificationsList
			
			-- Register our script with growl.
			-- You can optionally (as here) set a default icon 
			-- for this script's notifications.
			register as application THIS_APP_NAME ¬
				all notifications allNotificationsList ¬
				default notifications enabledNotificationsList ¬
				icon of application "Mail"
		end tell
	end if
end registerWithGrowl

on displayGrowlMessage(growlTitle, growlText)
	if isGrowlRunning() then
		tell application id "com.Growl.GrowlHelperApp"
			notify with name "Info" title growlTitle description growlText application name THIS_APP_NAME
		end tell
	else
		display dialog growlText buttons {"OK"} default button 1 with title growlTitle
	end if
end displayGrowlMessage


Part II:

Now the script for Mac OS Mountain Lion (10.8). Remember: For 10.8, the script can only be stored at:
~/Library/Application Scripts/com.apple.mail/

As described, the script works only for IMAP. For POP, it works only manually. :rolleyes:

:cool: Hopefully, someone can help us to get it working for IMAP and POP-Accounts! :cool:
The error display for POP-Accounts is:

same Mail-Rule:
IF sender contains "fax@example.ch
THEN move the e-mail to folder “stored fax”, mark it as read and run AppleScript “saveprintpdf”

AppleScript “saveprintpdf”

Code:

-- this path MUST exist!
property savePdfTo : "Hardisk:Users:ebi:folder"

-- do not change this (thats for Growl)
property THIS_APP_NAME : "SaveAndPrintAttachement"

-- main entry point of mail app
--on perform_mail_action(ruleData)
using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		
		try
			-- growl integration
			my registerWithGrowl()
			
			-- The folder to save the attachments in (must already exist)
			set attachmentsFolder to savePdfTo as rich text
			
			-- The downloads folder
			set downloadFolder to ((path to home folder) as rich text) & "Downloads"
			
			-- Get incoming messages that match the rule
			tell application "Mail"
				--set selectedMessages to |SelectedMessages| of ruleData
				repeat with theMessage in theMessages
					
					-- Save the attachment
					repeat with theAttachment in theMessage's mail attachments
						set originalName to name of theAttachment
						set downloadSavePath to downloadFolder & ":" & originalName
						set savePath to attachmentsFolder & ":" & originalName
						try
							-- save file
							save theAttachment in downloadSavePath
							
							-- move file
							my movefile(downloadSavePath, attachmentsFolder)
							
							-- print file
							my printfile(savePath)
							
							-- display growl-message
							my displayGrowlMessage("Fax empfangen", "Ein Fax wurde am " & theMessage's date sent & " empfangen, ausgedruckt und im Ordner 'Arztberichte' abgelegt.")
						on error msg
							display dialog "Error: " & msg buttons {"OK"} default button 1 with title "Fehler beim Faxempfang"
						end try
					end repeat
				end repeat
			end tell
			
		on error msg
			display dialog "Error: " & msg buttons {"OK"} default button 1 with title "Fehler beim Faxempfang"
		end try
		
	end perform mail action with messages
end using terms from
--end perform_mail_action

-- Adds leading zeros to date components
on pad(n)
	return text -2 thru -1 of ("00" & n)
end pad

-- I am moving the given file
on movefile(filepath, targetpath)
	
	-- move via the command line
	set cmd to "mv -f " & quoted form of (POSIX path of filepath)
	set cmd to cmd & " " & quoted form of (POSIX path of targetpath)
	set cmd to cmd as «class utf8»
	do shell script cmd
	
end movefile

-- I am printing the given file with the chosen application
on printfile(filepath)
	
	-- printing via the command line
	set cmd to "lpr " & quoted form of (POSIX path of filepath)
	set cmd to cmd as «class utf8»
	do shell script cmd
	
end printfile

(*
Growl Funktionen
*)
on isGrowlRunning()
	tell application "System Events"
		(count of (every process whose bundle identifier is "com.Growl.GrowlHelperApp")) > 0
	end tell
end isGrowlRunning

on registerWithGrowl()
	
	if isGrowlRunning() then
		tell application id "com.Growl.GrowlHelperApp"
			-- Make a list of all the notification types 
			-- that this script will ever send:
			set the allNotificationsList to {"Info"}
			
			-- Make a list of the notifications 
			-- that will be enabled by default.      
			-- Those not enabled by default can be enabled later 
			-- in the 'Applications' tab of the growl prefpane.
			copy the allNotificationsList to the enabledNotificationsList
			
			-- Register our script with growl.
			-- You can optionally (as here) set a default icon 
			-- for this script's notifications.
			register as application THIS_APP_NAME ¬
				all notifications allNotificationsList ¬
				default notifications enabledNotificationsList ¬
				icon of application "Mail"
		end tell
	end if
end registerWithGrowl

on displayGrowlMessage(growlTitle, growlText)
	if isGrowlRunning() then
		tell application id "com.Growl.GrowlHelperApp"
			notify with name "Info" title growlTitle description growlText application name THIS_APP_NAME
		end tell
	else
		display dialog growlText buttons {"OK"} default button 1 with title growlTitle
	end if
end displayGrowlMessage