Mail Rules with javascript instead of applescript
Asked Answered
C

1

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

Cocainism answered 5/3, 2018 at 18:32 Comment(0)
H
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.

Honduras answered 5/3, 2018 at 18:53 Comment(11)
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.Cocainism
I do this kind of code all the time. I use function performMailActionWithMessages(messages, why) { I sometimes use the second parameter.Honduras
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"Cocainism
it runs for me. Exactly what I shared. And ObjC.import('Cocoa') isn’t needed.Honduras
Are you running High Sierra or something older?Honduras
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.Honduras
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??Cocainism
Let us continue this discussion in chat.Honduras
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?Honduras
Glad we could figure out the problem in chat!Honduras
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!Cocainism

© 2022 - 2024 — McMap. All rights reserved.