Help! Translating AppleScript to JXA

Two steps forward, twenty steps backwards…

Can anyone help translate this AppleScript to JXA? I’m missing something that’s probably obvious…

tell application "System Events"
    tell process "Keyboard Maestro"
        get focused of text field 1 of scroll area 3 of splitter group 1 of group 1 of window "Keyboard Maestro Editor"
    end tell
end tell

Something like this, but this doesn’t work:

var _kmProc = Application("System Events").processes["Keyboard Maestro"];
return _kmProc.windows[0].groups[0].splitterGroups[0].scrollAreas[2].textField[0].focused;

Arg!!!

I figured it out. First of all, the application has to be activated, and you have to wait to make sure it actually finished activating:

var _km = Application("Keyboard Maestro");
_km.activate();
delay(.5); // probably longer than necessary...

var _kmProc = Application("System Events").processes["Keyboard Maestro"];
return _kmProc.windows[0].groups[0].splitterGroups[0].scrollAreas[2].textFields[0].focused();

That's great.

For future reference, I have found that with UI scripts it is best to test each UI element separately to make sure it works.

You may have noticed that Chris (@ccstone) uses hierarchical "tell blocks" in AppleScript.

By setting a var to each element, you can debug with Safari tools to view/inspect its properties.

Does this work for you? I thought the last time I tried this it didn't work. It works for JXA that uses the ObjC bridge?

It may depend on which OS build JMichaelTX is using.

Seems to be broken at the moment in Sierra. I’ve sent in one radar, but it takes a small flood of them to catch any attention.

Thanks. He's not running Sierra, I guarantee you And neither am I, for the same basic reasons.

it takes a small flood of them to catch any attention.

That really ticks me off. They were all gaga about JXA at the 2014 conference, and since then have almost pretended it doesn't exist. There isn't even a sub-forum dedicated to it on Apple's developer forum. And almost no questions, in any of the sub-forums.

Maybe we really are in the vast minority. Bleeding edge - I've spent most of my developer life there, so why should this be any different? :slight_smile:

I haven't tried it recently, but it has worked in the past.

What to you mean "JXA that uses the ObjC bridge"?
If you are referring to the automation objects, then yes.

See this video:
How to Debug JavaScript for Automation (JXA) Scripts with Safari Debugger - YouTube

1 Like