NSMenu programmatically select item

cocoa, nsmenu, nsmenuitem, nspopupbutton, objective-c

Solution

Use the `NSMenu` method `- (void)performActionForItemAtIndex:(NSInteger)index`

NSUInteger idx = [[[menuItem menu] itemArray] indexOfObject:menuItem];
[[menuItem menu] performActionForItemAtIndex:idx];

Problem

I'm writing a plugin for application - custom keyboard shortcut. I can traverse through its views. I need to open popup menu, select item in it, then open its submenu and select some item in submenu. For now I'm only able to open top popup menu by sending `performClick:` to related `NSPopUpButton` element. How can I programmatically select item in menu and open its submenu? I've tried: - call `selectItem:` on the `NSPopUpButton` (and related `NSMenu`). No luck and I see a notion in the doc: "Note that while a menu is tracking user input, programmatic changes to the menu such as adding, removing, or changing items on the menu is not reflected" - send keyboard events (using this answer). No luck - may be because I'm holding some keys at the moment of sending those events - to find any info on how to do it via Accessibility API, but I just can't find anything on how to use it on current Application (or even on any other application, but with Objective-C)

Original source

Related problems