Reputation: 103
I want to call some JavaScript code using the osascript command. The code should stay running until it receives a chat message from Messages. Ultimately, I intend to execute this from node and receive back the chat message, sender, etc.
I am able to compile a 'stay-running' JavaScript app and run it via
osascript -l JavaScript JSiMessageReceiver.app
where JSiMessageReceiver.app is
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);
}
Of course, the messageReceived
handler is never called, because it is not yet associated with Messages. In AppleScript this is done via
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
How does this translate into JavaScript? I did not find any documentation on this. Any ideas?
Upvotes: 1
Views: 259
Reputation: 2817
Try this on JavaScript:
function messageReceived(theMessage, eventDescription) {
processMessage("message received", theMessage,
eventDescription.from, eventDescription.for);
}
See event description of messageReceived function in AppleScript Editor
- menu Window > Library
- Messages Event Handler Suite
.
The worked example from Jake Wolpert is there: Attach event listeners in OS X JavaScript for Automation (JXA)
Upvotes: 1