How i can get the Application Menu in Cocoa

Lothar picture Lothar · Aug 9, 2010 · Viewed 13.8k times · Source

How can I get the NSMenu or NSMenuItem for the application menu (the one in the menu bar next to the apple menu). It seems to be automatically created and independent from the NSMenu I set via NSApplication setMainMenu.

By the way: I'm building my complete application without Xcode, so please no InterfaceBuilder tips.

PS: MacOSX 10.5

Answer

Jarret Hardie picture Jarret Hardie · Aug 9, 2010

Without IB, you can access the menu using the NSApplication's mainMenu:

NSMenu *mainMenu = [[NSApplication sharedApplication] mainMenu];
NSMenu *appMenu = [[mainMenu itemAtIndex:0] submenu];

for (NSMenuItem *item in [appMenu itemArray]) {
    NSLog(@"%@", [item title]);
}