Hammerspoon: Ability to select menu bar menu items

Created on 13 Jul 2015  路  8Comments  路  Source: Hammerspoon/hammerspoon

Hello,

I have some 3rd party menu bar items I would like to simulate a click on from HS, is this possible?

enhancement

Most helpful comment

@krasnovpro's AppleScript didn't work for me in macOS Sierra. This slightly modified version works though:

ignoring application responses
  tell application "System Events"
    click menu bar item 1 of menu bar 1 of application process "GrabBox"
  end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events"
  click menu item "Pause GrabBox" of menu 1 of menu bar item 1 of menu bar 1 of application process "GrabBox"
end tell

All 8 comments

I'm not aware of an API for getting the locations of the menubar items of 3rd party apps, so at the moment I'd say this isn't possible, unfortunately

Do you mean stuff like file, edit, view etc? Or the icons on the right side?

@HoloWolf I assume that @waynehoover means the icons on the right (NSMenuItem objects).

A way around it for now would be to use something like Keyboard Maestro and use a workflow that looks for the picture of the item you want and then you can have it do something with the data like move the mouse etc. I don't know if this functionality is in hammerspoon. This might be something useful to look into adding.

Yes I was talking about NSMenuItem objects. I'll look into the Keyboard Maestro option, thanks. Feel free to close this issue or keep it around if you feel it could be useful.

@waynehoover I'm going to close this issue and instead open a new one on the more generic option of detecting image content, which (if/when implemented) would allow for clicking on anything.

(FWIW, @HoloWolf is right, Keyboard Maestros image detection/clicking support works very well, I've used it myself).

I've use this code for clicking on menubar item:

hs.hotkey.bind('shift-alt', '5', function()
  hs.applescript([[
    tell application "System Events" to tell process "GrabBox"
      ignoring application responses
        click menu bar item 1 of menu bar 2
      end ignoring
    end tell
    do shell script "killall System\\ Events"
    delay 0.1
    tell application "System Events" to tell process "GrabBox"
      click menu bar item 1 of menu bar 2
      click menu item "Pause GrabBox" of menu 1 of menu bar item 1 of menu bar 2
    end tell
  ]])
end)

@krasnovpro's AppleScript didn't work for me in macOS Sierra. This slightly modified version works though:

ignoring application responses
  tell application "System Events"
    click menu bar item 1 of menu bar 1 of application process "GrabBox"
  end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events"
  click menu item "Pause GrabBox" of menu 1 of menu bar item 1 of menu bar 1 of application process "GrabBox"
end tell
Was this page helpful?
0 / 5 - 0 ratings

Related issues

jiahut picture jiahut  路  3Comments

specter78 picture specter78  路  3Comments

Pancia picture Pancia  路  4Comments

asmagill picture asmagill  路  4Comments

luckman212 picture luckman212  路  4Comments