Hammerspoon / hammerspoon

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

Open a menu in the application menu bar #2420

Open ebai101 opened 4 years ago

ebai101 commented 4 years ago

I'm trying to bind a hotkey to open the File menu. I'm coming from Keyboard Maestro, where there's a "Select or Show Menu Item" function that can just display a menu, and from there I can start typing the name of a menu item to select it. It doesn't seem like there's a way to do this in vanilla Hammerspoon, although I could be wrong - maybe there's an osascript-based solution?

latenitefilms commented 4 years ago

Does this do what you want?

hs.application:selectMenuItem(menuitem[, isRegex])

ebai101 commented 4 years ago

No, that doesn't work unfortunately. Here's an example - I want the code below to reveal the File menu in the frontmost app when I press F12:

hs.hotkey.bind({}, 'f12', function()
    hs.alert.show('showing the file menu')
    hs.application.frontmostApplication():selectMenuItem({'File'})
end)

I think this is the same quirk that I ran into with JXA (see here) where a simulated click on an item with no action doesn't actually do anything.

Let me know if I'm missing something. I'm on High Sierra 10.13.6 for what it's worth.

edit: that code works fine for me if I change the parameter list to a menu item that does have an action, like {'File', 'New Tab'} or similar (a new tab is created, if my browser is at the front)

latenitefilms commented 4 years ago

Ummm, that's strange - you're code is working fine for me with Safari, but I'm on Mojave 10.14.6.

latenitefilms commented 4 years ago

As a workaround, you could also use hs._asm.axuielement instead.