0

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.

4
  • Do you need to do this via command line? Commented May 27, 2017 at 18:39
  • I ask because I have a workaround that works in Script Editor. Commented May 27, 2017 at 18:39
  • I can see your deleted answer, and it's an interesting workaround... but no, I can run any code I want, I merely prefer JS and would like to know how to do it "purely". Commented May 27, 2017 at 18:44
  • doesn't look like there's a 'pure' solution. Looks like a bug in JXA. Commented May 27, 2017 at 18:48

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

This workaround works for me, but only works (as far as I can tell) in Script Editor:

var app = Application.currentApplication();
app.includeStandardAdditions = true;

app.doShellScript('osascript -e "tell application \\"loginwindow\\" to «event aevtrlgo»"');

1 Comment

Now, if you can find a way to get this to work in the command line, I'd sure be curious to find out how

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.