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!

Daniel Morrison
  • 311
  • 1
  • 4

1 Answers1

14

I finally figured it out:

function performMailActionWithMessages(messages) {
  messages.forEach( function(message) {  // if you want to iterate
  })
};
Daniel Morrison
  • 311
  • 1
  • 4
  • 1
    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) – GusOst Jan 19 '16 at 15:37
  • 1
    I never found anything great, just kept trying until I got something working. I haven't gone back to it since. – Daniel Morrison Jan 20 '16 at 15:58
  • 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: https://stackoverflow.com/questions/49117223/mail-rules-with-javascript-instead-of-applescript/49117532?noredirect=1#comment85248292_49117532 – Ben Quan Mar 06 '18 at 00:41
  • 1
    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)`. – BallpointBen Oct 15 '20 at 15:14