Selecting 2nd item of identical name in popup menu
Title
Selecting 2nd item of identical name in popup menu
What do you expect to happen when you run the script/macro?
Hi
Using Sonnox ListenHub. I'm trying to write a script that will select an ouput, either "Main" or "Alt" from the ListenHub icon in the Mac Status Bar.
Are you seeing an error?
What happens when you run this script?
The issue is that there are two separate " Main" options in the dropdown menu and I want to select the 2nd item.
How were you running this script?
I used a keyboard shortcut within the target app
How important is this issue to you?
4
Details
{ "inputExpected": "Hi\n\nUsing Sonnox ListenHub. I'm trying to write a script that will select an ouput, either \"Main\" or \"Alt\" from the ListenHub icon in the Mac Status Bar.", "inputIsError": false, "inputWhatHappens": "The issue is that there are two separate \" Main\" options in the dropdown menu and I want to select the 2nd item.", "inputHowRun": { "key": "-Mpfwh4RkPLb2LPwjePT", "title": "I used a keyboard shortcut within the target app" }, "inputImportance": 4, "inputTitle": "Selecting 2nd item of identical name in popup menu" }
Source
const AltOutput = " Alt"
const MainOutput = " Main"
function selectOutput(Output) {
sf.ui.app("com.sonnox.listenhub").getElement("AXExtrasMenuBar").popupMenuSelect({
menuPath: [Output],
});
}
selectOutput(MainOutput);
Links
User UID: Kf7km0JU15Q6NnRTk6HrX1PmHaw1
Feedback Key: sffeedback:Kf7km0JU15Q6NnRTk6HrX1PmHaw1:-N6rskLXigHJcJRItbci
- Christian Scheuer @chrscheuer2022-07-13 15:11:01.990Z
Try this:
const AltOutput = " Alt" const MainOutput = " Main" function selectOutput(outputName) { sf.ui.app("com.sonnox.listenhub").getElement("AXExtrasMenuBar").popupMenuSelect({ menuSelector: items => items.filter(i => i.path.length === 1 && i.path[0] === outputName)[1], }); } selectOutput(MainOutput);
Note: not tested as I don't have Sonnox ListenHub
Christian Scheuer @chrscheuer2022-07-13 15:13:19.541Z
The
menuSelector
callback presents you with a list of all items in the popup menu in theitems
array. Using the filter function I then filter to take all menu items matching the output name, and then I take the 2nd item ([1]
as arrays are zero-based).- AAlex Oldroyd @Alex_Oldroyd8
Thanks for your help as always, Christian. Unfortunately it is still selecting the first " Main" item...
I've attached a picture in case it might help
Christian Scheuer @chrscheuer2022-07-13 19:28:31.712Z
Try this instead:
const AltOutput = " Alt" const MainOutput = " Main" function selectOutput(outputName) { const items = sf.ui.app("com.sonnox.listenhub").getElement("AXExtrasMenuBar").popupMenuFetchAllItems().menuItems; const item = items.filter(i => i.path.length === 0 && i.path[0] === outputName)[1]; item.element.elementClick(); } selectOutput(MainOutput);
- AAlex Oldroyd @Alex_Oldroyd8
Thanks again, Christian. I'm getting the following error
14.07.2022 12:07:08.77 <info> [Backend]: JavaScript error with InnerException: null !! Command Error: Change ListenHub output [user:cl5jfx0z90000w710r2y5m0gl:cl5jo61gt0005za10ghv3z618]: TypeError: Cannot read property 'element' of undefined (Change ListenHub output line 7)
Christian Scheuer @chrscheuer2022-07-14 11:13:45.063Z
This indicates there's not in fact 2 menu items with that exact name.
I would guess one of the items has a different number of spaces in front.
You can debug what the actual items are by logging the
items
array:const AltOutput = " Alt" const MainOutput = " Main" function selectOutput(outputName) { const items = sf.ui.app("com.sonnox.listenhub").getElement("AXExtrasMenuBar").popupMenuFetchAllItems().menuItems; log(items); const item = items.filter(i => i.path.length === 0 && i.path[0] === outputName)[1]; item.element.elementClick(); } selectOutput(MainOutput);
- AAlex Oldroyd @Alex_Oldroyd8
Thanks, Christian. I'm sure this will likely work. Haven't been able to make it into the studio to test yet
- JIn reply toAlex_Oldroyd8⬆:Josh Morales @Josh_Morales
Did you ever get this working? Here's the log of items up to the mute, but it's still unclear to me how to select the inputs and outputs...
- JIn reply toAlex_Oldroyd8⬆:Josh Morales @Josh_Morales
"Path": [ "Open ListenHub" ], "Names": [ "Open ListenHub" ], "Delimiter": " -> ", "Element": "{\"title\":\"Open ListenHub\",\"role\":\"AXMenuItem\",\"fullRole\":\"AXMenuItem\"}" }, { "Path": [ "Preferences" ], "Names": [ "Preferences" ], "Delimiter": " -> ", "Element": "{\"title\":\"Preferences\",\"role\":\"AXMenuItem\",\"fullRole\":\"AXMenuItem\"}" }, { "Path": [ "Plug-In Manager" ], "Names": [ "Plug-In Manager" ], "Delimiter": " -> ", "Element": "{\"title\":\"Plug-In Manager\",\"role\":\"AXMenuItem\",\"fullRole\":\"AXMenuItem\"}" }, { "Path": [ "Plug-In Buffer Size", "32 samples / 0.7 ms" ], "Names": [ "Plug-In Buffer Size", "32 samples / 0.7 ms" ], "Delimiter": " -> ", "Element": "{\"title\":\"32 samples / 0.7 ms\",\"role\":\"AXMenuItem\",\"fullRole\":\"AXMenuItem\"}" }, { "Path": [ "Plug-In Buffer Size", "64 samples / 1.3 ms" ], "Names": [ "Plug-In Buffer Size", "64 samples / 1.3 ms" ], "Delimiter": " -> ", "Element": "{\"title\":\"64 samples / 1.3 ms\",\"role\":\"AXMenuItem\",\"fullRole\":\"AXMenuItem\"}" }, { "Path": [ "Plug-In Buffer Size", "128 samples / 2.7 ms" ], "Names": [ "Plug-In Buffer Size", "128 samples / 2.7 ms" ], "Delimiter": " -> ", "Element": "{\"title\":\"128 samples / 2.7 ms\",\"role\":\"AXMenuItem\",\"fullRole\":\"AXMenuItem\"}" }, { "Path": [ "Plug-In Buffer Size", "256 samples / 5.3 ms" ], "Names": [ "Plug-In Buffer Size", "256 samples / 5.3 ms" ], "Delimiter": " -> ", "Element": "{\"title\":\"256 samples / 5.3 ms\",\"role\":\"AXMenuItem\",\"fullRole\":\"AXMenuItem\"}" }, { "Path": [ "Plug-In Buffer Size", "512 samples / 10.7 ms" ], "Names": [ "Plug-In Buffer Size", "512 samples / 10.7 ms" ], "Delimiter": " -> ", "Element": "{\"title\":\"512 samples / 10.7 ms\",\"role\":\"AXMenuItem\",\"fullRole\":\"AXMenuItem\"}" }, { "Path": [ "Plug-In Buffer Size", "1024 samples / 21.3 ms" ], "Names": [ "Plug-In Buffer Size", "1024 samples / 21.3 ms" ], "Delimiter": " -> ", "Element": "{\"title\":\"1024 samples / 21.3 ms\",\"role\":\"AXMenuItem\",\"fullRole\":\"AXMenuItem\"}" }, { "Path": [ "[Sample Rate: 48000]" ], "Names": [ "[Sample Rate: 48000]" ], "Delimiter": " -> ", "Element": "{\"title\":\"[Sample Rate: 48000]\",\"role\":\"AXMenuItem\",\"fullRole\":\"AXMenuItem\"}" }, { "Path": [ "Input Source" ], "Names": [ "Input Source" ], "Delimiter": " -> ", "Element": "{\"title\":\"Input Source\",\"role\":\"AXMenuItem\",\"fullRole\":\"AXMenuItem\"}" }, { "Path": [ " System" ], "Names": [ " System" ], "Delimiter": " -> ", "Element": "{\"title\":\" System\",\"role\":\"AXMenuItem\",\"fullRole\":\"AXMenuItem\"}" }, { "Path": [ " Main" ], "Names": [ " Main" ], "Delimiter": " -> ", "Element": "{\"title\":\" Main\",\"role\":\"AXMenuItem\",\"fullRole\":\"AXMenuItem\"}" }, { "Path": [ " Reference" ], "Names": [ " Reference" ], "Delimiter": " -> ", "Element": "{\"title\":\" Reference\",\"role\":\"AXMenuItem\",\"fullRole\":\"AXMenuItem\"}" }, { "Path": [ "Output" ], "Names": [ "Output" ], "Delimiter": " -> ", "Element": "{\"title\":\"Output\",\"role\":\"AXMenuItem\",\"fullRole\":\"AXMenuItem\"}" }, { "Path": [ " Main" ], "Names": [ " Main" ], "Delimiter": " -> ", "Element": "{\"title\":\" Main\",\"role\":\"AXMenuItem\",\"fullRole\":\"AXMenuItem\"}" }, { "Path": [ " Alt" ], "Names": [ " Alt" ], "Delimiter": " -> ", "Element": "{\"title\":\" Alt\",\"role\":\"AXMenuItem\",\"fullRole\":\"AXMenuItem\"}" }, { "Path": [ "Volume" ], "Names": [ "Volume" ], "Delimiter": " -> ", "Element": "{\"title\":\"Volume\",\"role\":\"AXMenuItem\",\"fullRole\":\"AXMenuItem\"}" }, { "Path": [ " Loud" ], "Names": [ " Loud" ], "Delimiter": " -> ", "Element": "{\"title\":\" Loud\",\"role\":\"AXMenuItem\",\"fullRole\":\"AXMenuItem\"}" }, { "Path": [ " Normal" ], "Names": [ " Normal" ], "Delimiter": " -> ", "Element": "{\"title\":\" Normal\",\"role\":\"AXMenuItem\",\"fullRole\":\"AXMenuItem\"}" }, { "Path": [ " Quiet" ], "Names": [ " Quiet" ], "Delimiter": " -> ", "Element": "{\"title\":\" Quiet\",\"role\":\"AXMenuItem\",\"fullRole\":\"AXMenuItem\"}" }, { "Path": [ " Soft" ], "Names": [ " Soft" ], "Delimiter": " -> ", "Element": "{\"title\":\" Soft\",\"role\":\"AXMenuItem\",\"fullRole\":\"AXMenuItem\"}" }, { "Path": [ " Dim" ], "Names": [ " Dim" ], "Delimiter": " -> ", "Element": "{\"title\":\" Dim\",\"role\":\"AXMenuItem\",\"fullRole\":\"AXMenuItem\"}" }, { "Path": [ " Mute" ], "Names": [ " Mute" ], "Delimiter": " -> ", "Element": "{\"title\":\" Mute\",\"role\":\"AXMenuItem\",\"fullRole\":\"AXMenuItem\"}" }, preformatted text
- JJosh Morales @Josh_Morales
The below script works for Open ListenHub or Preferences. I can't get it to work with any other menu items though...:
sf.ui.app("com.sonnox.listenhub").getElement("AXExtrasMenuBar").popupMenuSelect({ menuPath: ['XXX'], });```
- AAlex Oldroyd @Alex_Oldroyd8
Yeh this works for me for a few of the items...