Attach AppleScripts to Mail.app rules

Jan 16, '03 10:11:18AM

Contributed by: russh

In case you missed it, the version of mail included with 10.2.3 can execute Applescripts as part of a rule. This will open up a lot of possibilities. A member of the Mail.app development team passed along this script that uses the 'perform_mail_action' handler to access messages that match the given rule or rule object. In other words, it kicks up a dialog box that automagically tells you a rule was matched and what the message was that matched.

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
In the example, 'info' is an Applescript record with two keys: |SelectedMessages| and |Rule|. |SelectedMessages| is a list of message objects that match the conditions for the rule. As messages are fetched and evaluated in batches, this list may contain more than one message, so be sure to take that into account. |Rule| is the rule object that triggered the script action.

You don't have to make any modifications to the script itself to make it work. Simply save the above text as a compiled script editor and include it with a mail rule to see how it works.

[Editor's note: I have not tested this one other than ensuring that the script syntax is acceptable to the Script Editor.]

Comments (31)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20030116071118239