Script Not Working in Mail 2.x Rule, But Works From Script Editor?

I’m trying to tackle a completely off-the-wall problem, and one such element of dealing with it requires Mail.app to export the text of any email it receives as a file on the Desktop (or whatever location ultimately works). I found the following script (I think it was here; apologies for not giving proper credit if I’ve copied this from someone here):

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		
		tell application "Finder" to set pathToDesktop to ("Macintosh HD:Users:shortusername:Desktop:") as string
		tell application "Mail"
			set theMessages to selection
			repeat with theMessage in theMessages
				set theText to content of theMessage
				set theFile to pathToDesktop & "Command.txt"
				set theFileID to open for access file theFile with write permission
				write theText to theFileID
				close access theFileID
			end repeat
		end tell
		
		
	end perform mail action with messages
end using terms from

Here’s my problem: if I comment out the ‘using terms from’ and the ‘perform mail action’ lines at the start and end of the script and just run this directly from Script Editor starting with “tell application ‘Finder’”, it works perfectly. But without commenting out those two lines, Mail.app refuses to run the script as part of a rule. I know the rule is running, as it’s also moving the matching message to the ‘Drafts’ folder (which I added to the rule as a test to see if it was running).

Here’s the rub, though: this machine is running Tiger (10.4.11) and Mail 2.1.3, and I don’t really have any options for upgrading to Leopard. In addition, I don’t have a “real” mail account set up yet (waiting on our IT guys to decide how they want to let it through…long story), so the account set up in Mail.app is just a dummy account for now and it’s working offline. I don’t think that should affect anything; the rule is working, but the test message I set up and am using is just a draft I saved within Mail.

Anyone know why the script would work when I run it manually but not as part of a Mail.app rule? Apologies if this is a simple question to answer; I’m pretty new to AppleScript, working more in good old SuperCard. Many thanks in advance!

Model: iMac G4 1.83 Intel Core Duo
AppleScript: 1.10.7
Browser: Safari 4.1.3
Operating System: Mac OS X (10.4)

I think I know why… I had a similar problem a while ago, and it took me forever to figure out why it was happening, but it has such a simple answer:
I could be wrong, but I think Apple has some kind of rule that blocks certain events from being AppleScripted in mail. Especially dialog boxes and exported files… It would prevent people from making viruses, or auto-executing AppleScripts, etc…
Unfortunately, I do not know of a simple solution. Similar blocks do not exist in other languages, so try that/those… ;D

Hi,

the problem is probably the selection line.
The perform mail action event passes the appropriate messages in its theMessages parameter.
I recommend also to use a handler for the writing part.


using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		repeat with aMessage in theMessages
			set theContent to content of aMessage
			writeToFile of me from theContent
		end repeat
	end perform mail action with messages
end using terms from

on writeToFile from theText
	set theFile to (path to desktop as text) & "Command.txt"
	try
		set theFileID to open for access file theFile with write permission
		write theText to theFileID
		close access theFileID
	on error
		try
			close access file theFile
		end try
	end try
end writeToFile

Yup, still doesn’t work as a rule within Mail. I think Catalyst was on to something with that thing about dialogs and exports; there’s a sample script that’s supposed to generate a dialog box when triggered by a rule. Every time I try that, it crashes Mail.app before it can finish drawing the dialog box.

Regardless, I think I’ve found a workaround by building the script into an external application I’m building and triggering it that way, so I think I’m good. Thanks both for your help!