user83319
user83319

Reputation: 1

Processing incoming messages in Applescript via mail rule in Mavericks

I have a simple auto-reply script, based on examples I have seen here and elsewhere, that is intended to run from a spam-filtering Mail rule. Here's the gist of it:

on perform_mail_action(theData)
    tell application "Mail"
        set theMessages to |selectedMessages| of theData
        repeat with theIncomingMessage in theMessages
            set theOriginalSender to sender of theIncomingMessage
            set theOriginalSubject to subject of theIncomingMessage
            set theOriginalContent to content of theIncomingMessage
            set t to ""
            set t to t & "This is an automated response to your message below..."
            set t to t & return & return & "===============================================" & return & return
            set t to t & "Subject:" & theOriginalSubject & return & return & theOriginalContent
            set theReply to make new outgoing message
            tell theReply
                make new to recipient at beginning of to recipients ¬
                    with properties {address:theOriginalSender}
                set sender to "[email protected]"
                set subject to "RE: your message"
                set content to t
                send
            end tell
            set mailbox of theIncomingMessage to mailbox "Trash"
        end repeat
    end tell
end perform_mail_action

I know the rule that invokes this script is firing, because it plays a sound. But nothing is sent, and the incoming message remains in my inbox.

If I remove the first and last lines, and replace the third line with

    set theMessages to messages of mailbox "test" of account "iCloud"

then it successfully processes all messages in the "test" folder (it sends the reply and deletes the incoming message).

The rule does nothing but play a sound and invoke the script. As noted above, the sound plays. What am I missing?

Upvotes: 0

Views: 2808

Answers (2)

jomoz
jomoz

Reputation: 29

To elaborate on Michele Percich's response, despite information available online (e.g., Introduction to Scripting Mail) Mail doesn't seem to run a handler of the form:

on perform_mail_action(theData)

But it does run the handler if it's written in this form:

using terms from application "Mail"
   on perform mail action with messages theSelectedMessages for rule theRule

Upvotes: 0

Michele Percich
Michele Percich

Reputation: 1892

Replace the first 3 lines (including set theMessage...) with:

using terms from application "Mail"
   on perform mail action with messages theMessages for rule theRule
      tell application "Mail"

Replace the last line with:

   end perform mail action with messages
end using terms from

Upvotes: 0

Related Questions