tags:

views:

42

answers:

2

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

A: 

Making a Cocoa app without Xcode or IB sounds masochistic to me, but to each his own... Try this: [[[NSApp mainMenu] itemAtIndex: 0] submenu].

JWWalker
Well i'm writting a language binding and want to stay platform portable. But your solution does not work. The menu item with the bundle name that is autogenerated is not part of the mainMenu.
Lothar
It worked for me, but I tried it in a normal app with Xcode and IB.
JWWalker
Is it possible that you tried it too early in the app startup process? I tried it in the `applicationDidFinishLaunching:` delegate method.
JWWalker
No i checked it while pressing a button inside the window.I can only guess that there is some magic with the names of NSMenu and NSMenuItem, Apple is using some magic with names or tags (like the addition of menu items to menus with specific names) but i think i tried the obvious ones.
Lothar
You can name the first menu in IB whatever you want and it will have the correct name at run time. Are you creating that menu yourself, or are you only creating the File, Edit, etc. menus?
Peter Hosey
I only create File and Edit. The application menu or maybe just the NSMenuItem in the menu bar is created automagically by Cocoa - well otherwise i would have a pointer to it.
Lothar
In the templates that come with Xcode, there is an application menu in the nib's main menu along with the File and Edit menus. You should create the application menu yourself.
Peter Hosey
A: 

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]);
}
Jarret Hardie
No this does not work. numberOfItems in NSMenu also gives me 1 when i clearly see the "TestMenu File" items in the menubar.
Lothar
Ah, I appear to have made an assumption about your project structure. Did you create the project in XCode and then remove the nib/xib completely? Or are you totally going from scratch? This blog entry talks about what you're experiencing with the mainMenu and how you might be able to work your project in such a way to get the application menu: http://lapcatsoftware.com/blog/2007/05/16/working-without-a-nib-part-1/
Jarret Hardie
Thanks thats it. And yes i'm completely nibless, in fact during development i even run as a normal ELF a.out program without a Bundle structure. It works fine execept for the menu.
Lothar