Bug when creating a Mail rule that fires an AppleScript?

Hello everyone!

I’ve written an AppleScript that creates a new Mail rule and attempts to tell that Mail rule to run an AppleScript.

tell application "Finder"
	set theScriptPath to (home as string) & "Library:Application Scripts:com.apple.mail:My script.scpt"
end tell

tell application "Mail"
	set theNewRule to make new rule at end of rules with properties {name:"Test rule"}
	tell theNewRule
		make new rule condition at end of rule conditions with properties {rule type:matches every message}
		set run script to theScriptPath
		set enabled to true
	end tell
	set scriptPathTest to ""
	try
		set scriptPathTest to run script of first rule whose name is "Test rule"
	end try
end tell

The rule is successfully created, but in Mail on OS X 10.8.2 the ‘Run AppleScript’ set has ‘No script selected’ in its popup menu.

I added the scriptPathTest step at the end to try and work out what’s going behind the scenes. The real path to the script file is:

“Macintosh HD:Users:tom:Library:Application Scripts:com.apple.mail:My script.scpt”

However, here it returns:

“Macintosh HD:Users:tom:Library:Application Scripts:com.apple.mail:Users:tom:Library:Application Scripts:com.apple.mail:My script.scpt”

If I go into Mail and manually select the script, AppleScript returns the correct script path.

So. has anyone come across this before? Is it something that I’m doing wrong, or a bug in Mail when creating a rule that runs an AppleScript?

Hi,

the dictionary says, the parameter must be file specifier or missing value, so try


 set run script to file theScriptPath

Thanks for the suggestion, Stefan, but I tried that originally and there’s no difference. I should have put it back into my sample script before I posted it, so here it is again:

tell application "Finder"
	set theScriptPath to (home as string) & "Library:Application Scripts:com.apple.mail:My script.scpt"
end tell

tell application "Mail"
	set theNewRule to make new rule at end of rules with properties {name:"Test rule"}
	tell theNewRule
		make new rule condition at end of rule conditions with properties {rule type:matches every message}
		set run script to file theScriptPath
		set enabled to true
	end tell
	set scriptPathTest to ""
	try
		set scriptPathTest to run script of first rule whose name is "Test rule"
	end try
end tell

Maybe it’s a 10.8 issue.
In Lion it works even assigning the script path without the file keyword

Thanks, Stefan: that’s interesting to hear.