Hammerspoon / hammerspoon

Staggeringly powerful macOS desktop automation with Lua
http://www.hammerspoon.org
MIT License
12.08k stars 582 forks source link

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

Open muescha opened 2 years ago

muescha commented 2 years ago

i have this problem:

Question:

EDIT: additional information:

latenitefilms commented 2 years ago

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

hs.application.get("Safari"):selectMenuItem({"Safari", "About Safari"})
muescha commented 2 years ago

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?

latenitefilms commented 2 years ago

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

muescha commented 2 years ago

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

latenitefilms commented 2 years ago

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