Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attach event listeners in OS X JavaScript for Automation (JXA)

How is it possible to listen for events in OS X JavaScript for automation.

In the scripting Library for the Messages application there is a list of event handlers, such as messageSent and messageReceived. However, I cannot figure out how to use them. Trying to pass a function yields an error, and trying to set these variables to new functions causes the REPL to hang.

What is the proper way to set up these event handlers?

like image 208
wtf_are_my_initials Avatar asked Oct 31 '22 14:10

wtf_are_my_initials


1 Answers

You would create a script that you then select from the AppleScript handler menu in Preferences > General. Use the built-in Speak Events.applescript file as your guide and be aware that you have to override every single handler for it to execute properly.

Note: even with Standard Additions included, you still cannot call scripting addition commands like say() likely due to how they've implemented this layer on top of the scripting engine, making even an appropriate call using currentApplication() look as if it is being sent cross-application.

Here is a template:

Messages = Application.currentApplication()
Messages.includeStandardAdditions = true

function messageSent(m, e) {
}

function messageReceived(m, e) {
}

function chatRoomMessageReceived(e) {
}

function activeChatMessageReceived(m, e) {
}

function addressedMessageReceived(m, b, c, e) {
}

function receivedTextInvitation(e) {
}

function receivedAudioInvitation(m, b, c, e) {
}

function receivedVideoInvitation(m, b, c, e) {
}

function receivedLocalScreenSharingInvitation(b, c, e) {
}

function buddyAuthorizationRequested(e) {
}

function addressedChatRoomMessageReceived(e) {
}

function receivedRemoteScreenSharingInvitation(e) {
}

function loginFinished(e) {
}

function logoutFinished(e) {
}

function buddyBecameAvailable(e) {
}

function buddyBecameUnavailable(e) {
}

function receivedFileTransferInvitation(e) {
}

function avChatStarted(e) {
}

function avChatEnded(e) {
}

function completedFileTransfer(e) {
}
like image 144
chrissphinx Avatar answered Nov 08 '22 06:11

chrissphinx