1

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

Ben Quan
  • 773
  • 11
  • 25

1 Answers1

1

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.

JakeCigar
  • 603
  • 6
  • 12
  • 1
    The main problem is not really what is being called inside the function. The inner part can be any miscellaneous code. The problem is that the function is not being fired by the mail rule. – Ben Quan Mar 05 '18 at 21:33
  • I do this kind of code all the time. I use function performMailActionWithMessages(messages, why) { I sometimes use the second parameter. – JakeCigar Mar 05 '18 at 21:37
  • I copied you code verbose to a mail.scpt file. I add a new mail rule, which makes a beep and fires the mail.sctp. I am able to listen to the beep but not the "running" – Ben Quan Mar 05 '18 at 23:47
  • it runs for me. Exactly what I shared. And ObjC.import('Cocoa') isn’t needed. – JakeCigar Mar 05 '18 at 23:59
  • Are you running High Sierra or something older? – JakeCigar Mar 06 '18 at 00:00
  • I suggest you use mail to change the color of the text for these messages. Just to make sure the rule is being used. I color the text red to prove it matched the rule. – JakeCigar Mar 06 '18 at 00:02
  • I added color purple action... replaced the whole mail2.scpt file with your code. I can listen the "ping" , the message turns purple, but I never listen tu "running". If I console.log, where would the text appear?? – Ben Quan Mar 06 '18 at 00:37
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/166282/discussion-between-jake-wolpert-and-ben-quan). – JakeCigar Mar 06 '18 at 00:41
  • It’s buried in one of the logs. I found it once. But never went back. Does the shell command "say Jake!" work for you? – JakeCigar Mar 06 '18 at 00:42
  • Glad we could figure out the problem in chat! – JakeCigar Mar 06 '18 at 02:11
  • Thanks a lot.... At the end my main problem was that I was not saving the script using script editor, so it was not being compiled. And the new script worked! – Ben Quan Mar 06 '18 at 02:18