In Apple Mail I've gone to "Preferences > Rules" and made a rule to trigger an AppleScript (screenshot attached)
I know it's triggering because the background of the incoming message turns orange.
However, it seems that the AppleScript is not triggering. I've set it to do a simple "display dialog" to see if it's triggering but nothing happens. Can you help me see where I've gone wrong?
Thank you!
Script is from http://preserve.mactech.com/articles/mactech/Vol.21/21.09/ScriptingMail/index.html
Here's the script:
on perform_mail_action(theData)
tell application "Mail"
set theSelectedMessages to |SelectedMessages| of theData
set theRule to |Rule| of theData
repeat with a from 1 to count theSelectedMessages
-- Process the current message
display dialog "did this work?"
end repeat
end tell end perform_mail_action

The MacTech article may be out of date. The Apple pages on the subject I could find do not mention anything about that handler, with underscores, only with spaces.
The former implies that you don’t need a handler at all for your script. I’ve verified that this is true. The following script file is triggered on an incoming message, and does display the dialog:
display dialog "Hello without handler"
The latter page uses the following format, which I’ve also verified:
using terms from application "Mail"
on perform mail action with messages caughtMessages for rule catchingRule
display dialog "We got one!"
end perform mail action with messages
end using terms from
If you are using IMAP, you can get data from the incoming messages. For example:
using terms from application "Mail"
on perform mail action with messages caughtMessages for rule catchingRule
repeat with caughtMessage in caughtMessages
try
set mailSubject to subject of caughtMessage
display dialog mailSubject
on error errorString number errorNumber
display dialog errorString
end try
end repeat
end perform mail action with messages
end using terms from
If you are using POP, incoming messages are not available; you’ll get something like Can’t get «class mssg» "Incoming POP Messages" of «class mact» id "LONG-ID". Invalid index. In that case, you may find the comments on the answer to this question useful.
The summary is that, in order to get the data from an incoming message on a POP account, the message must be moved to a folder other than the inbox first. Have the rule move the message into another mailbox you’ve created, for example, “Incoming Triage”, and then have the script iterate over all messages in that mailbox instead of over the messages given to the handler. For example:
using terms from application "Mail"
on perform mail action with messages caughtMessages for rule catchingRule
--for POP messages, the rule must have already moved it into Incoming Triage
set caughtMessages to every message of mailbox "Incoming Triage"
repeat with caughtMessage in caughtMessages
tell caughtMessage
set mailSubject to subject
end tell
display dialog mailSubject
move caughtMessage to mailbox "INBOX" of account 1
end repeat
end perform mail action with messages
end using terms from
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With