Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set titles of items in my app's main menu?

I am trying to change the titles of some of the items in my Cocoa app's main menu. I have tried setting them both within IB and also programmatically from my app's applicationDidFinishLaunchingWithOptions: method. Either way, the title property of my NSMenuItem object does change. But none of my changes are reflected in the actual title of the item at the top of the screen when the app is running.

Can anyone explain what is going on? And how can I change this?

EDIT: The data structure is the default one that IB sets up:

NSApplication *app = [NSApplication sharedApplication];
NSMenu *mainMenu = [app mainMenu];
NSArray *itemArray = [mainMenu itemArray];
NSMenuItem *firstItem = [itemArray objectAtIndex: 0];
NSMenu *submenu = [firstItem submenu];

I have changed the title properties of both firstItem and subMenu to be my desired title. Yet the default one still shows.

like image 659
William Jockusch Avatar asked Feb 11 '11 03:02

William Jockusch


2 Answers

Instead of renaming project change "Bundle name" in your-application-Info.plist from ${PRODUCT_NAME}; to whatever you want; this change will be reflected in the title of Application menu item of Main Menu.

like image 96
askh Avatar answered Nov 14 '22 16:11

askh


Sometimes, this just does not work, because Cocoa doesn't like your title :-p That happens e.g. when the title you chose is the localized app name, but it wants to display the non-localized app name. A little trick can help...

NSMenu *menu = [[[NSApp mainMenu] itemAtIndex:0] submenu];
NSString *title = @"My app name";

// Append some invisible character to title :)
title = [title stringByAppendingString:@"\x1b"];

[menu setTitle:title];

Yeah, it's crazy, but that extra character at the end made all the difference. (You may also append just a SPACE, but then the menu item grows, which is probably not what you want.)

Tested on OS X 10.9.5.

Another thing:

You have to do all of this AFTER you have showed a window. Otherwise it just does not work. Furthermore if you do this procedure at app start, and then later when the window is shown do it again, it may not work too.

like image 6
Michael Avatar answered Nov 14 '22 17:11

Michael