如何在 OSA 事件处理程序中将 'using terms from ...' 转换为 Javascript

How to translate 'using terms from ...' to Javascript in an OSA Event Handler

我想使用 osascript 命令调用一些 JavaScript 代码。该代码应保持 运行ning,直到它收到来自消息的聊天消息。最终,我打算从节点执行此操作并接收聊天消息、发件人等。

我可以通过

编译 'stay-running' JavaScript 应用程序并 运行 它
osascript -l JavaScript JSiMessageReceiver.app

其中 JSiMessageReceiver.app 是

function run() {
    var Messages = Application('Messages');
    Messages.includeStandardAdditions = true;
    console.log('started');
}

function quit() { // should prevent app from quitting
    return true;  // according to Apple's developer release notes
}

function messageReceived(text) {
    console.log('message received: ' + text);
}

当然,messageReceived 处理程序永远不会被调用,因为它尚未与 Messages 相关联。在 AppleScript 中,这是通过

完成的
using terms from application "Messages"
    on message received theMessage from theBuddy for theChat
        processMessage("message received", theMessage, theBuddy, theChat)
    end message received
end using terms from

这如何转化为 JavaScript?我没有找到任何关于此的文档。 有什么想法吗?

在 JavaScript 上试试这个:

function messageReceived(theMessage, eventDescription) {
    processMessage("message received", theMessage,
                    eventDescription.from, eventDescription.for);
}

参见 AppleScript Editor - menu Window > Library - Messages Event Handler SuitemessageReceived 函数的事件描述。 来自 Jake Wolpert is there: Attach event listeners in OS X JavaScript for Automation (JXA)

的示例