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

How can I listen to events in OS X JavaScript for automation.

The Message Scripting Library application has a list of event handlers, such as messageSentand messageReceived. However, I cannot figure out how to use them. Trying to pass a function gives an error, and trying to set these variables to new functions makes the REPL hang.

What is the correct way to configure these event handlers?

+4
source share
2 answers

script, AppleScript Preferences > General. Speak Events.applescript , .

: - , say(), - , , currentApplication() , -.

:

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) {
}
+1

Ive , OS X 10.11.3 Beta (15D9c).

ObjC.import('stdlib')
var yell = "say"

function run() {
    //  messageReceived("test",{from:Application("Messages").services.byName("SMS").buddies[0]})
}

function execute(program, args) {
    var command = program + " " + args.map(q).join("")
    console.log(command)
    $.system(command)

    function q(s) {
        return " '" + s.replace("'", "'\\''") + "' "
    }
}

function messageReceived(text, who) {
    execute(yell, [who.from.name(), text])
}

function loginFinished(service) {
    execute(yell, ["login", service.for.name()])
}

function logoutFinished(service) { // doesn't work!
    execute(yell, ["Logout", service.for.name()])
}

function keys(o) { // debugging tool
    return Object.keys(o).join(",")
}
receivedTextInvitation = addressedMessageReceived = chatRoomMessageReceived = activeChatMessageReceived = messageReceived

function messageSent() {}

function receivedAudioInvitation() {}

function receivedVideoInvitation() {}

function receivedLocalScreenSharingInvitation() {}

function buddyAuthorizationRequested() {}

function addressedChatRoomMessageReceived() {}

function receivedRemoteScreenSharingInvitation() {}

function logoutFinished() {}

function buddyBecameAvailable() {}

function buddyBecameUnavailable() {}

function receivedFileTransferInvitation() {}

function avChatStarted() {}

function avChatEnded() {}

function completedFileTransfer() {}
+1

All Articles