javascriptapplescriptosascriptappleevents

How can I do AppleScript's «event aevtrlgo» in JavaScript?


On macOS, the osascript command line tool can be used to run AppleScript and JavaScript, both of which have access to the Apple Events API.

For most of the API, the translation is quite straightforward. E.g.

tell application "Terminal" to quit

translates to:

Application("Terminal").quit()

Moreover, in JS there is a commandsOfClass function that lists available actions:

osascript -l JavaScript -e 'Application("Terminal").commandsOfClass()'
# close, count, delete, doScript, duplicate, exists,
# getURL, make, move, open, print, quit, save

osascript -l JavaScript -e 'Application("System Events").commandsOfClass()'
# abortTransaction, attachActionTo, attachedScripts, beginTransaction, cancel, click,
# close, confirm, connect, count, decrement, delete, disconnect, doFolderAction,
# doScript, duplicate, editActionOf, enable, endTransaction, exists, increment,
# keyCode, keyDown, keyUp, keystroke, logOut, make, move, open, perform, pick,
# print, quit, removeActionFrom, restart, save, select, shutDown, sleep, start, stop

But AppleScript has some more obscure events, in particular the aevtrlgo ("really log out") one:

tell application "loginwindow" to «event aevtrlgo»

I can't even type the «» characters in the Terminal, so here's a copy-paste-able version:

printf 'tell application "loginwindow" to \xc2\xabevent aevtrlgo\xc2\xbb' | osascript

Now, how can I do this «event aevtrlgo» in JavaScript?

I've tried things like:

Application("loginwindow").rlgo();
Application("loginwindow").aevtrlgo();
Application("loginwindow").reallyLogOut();
Application("loginwindow").event("rlgo");
Application("loginwindow").event("aevtrlgo");
Application("loginwindow")["«event aevtrlgo»"]();

But I always get the exact same response:

execution error: Error on line 1: Error: Message not understood. (-1708)

Plus Application("loginwindow").commandsOfClass() yields an empty list.


Solution

  • JXA, like ScriptingBridge before it, is flawed in design and half-baked in implementation, so many operations that work perfectly in AppleScript don't work right/at all (and it's rarely clear why). In this case, JXA lacks public APIs for working with raw four-char codes, which means you're SOOL if JXA/the target app don't provide valid terminology for the AE types/events/etc you want to use.

    My standard advice: stick to AppleScript as it's the only officially supported option that knows how to speak Apple events right.