Mail Rules using JavaScript for Automation instead of AppleScript
Asked Answered
C

1

7

I'm trying to build a Mail.app rule (on OS X Yosemite) using Javascript for Automation instead of AppleScript, but I'm stuck on the basics.

I see this code in AppleScript:

using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule
        # actual code here
    end perform mail action with messages
end using terms from

but I'm unclear how this translates to JavaScript.

Do I define a function? Set a callback? I'm unclear.

I see that there is the performMailActionWithMessages function, but I can't figure out how to get it working.

Any guidance is very much appreciated!

Contrivance answered 14/5, 2015 at 17:30 Comment(0)
C
14

I finally figured it out:

function performMailActionWithMessages(messages) {
  messages.forEach( function(message) {  // if you want to iterate
  })
};
Contrivance answered 14/5, 2015 at 20:1 Comment(4)
It's a shame you only have three upvotes. You got mine anyways. Where did you find documentation? Tried finding good sources for ages now. (For Javascript automation that is)Farmelo
I never found anything great, just kept trying until I got something working. I haven't gone back to it since.Contrivance
I am trying this code, but events are not firing. It works fine in applescript, but cannot make it work in javascript. Opened a followup question with more details: #49117723Tasse
The general rule for converting from AppleScript is to concatenateAndTitleCase the words. Since the action in AppleScript is on perform mail action with messages theMessages for rule theRule, in JS it becomes performMailActionWithMessages(theMessages, theRule).Lest

© 2022 - 2024 — McMap. All rights reserved.