AppleScript From Mail Rule

Having some issues with calling an ApleScript from a Mail Rule. The purpose of the script is to send an email to the sender of the email identified by the Mail rule and attach a file to it. Here’s the script:

using terms from application "Mail"
	on perform mail action with messages The_Messages
		set FilePath to {"Macintosh HD:Users:Dad:Documents:Scores"} as string
		tell application "Mail"
			repeat with thisMessage in The_Messages
				set theSubject to subject of thisMessage
				display dialog "processing message " & theSubject
				set theSenderName to extract name from sender of thisMessage
				set theSenderAddress to extract address from sender of thisMessage
				if theSubject contains "SCORE001" then
					set FilePath to {"Macintosh HD:Users:Dad:Documents:Scores:SCORE001.pdf"} as string
				else if theSubject contains "SCORE002" then
					set FilePath to {"Macintosh HD:Users:Dad:Documents:Scores:SCORE002.pdf"} as string
				end if
				set file_attachment to FilePath as alias
				set newSubject to "Thank you for your score order"
				set theBody to "Thank you for ordering my music Score.  Your score is attached to this email as a pdf file." & return & return
				display dialog "making new message"
				set newMessage to make new outgoing message with properties {subject:newSubject, content:theBody}
				display dialog "adding recipient"
				tell newMessage to make new to recipient with properties {name:theSenderName, address:theSenderAddress}
				tell newMessage to make new attachment with properties {file name:file_attachment} at after the last word of the last paragraph
				send newMessage
			end repeat
		end tell
	end perform mail action with messages
end using terms from

You’ll see I’ve got some display dialog statements in there to track whats going on. The last display I see is the “making new message” one, the script doesn;t reach the “adding recipient display”.

Any ideas what is going wrong?

Hi phaworth,

According to my experience with scripting Apple Mail you are not supposed to use any user interaction (e.g. «display dialog», «choose from list») in mail rule scripts. Using these elements will often even crash the complete Mail app.

See this…

And also this…

Thanks. I put the displys in there to track where things were going wrong. Even without them, no message gets sent. Thanks for the inks though - they led me indirectly to a bunch of info that sending mail from within a script invoked from a Mail rule stopped working in Leopard and a possible workaround for it which I’m trying out right now.