curt
curt

Reputation: 4582

Getting at a Message from a Mail Rule in Applescript

As an excuse to learn Applescipt, I writing a script to be attached to a Mail rule to filter messages. I scrounged around and put together this much code:

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
            set theMessages to selection
            set theMessage to item 1 of theMessages
        end repeat
    end tell
end perform_mail_action

I would have thought the rule would only pass one message, but you never know so the repeat makes sense. I presume "selection" is a pointer to an item in theSelectedMessages. What seems strange is "set theMessage to item 1 of theMessages". I would have thought you would code "set theMessage to selection". I want to get to the body, "content" of the message to test for certain words.

Thanks for any help, Curt

Upvotes: 3

Views: 3886

Answers (2)

emale
emale

Reputation: 327

user866649s answer only works for me (macOS 10.13) when adding another line of code: using terms from application "Mail"

Following snippet displays the senders names of all messages that triggered the script:

using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule
        repeat with theMessage in theMessages
            display dialog (sender of theMessage as string)
        end repeat
    end perform mail action with messages
end using terms from

Upvotes: 3

user866649
user866649

Reputation:

There is an example rule script at /Library/Scripts/Mail Scripts/Rule Actions/Sample Rule Action Script.scpt. You will need to use the handler declaration that is set in Mail's scripting dictionary, i.e.

on perform mail action with messages theMessages for rule theRule

Upvotes: 3

Related Questions