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

4.8k views Asked by At

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.

5

There are 5 answers

4
Michael On

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.

4
William Jockusch On

Renaming the project did it. Seems too extreme, though. I would still be interested in a less extreme solution.

0
Alexey Martemyanov On

Better late than never: after digging around I've found a working solution, even two!

You can set a menu image with a rendered text (you can use bold font!)

// render a string into image
let string = NSString(string: "Application Name")
let font = NSFont.boldSystemFont(ofSize: 13)
let size = string.size(withAttributes: [.font: font])
let image = NSImage(size: size)
image.lockFocus()
let rect = NSRect(origin: NSPoint(x: 0, y: 0.5), size: size)
string.draw(with: rect, options: [.usesLineFragmentOrigin], attributes: [.font: font])
image.unlockFocus()

appMenuItem.submenu?.title = " " // clear original App Name
appMenuItem.image = image // set the image instead

Alternatively you may change the whole Process Name including the Apple Menu title (thanks to this answer!)

// dynamically load ApplicationServices.GetCurrentProcess
typealias GetCurrentProcessType = @convention(c) (UnsafePointer<ProcessSerialNumber>) -> OSStatus
let getCurrentProcessSym = dlsym(UnsafeMutableRawPointer(bitPattern: -2), "GetCurrentProcess")!
let getCurrentProcess = unsafeBitCast(getCurrentProcessSym, to: GetCurrentProcessType.self)

// dynamically load ApplicationServices.CPSSetProcessName
typealias CPSSetProcessNameType = @convention(c) (UnsafePointer<ProcessSerialNumber>, UnsafePointer<Int8>) -> OSStatus
let cpsSetProcessNameSym = dlsym(UnsafeMutableRawPointer(bitPattern: -2), "CPSSetProcessName")!
let cpsSetProcessName = unsafeBitCast(cpsSetProcessNameSym, to: CPSSetProcessNameType.self)

// Get current process serial number
var psn = ProcessSerialNumber()
getCurrentProcess(&psn)
// Change Process Name
"Custom Title".withCString { cpsSetProcessName(&psn, $0) }
0
Trident On

There is actually a better way to address this issue. There will be a Main.strings file in your project which contains all the strings for MenuItems for the application, you can directly go there and update as per your requirements. This way we can also support localization with out code level changes.

0
askh On

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.