Running Applescript with Mail Rule

I have a script that runs fine by itself, but when I try to run it automatically with a Mail Rule, it doesn’t work.


using terms from application "Mail"
	on perform mail action with messages theMessages for rule SaveOrderFiles
		tell application "Finder"
			try
				mount volume "afp://192.168.2.11/CustomerFiles" as user name "user"
			on error
				display dialog "There was an error mounting the Volume." & return & return & "The server may be unavailable at this time." & return & return & "Please inform the Network Administrator if the problem continues." buttons {"Okay"} default button 1
			end try
		end tell
		tell application "Mail"
			repeat with eachMessage in (get selection)
				set newText to paragraphs of content
				tell eachMessage
					repeat with i in newText
						if i starts with "Order ID:" then
							set order_id to text 10 thru length of i
						end if
						if i starts with "https" then
							set download to text of i
						end if
					end repeat
				end tell
				try
					set destFolder to "CustomerFiles:UPLOADED_ORDER_FILES:"
					set posixPath to quoted form of POSIX path of destFolder
					do shell script "curl " & download & " -o " & posixPath & order_id & ".zip"
				on error
					-- if error, download manually
					try
						tell application "Finder"
							activate
							display dialog "Oops! There was a download error. Please download by clicking the download link in the order email."
						end tell
					end try
				end try
			end repeat
		end tell
	end perform mail action with messages
end using terms from

Hi,

Mail.app does not support user interaction (e.g. display dialog) in mail rules

Thanks Stefan

I removed the dialog blocks, but the script still won’t run.


using terms from application "Mail"
	on perform mail action with messages theMessages for rule SaveOrderFiles
		tell application "Mail"
			repeat with eachMessage in (get selection)
				set newText to paragraphs of content
				tell eachMessage
					repeat with i in newText
						if i starts with "Order ID:" then
							set order_id to text 10 thru length of i
						end if
						if i starts with "https" then
							set download to text of i
						end if
					end repeat
				end tell
				try
					set destFolder to "CustomerFiles:UPLOADED_ORDER_FILES:"
					set posixPath to quoted form of POSIX path of destFolder
					do shell script "curl " & download & " -o " & posixPath & order_id & ".zip"
				end try
			end repeat
		end tell
	end perform mail action with messages
end using terms from

I guess that selection is also regarded as user interaction.

The messages in the variable theMessages are the incoming messages which match the rule conditions.

Had to put this on hold for a while, but now I’m back.
My latest incantation does not work.


using terms from application "Mail"
	on perform mail action with messages theMessages for rule SaveOrderFiles
		repeat with eachMessage in theMessages
			tell application "Mail" to set orderText to paragraphs of the content of eachMessage
			repeat with i in orderText
				if i starts with "Order ID:" then
					set order_id to text 10 thru length of i
				end if
				if i starts with "https" then
					set download to text of i
				end if
				set destFolder to "CustomerFiles:UPLOADED_ORDER_FILES:"
				set posixPath to quoted form of POSIX path of destFolder
				do shell script "curl " & download & " -o " & posixPath & order_id & ".zip"
			end repeat
		end repeat
	end perform mail action with messages
end using terms from

???

Hi,

the curl line will be already executed in the first iteration of the inner repeat loop,
at this moment only one of the curl parameters could be set and the script throws definitely an error,
which causes the script to abort silently in a mail rule

Try this


using terms from application "Mail"
	on perform mail action with messages theMessages for rule SaveOrderFiles
		set posixPath to "/Volumes/CustomerFiles/UPLOADED_ORDER_FILES/"
		repeat with eachMessage in theMessages
			set orderIDFound to false
			set downloadFound to false
			tell application "Mail" to set orderText to paragraphs of the content of eachMessage
			repeat with i in orderText
				if i starts with "Order ID:" then
					set order_id to text 10 thru length of i
					set orderIDFound to true
				else if i starts with "https" then
					set download to text of i
					set downloadFound to true
				end if
				if orderIDFound and downloadFound then
					do shell script "curl " & quoted form of download & " -o " & quoted form of (posixPath & order_id & ".zip")
					exit repeat
				end if
			end repeat
		end repeat
	end perform mail action with messages
end using terms from

Finally got back to this project, and again, I can’t thank you enough, Stefan :smiley:
I get to work on my scripts for 20 minutes once or twice a month, then wonder why I struggle with them sometimes… :confused:

I modified your script, and it still wouldn’t work, but I knew everything was correct.
I finally deciphered that our download site’s CA certificate had expired, and the new one was self-signed :(, and not trusted by cURL.

So until I can get the cert updated, I added the “-k” to disable verification, and here is the revised script that works just fine:


using terms from application "Mail"
	on perform mail action with messages theMessages for rule SaveOrderFiles
		set destFolder to "CustomerFiles:_UPLOADED_ORDER_FILES:"
		set posixPath to POSIX path of destFolder
		repeat with eachMessage in theMessages
			set orderIDFound to false
			set downloadFound to false
			tell application "Mail" to set orderText to paragraphs of the content of eachMessage
			repeat with i in orderText
				if i starts with "Order ID:" then
					set order_id to text 11 thru 20 of i
					set orderIDFound to true
				else if i starts with "Message Tracking" then
					set order_id to text 27 thru 36 of i
					set orderIDFound to true
				else if i starts with "https" then
					set download to text of i
					set downloadFound to true
				end if
				if orderIDFound and downloadFound then
					do shell script "curl -k " & quoted form of download & " -o " & quoted form of (posixPath & order_id & ".zip")
					exit repeat
				end if
			end repeat
			tell application "Mail"
				set myMailbox to mailbox "___ORDERS"
				move eachMessage to myMailbox
			end tell
		end repeat
	end perform mail action with messages
end using terms from

Best regards