hammerspoon
hammerspoon copied to clipboard
selectMenuItem - how can i call it with english and without localized name
i have this problem:
- i try to call a menu item. i know the exact english identifier (
Move tab to new window) - but the english identifier not works with
selectMenuItem - i have to dig all the way down to find it in my language with:
print(hs.inspect.inspect(app:getMenuItems())) - with this the script is not anymore universal and everyone need to fix it to the localized string
Question:
- is there a way to select a menu item with
selectMenuItemin english? - or some automated way to find the localized string from english identifier?
EDIT: additional information:
- i have my macOS system in german
You can definitely just use plain English as it appears in the menu itself. For example:
hs.application.get("Safari"):selectMenuItem({"Safari", "About Safari"})
sorry - that not works - and that is my problem as described in the issue
my macOS system language is german:
the call i need for my example is:
app:selectMenuItem("Tab in ein neues Fenster verschieben")
and for your example i need:
hs.application.get("Safari"):selectMenuItem({"Safari", "Über Safari"})

Please double check: did you have the same german menu in your Safari and you can use the english identifiers for selectMenuItem?
Oh, I see what you mean now - you want to share some Lua code with others, and you want it to work on any language.
This is more tricky.
One way to do it would be using "position" data instead - i.e. select menu item 1, sub-menu item 1, etc. You could use hs.axuielement for this. Of course, the downside is that if the menu structure changes between updates, your code breaks.
Another way would be to read the MainMenu.nib file data - but that'll require a lot of code. You can see how we do this in CommandPost here: https://github.com/CommandPost/CommandPost/blob/develop/src/extensions/cp/app/menu.lua
thats tricky - is there any debugger for nib-files available just to look into it?
You could download CommandPost and have a play with cp.nib.archiver.