Applescript and Mail

Howdy!
I’ve been struggling with scripting Mail in Leopard. I have several questions if anyone out there can be of assistance.

  1. I want to create a Mail rule/applescript that sends a new message for received messages with a specific subject, but I couldn’t even get past creating the message. I’ve stripped down the code to the bare essentials that includes the problem code.
using terms from application "Mail"
	on perform mail action with messages theSelectedMessages for rule theRule
		repeat with a from 1 to count theSelectedMessages
			set read status of item a of theSelectedMessages to false
			try
				make new outgoing message with properties {visible:true, subject:"the subject", content:"the body"}
			on error errMsg number errNum
				set flagged status of item a of theSelectedMessages to true
			end try
		end repeat
	end perform mail action with messages
end using terms from

The “set read status” and “set flagged status” are only there for crude debugging since is seems to be nearly impossible to get reliable feedback as the script runs from the rule. When I select a message and choose to apply rules, the read status gets changed to false as it should, but the make new outgoing message apparently causes and error since no message is created and the flagged status is changed to true. The console logs this every time I apply the rules.

1/16/08 2:02:31 PM Mail[158] An exception was thrown during execution of an NSScriptCommand…
1/16/08 2:02:31 PM Mail[158] load must be called on the main thread

Any suggestions? Anyone get this kind of thing to work? I’ve googled the web and haven’t found anything definitive, but I’m getting the impression this is a bug. Hopefully it’s a foolish mistake on my part that someone can correct.

  1. In the above code I would prefer to use use “reply” instead of “make new outgoing message” but something as simple as this:
tell application "Mail"
	set theSelection to selection
	repeat with i from 1 to count of theSelection
		set theMessage to item i of theSelection
		set outMessage to reply theMessage
	end repeat
end tell

yields weird results when tested in script debugger. Two windows are created - the reply message and for some reason a new message window. Also, when I look at the properties of outMessage I see a lot of this:
Get failed: no such object (e.g. specifier asked for the 3rd, but there are only 2 errAENoSuchObject:-1728

Any ideas would be appreciated.

Hi,

in your first script you must target Mail explicitly, only using terms from is not sufficient


using terms from application "Mail"
	on perform mail action with messages theSelectedMessages for rule theRule
		tell application "Mail"
			repeat with oneMessage in theSelectedMessages
				set read status of oneMessage to false
				try
					make new outgoing message with properties {visible:true, subject:"the subject", content:"the body"}
				on error errMsg number errNum
					set flagged status of oneMessage to true
				end try
			end repeat
		end tell
	end perform mail action with messages
end using terms from