hammerspoon icon indicating copy to clipboard operation
hammerspoon copied to clipboard

selectMenuItem - how can i call it with english and without localized name

Open muescha opened this issue 3 years ago • 5 comments

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 selectMenuItem in english?
  • or some automated way to find the localized string from english identifier?

EDIT: additional information:

  • i have my macOS system in german

muescha avatar Jul 05 '22 23:07 muescha

You can definitely just use plain English as it appears in the menu itself. For example:

hs.application.get("Safari"):selectMenuItem({"Safari", "About Safari"})

latenitefilms avatar Jul 05 '22 23:07 latenitefilms

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"})

Bildschirmfoto 2022-07-06 um 03 41 20

Please double check: did you have the same german menu in your Safari and you can use the english identifiers for selectMenuItem?

muescha avatar Jul 06 '22 01:07 muescha

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

latenitefilms avatar Jul 06 '22 01:07 latenitefilms

thats tricky - is there any debugger for nib-files available just to look into it?

muescha avatar Jul 06 '22 02:07 muescha

You could download CommandPost and have a play with cp.nib.archiver.

latenitefilms avatar Jul 06 '22 02:07 latenitefilms