2

I have an app that is supposed to have two different operating modes: let's call them mode 1 and mode 2.

I can only evaluate which mode the app should use after the app is launched (checking for license details). The easiest option is to provide 2 binaries and to decide which mode the app uses at compile time. But this is not what I wanted to do as the application's code only differs very little. I'd rather do some runtime magic.

What would be the best way to have the app change its title, app icon and menu labels depending on the mode to be used?

I experimented with the app's name displayed in the menu bar and while I could modify the title it didn't use the bold font but a regular one. [NSMenu title] is only a regular (non-attributed) string, so I'm thinking that AppKit is doing some magic when launching the app and pulling the name from the Info.plist (CFBundleName).

I also considered changing the application's Info.plist file but this would have 2 issues:

  1. the app needs to be restarted (not so bad)
  2. the app would need to be re-signed (very bad)

Looking forward to your suggestions!

3 Answers 3

8

By "title", I assume you mean the title of the Dock icon that's shown when you hover over it.

Of app icon, Dock icon title, and application menu title, the only one you can change properly is the app icon. Do [NSApp setApplicationIconImage:someNSImage].

The title of the Dock icon comes from the display name of the app bundle. The Dock picks this title and you basically have no control over it (other than renaming your app bundle).

The title of the application menu comes from the bundle name in the Info.plist file. It is read at a very low level in the frameworks with no opportunity to customize it.

1
  • By "title" I was referring to the application title as shown in the menu bar. Sorry for not being clear about it.
    – guitarflow
    Mar 3, 2015 at 21:29
1

I think you can easily do what you want by working around the NSApplicationMain function that is called in main.m, and then replacing what it did in applicationDidFinishLaunching in your App Delegate. Or maybe right in main.m.

There is a how-to of sorts here.

I think you can check your license, then launch different nibs for the different mainmenus based on that.

Looking into how to change an OSX App's icon programmatically, I think you have to grab the NSApplication's dockTile property of the class NSDockTile and set it's contentView. There are amazingly few mentions of NSDockTile on SO, but there it is...

In any event, forget the plist, but you can do this. Fundamentally you want to look beyond the NSApplicationDelegate protocol into NSApplication to see what you can screw with to achieve what you want, which is to programatically determine a lot of what's baked into Info.plist and NSApplicationMain(). I have done this for NSMainNib, but you'll have to do a few more. It is possible and I hope this gets you there. Good luck!

3
  • Hmm, not sure if I understand how loading two different NIBs would solve the naming issue. In any case, the name is obtained from the CFBundleName, right?
    – guitarflow
    Mar 3, 2015 at 10:02
  • Change the text of the menuitem that is upper left, and use a bold font...(?) Ken's answer is likely correct about the icon text likely being from the bundle, though. Otherwise I think you can get this done, and I'd do it with 2 menus. If you combine this and the other answer, by all means make your own. I sort of expect it. Too many details that you can't google for.
    – stevesliva
    Mar 3, 2015 at 17:18
  • Have you tried it yourself? It just doesn't work. You can change the menu title like so: NSMenu* theMenu = [[[NSApp mainMenu] itemAtIndex:0] submenu]; theMenu.title = @"Test"; but this will not have a bold font. NSMenu doesn't have an attributed title so there's no chance to change the font.
    – guitarflow
    Mar 3, 2015 at 21:34
1

You can change the title shown in the MenuBar using this code (Objective-C):

#import <AppKit/NSApplication.h>
#import <AppKit/NSMenu.h>

void setMenuProgramName(const char* newName)
{
    NSMenu* mainMenu = [[NSApplication sharedApplication] mainMenu];
    NSMenu* appMenu = [[mainMenu itemAtIndex:0] submenu];
    [appMenu setTitle:[[NSString alloc] initWithUTF8String:newName]];
}

What you can't change is the title displayed in the Dock when you hover over the icon.

For more information please refer to this question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.