Apple script in Applemail

I am trying to set up a script that processes some incoming email. I tried to clone the sample script provided in Applescript that simply extracts the message body and displays it in a dialog. While I get no errors, neither do I get the dialog. It appears that the script simply will not run. Here is my script:

using terms from application "Mail"
	on perform mail action with messages theSelectedMessages
		tell application "Mail"
			set theSource to source of item 1 of theSelectedMessages
			set theSubject to subject of item 1 of theSelectedMessages
		end tell
	end perform mail action with messages
end using terms from
display dialog theSubject
display dialog theSource

I don’t know how I can make it any simpler. Can anyone see why this is not working? TIA.

I would say it’s because there isn’t a message selected to process plus your display dialog lines are outside the script - try this:


using terms from application "Mail"
	on perform mail action with messages theMessages
		repeat with thisMail in theMessages
			tell thisMail
				set the_subject to get subject
				set the_sender to get sender
			end tell
			display dialog the_subject
			display dialog the_sender
		end repeat
	end perform mail action with messages
end using terms from

Thanks for the reply. That didn’t work either. Its hard to beleive there was no message selected. Since it is a mail rule, shouldn’t mail be passing the message that fits the rule as the selected message? How else could it ever work?

You would think so but I had an identical problem and the above script (in an expanded form) fixed it. Maybe you’re doing something wrong with setting up the rule in Mail - I set it up as:

if Any of the folllowing conditions are met:
Every message
perform the following actions:
Run AppleScript → name of Applescript.

Using this with the script I posted works exactly how you specified in your original post. The only reason I can think of that it wouldn’t work is that you are using an older version of Mail - I only have 2.02

Hm! I’m using 1.3.11 but that is the latest for Panther. We have not yet upgraded to Tiger. I know the rule is working because the first commadn is to move the message to a specific folder. Is it possible that if running a script it must be the only command in the rule? The order doesn’t seem to matter.

I’m an Entourage user so I’m not sure, try sticking the applescript rule in front of the other one and see if the order makes a difference…

Thanks but the order does not matter.

:frowning: Shame on me. I tried changing the order again and this time it worked. I don’t know what I did the first time that made it not work. Of course this begs the question, why should the order matter?