Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to control menu items in dock context menus? #887

Closed
fkling opened this issue Apr 28, 2016 · 7 comments
Closed

Is it possible to control menu items in dock context menus? #887

fkling opened this issue Apr 28, 2016 · 7 comments

Comments

@fkling
Copy link

fkling commented Apr 28, 2016

I fear the answer is "no", but since I don't have any experience with OS X APIs I thought I'd ask.

Context: Pandora is implemented as an Adobe Air (or whatever it's called) application, and its top bar menu doesn't provide any way to control the app. The dock menu does though:

screen shot 2016-04-28 at 1 36 13 pm

I tried p:findMenuItem('Play') from the console, but that returns nil.

P.S.: I searched the issues before but couldn't find any related questions to this.

@fkling fkling changed the title Is it possible to controll menu items in dock context menus? Is it possible to control menu items in dock context menus? Apr 28, 2016
@asmagill
Copy link
Member

ATM, I don't think so (but chime in, anyone, if I'm wrong) but I have been giving thought to adding support in hs.menubar so that Hammerspoon could add a menu to its Dock icon; initial research into this indicates that Dock menus are built from the same type of NSMenu object as the menus found in the menu bar, so I suspect its just a matter of finding the right AXUIElement parent to search from.

Hopefully this may spur an idea for someone more familiar with the menu search code; otherwise, I'll make sure to look into it myself when adding Dock menu creation support, hopefully in May.

@asmagill
Copy link
Member

I'm less certain getting the menu list for Dock items will be quite that easy after looking at http://stackoverflow.com/questions/35821506/how-to-get-an-array-of-axmenuitems-from-axmenu/36115210#36115210... According to this and the Accessibility Inspector, it looks like the accessibility object for a Dock items menu doesn't actually exist until its visible...

However, I do have a module I've been working off and on with that deals with AXUIElements directly... it needs a lot of cleanup and documentation and I haven't figured out how it fits in with the core modules yet, but it looks like it might be a step in the right direction if you (or anyone following this) wants to take a look... the module code can be found at https://github.com/asmagill/hammerspoon_asm/tree/master/axuielement... with it, I was able to pop up the menu for the Dock item for the System Preferences with (haven't tried selecting an actual menu item yet, as I'd need to write a function, rather than just use the console, since the menu takes focus away from the console):

axuielement = require"hs._asm.axuielement"
SPDockItem = axuielement.applicationElement(hs.application("Dock")):elementSearch{title="System Preferences"}[1]
SPDockItem:doShowMenu()

screen shot 2016-04-28 at 11 47 39 pm

Looking at the code in the first link I gave above, I suspect that if this is followed with code to iterate through the children of SPDockItem (SPDockItem:children()), it should be possible to find a specific menu item and then act on it.

I'll clean up the AXUIElement module and document it next week... it may be time to figure out where it fits in with Hammerspoon after all...

@asmagill
Copy link
Member

Update: yeah, I think some variant of this needs to be added to Hammerspoon

axuielement = require"hs._asm.axuielement"
SPDockItem = axuielement.applicationElement(hs.application("Dock")):elementSearch{title="System Preferences"}[1]
SPDockItem:doShowMenu()
SPDockItem[1]:elementSearch{title="General"}[1]:doPress()

Note that elementSearch returns a list, which is why we add [1] after the elementSearch method call; the above causes the General System Preferences panel to be opened under Hammerspoon control.

I think something like this will do what you want if you change the two elementSearch parameters.

@asmagill
Copy link
Member

FYI, I've finally provided some documentation and a precompiled binary for the axuielement module. If you're interested, check out the Reference.md file in the repository folder.

@cmsj
Copy link
Member

cmsj commented Aug 26, 2016

@asmagill is axuielement in shape for inclusion? :D

@asmagill
Copy link
Member

Not yet... I realized recently that it leaks memory (not a lot, but it will add up)... I added code to allow direct comparison of the userdata objects and their use as keys in a table to help detect loops during traversal on the Lua side and didn't realize until recently that this code prevents the objects from ever being collected.

And it still needs better documentation, so while I hope to add it into core at some point, it won't happen until the next release, I'm afraid.

@cmsj
Copy link
Member

cmsj commented Apr 27, 2017

I'm going to close this out since it will either be solved or not by the fate of axuielement :)

@cmsj cmsj closed this as completed Apr 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants