Those stinkin’ NewApplication menu items.
I feel pretty bad.. it’s been a long time since I’ve posted. I had such good intentions about doing this regularly, didn’t quite keep that up. To allay my guilt, here’s something (I think is) useful.
Why can’t I just have some kind of substitution token for my App name in my menus? It would be so nice if this was just handled for you. Or even just use preprocessor macros. Or something so I don’t have to build things programmatically.
I’m working on a project which produces various targets, why would I want a separate MainMenu.nib for each target, when except for the App name they are the same (or at least nearly so).
So here’s what I did in my AppController’s AwakeFromNib:
NSEnumerator *menuEnumerator = [[[NSApp mainMenu] itemArray] objectEnumerator];
id menuItem;
while (menuItem = [menuEnumerator nextObject]) {
if ([menuItem hasSubmenu]) {
NSEnumerator *subMenuEnumerator = [[[menuItem submenu] itemArray] objectEnumerator];
id subMenuItem;
while (subMenuItem = [subMenuEnumerator nextObject]) {
NSString *menuTitle = [subMenuItem title];
[subMenuItem setTitle:[[menuTitle
componentsSeparatedByString:@"NewApplication"]
componentsJoinedByString:[[NSProcessInfo processInfo] processName]]];
}
}
}
Maybe you’ll find a use for it somewhere.

Very cool use of componentsSeparatedByString: never really though of using it that way!
Comment by Rick Steele — January 22, 2007 @ 9:37 am
Thanks.
It just looked like the simplest way to do that, kinda harkens back to using text item delimiters in AppleScript.
Comment by ganyard — January 22, 2007 @ 9:44 am
Nice, I saw your post on the Cocoa Students list and now you have published the answer. I think this will come in handy even though I only have a single target (I tend to change the application’s name frequently). Thanks.
Comment by Kyle Killion — January 26, 2007 @ 2:31 pm