trick to get mail.app to run applescript from rule???

is there? does anyone know the answer??? i have tried compiled script, application, putting my whole script in a on…end handler… i am at my wits end!!!

thank you…

Here are a couple of example scripts that one of the Apple engineers passes around when someone asks the same question:

on perform_mail_action(info)
	tell application "Mail"
		set selectedMessages to |SelectedMessages| of info
		set theRule to |Rule| of info
		repeat with eachMessage in selectedMessages
			set theSubject to subject of eachMessage
			set theRuleName to name of theRule
			set theText to "The rule named '" & theRuleName & "' matched this message:" & return & return & "Subject: " & theSubject
			display dialog theText
		end repeat
	end tell
end perform_mail_action
on perform_mail_action(info)
	display dialog "Hi!"
end perform_mail_action

The engineer also promised to include better documentation on the topic in the next update to Mail’s help section. :shock:

I’m just gonna go ahead and give this topic a hefty bump. I can’t get mail to launch a script either. It’s rather frustrating. And, I’m a n00b, so I’m afraid that Rob’s examples wen’t a bit over my head.

Any other pointers?

Ok, I’m not a Mail user but I’ll try to explain as I see it. Using the following code as a template:

on perform_mail_action(info)
	tell application "Mail"
		set selectedMessages to |SelectedMessages| of info
		repeat with eachMessage in selectedMessages
			-- add your stuff here
			-- do something to eachMessage
		end repeat
	end tell
end perform_mail_action

You would add your code where indicated. It would process each message which met the criteria defined by the rule. To make it work:

  1. Write the script. Since the above script will only run when called by a Mail rule, you’ll need to use another script to test/debug your code before plugging it into the template above. So to debug, use this:
tell application "Mail"
	-- do stuff
end tell
  1. When the script is complete, go to Mail’s preferences and open the Rules pane.

  2. Define the criteria that should trigger the script.

  3. As the action, select “Run AppleScript” and then hit the “Choose” button and navigate to your script. The script can be stored anywhere that’s convenient.

Now, when an incoming message meets the criteria that you’ve defined, the script should kick in automatically. You can also apply the rules manually. Just select some messages and, from the “Message” menu, choose “Apply Rules to Selection”.

I’m sure that I haven’t stated this as clearly as is possible so feel free to ask for clarification. :slight_smile:

Rob, you’re making sense, and thanks for the reply. I think that I’m not being too clear though.

The problem with Mail.app is that is when you create a rule to launch a script, the script just doesn’t launch. I even tried putting the script in the Library (because OS X can be fussy somtimes) and it still didn’t launch.

Perhaps if the first script you cited were always open, it may work. I suppose it’ll watch mail, and when the criteria are met, it’ll do its thing. I guess I’ll give it a whirl. It’s not the nicest solution, but it’ll probably do the trick.

Thanks again!

Perhaps you have a misconception of what should happen. If you expect to see a script launch, and show up in the Dock, I don’t think you’ll see that happen when the script is triggered by a Mail rule. The script should just execute somewhat invisibly and you should see only the result of what the script does.

Regarding the location of the script, one of the Mail or AppleScript programmers has stated, in answer to a direct question on this very topic, that it doesn’t matter where the file is stored. :slight_smile:

I don’t know what to tell you regarding the scripts not working for you. Every time I’ve experimented using the code above, the rules/scripts have executed as expected.

Holy smokes. It worked. It’s so counter intuitive, but it works. I just pasted the code you posted around my script, and it actually worked.

I wouldn’t have thought that the script should have to address mail.app at all, but who cares, it works.

Thank you a thousand thank yous!

:smiley:

Great, I’m glad it worked! A thousand you’re welcomes back at ya. :wink:

Have fun,
Rob

Thanks! I had been stumbling around frustrated for hours and now I have a working filter script just like that. Peachy!