Search code examples
javascriptapplescriptjavascript-automationapple-mail

Mail Rules with javascript instead of applescript


In a Mac mail rule, I am trying to run a javascript instead of an applescript. This has been asked in Mail Rules using JavaScript for Automation instead of AppleScript but the answer is not working for me!

I have tried to simplify the code as much as I can. So, the following applescript works fine:

on perform mail action with messages theMessages 
    say "running!"
end perform mail action with messages

but the equivalent javascript is not working.

function performMailActionWithMessages(messages) {
    app = Application.currentApplication()
    app.includeStandardAdditions = true
    app.say ("running")
}

Edit Here are my rule parameters

mail2.scpt

rule configuration


Solution

  • I do it without getting app. Try something like…

    // ObjC.import('Cocoa')
    ObjC.import('stdlib')
    var program="/usr/bin/say"
    function performMailActionWithMessages(messages) {
        say("running")
    }
    function say(what){
        var command = program + " '"+what+"'"
        console.log(command)
        $.system(command)
    }
    

    I’m not sure you need cocoa. You probably need stdlib.