6

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?

foo
  • 3,171
  • 17
  • 18

2 Answers2

1

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) {
}
chrissphinx
  • 364
  • 3
  • 14
  • Maybe I misunderstand you. Are you saying you cannot run app.say('hello') ? Because you can – markhunte Jan 21 '15 at 00:08
  • No, you cannot. You can run that function from the `ScriptEditor`, but attempting to use `say()` in one of these callbacks will result in a privilege violation. – chrissphinx Apr 07 '15 at 01:18
1

I’ve gotten say to work in 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() {}
JakeCigar
  • 603
  • 6
  • 12
  • This should be easier: `app = Application.currentApplication()`, `app.includeStandardAdditions = true`, `app.say("hello")` – Besi Mar 17 '16 at 02:04
  • Should be. Yes. But it isn’t. Especially when you aren’t actually using the /usr/bin/say program. – JakeCigar Mar 17 '16 at 15:41
  • It worked, when: 1. script-file has format **[fileName].scpt** (create new File in Script Editor) 2. selected type **JavaScript** in `Script Editor` 3. save file in folder from `application Messages` - `menu Messages > Preferences` - `General Section` - `AppleScript handler dropdown menu` - `Open Scripts Folder` 4. select this file in `AppleScript handler dropdown menu` from p.3. – Dmitry Stolbov Mar 22 '16 at 05:11