JXA: How to select a popup menu item (Solution)

NOTE: If you don't already know JXA, and/or don't know how to find the paths to UI elements, you can ignore this post. There's other topics on the subject on this forum, and a number of web articles available if you do a Google search.

Let's say you have a popup button like this:

image

When you click it, you get this menu:

image

And let's assume you know JXA, and have managed to obtain the path to the button, which would look something like this:

var myButton = _win.splitterGroups[0].splitterGroups[0].groups[0].popUpButtons[0];

You probably know you can get the value like this:

console.log(myButton.value());
// Only Once

But did you know you can easily set the value? You can, like this:

myButton.click();
myButton.menus[0].menuItems.byName("Once Every Second").click();

You're welcome. :stuck_out_tongue:

3 Likes

Thanks, Dan. Knew how to do that with AppleScript, but not JXA (at least not off the top of my head).

BTW, at least in AppleScript, you sometimes need to add a delay(n) after the first myButton.click(), particularly if it is a dynamic menu that is NOT created until the click. Usually a delay between 0.5 and 1.0 seconds will work.

1 Like

Thanks, Jim. I was surprised I didn't need to add a delay, but it's good to know that I might need to in the future.

I'm doing all this for a stupid control panerl that sets up all the soft keys and jog wheel stuff for my Contour Shuttle Pro V2, so I can save and restore settings.

There's different settings for each app I use it with, and if I upgrade an app, the stupid software won't bring across the settings, and worse, it won't let me see the old settings, even though it still knows about them! (If I restore the old app, the settings are still there.)

You can export and import, but only for the same version of the app. And the export file is binary, so I don't know how to change it.

So, I'm manually scraping something like a hundred settings for one app (that's probably exaggerated, but not by much), saving it as JSON, then applying the settings for the updated app by automating the UI. Joy. :roll_eyes: