How do you display NSMenu programmatically?

I wrote a small application that lives in NSStatusBar. I want to have a global shortcut that, when clicked, displays the contents of the menu bar, just like the behavior of a spotlight. I have added a global key shortcut to my application, but I cannot get the meun panel to display. How can i do this? I tried using "popUpContextMenu", but this method displays the menu in the lower left corner, I want the menu to open right below the NSStatusBar menu icon.

+7
cocoa macos
source share
2 answers

You can do this, the apple provides a method for it "popUpStatusItemMenu"

+5
source share

I don’t think there is a supported way to do this without using the Accessibility framework to simulate a click on your status element. Regardless, it is probably not advisable to abuse the menu in this way.

The Spotlight menu bar item does not use NSMenu, it uses its own window / view. You can consider this route if you have some kind of custom view to display.

0
source share

All Articles