Mac OS X Reference Library Apple Developer Connection spyglass button

Obtaining the localized application name in Cocoa

Q: How can I get the localized name of my Cocoa application?

A: There are several different versions of the application name, all of which are readily available as an NSString. Depending on what you need, you can use one of the following five approaches:

WARNING: Please note that application packages can be renamed by users in the Finder. If you need to get the user-modified application name, you should use the NSFileManager approach detailed in Listing 5.

The application's canonical "short" name is defined by CFBundleName and is used by the menu and about box.

Listing 1: Getting the application short name from the main bundle.

 NSString *appName = 
        [[[NSBundle mainBundle] localizedInfoDictionary] objectForKey:(NSString *)kCFBundleNameKey];

The application's canonical "display name" is typically longer and is used by the Finder to display the bundle.

Listing 2: Obtaining the application's display name.

 NSString *appName = 
        [[[NSBundle mainBundle] localizedInfoDictionary] objectForKey:@"CFBundleDisplayName"];

Note that it is possible for both of these preceding methods to return nil. If they do, a viable approach is to do what the default about box does and simply use the process name itself.

Listing 3: Acquiring the process name.

 NSString *appName = [[NSProcessInfo processInfo] processName];

Starting with Mac OS X 10.6, you can now use NSRunningApplication to get the localized application name.

Listing 4: Obtaining the localized application name using NSRunningApplication.

 NSString *appName = [[NSRunningApplication currentApplication] localizedName];

Probably the most general approach, however, is to ask the NSFileManager. The method displayNameAtPath: will return the localized display name if it exists, fall back on the bundle name if it does not, and will even reflect a renaming of the application by the user.

Listing 5: Obtaining a general display name from NSFileManager.

 NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
    NSString *appName = [[NSFileManager defaultManager] displayNameAtPath: bundlePath];

For more information, see File System Overview: Display Names and the section "Localizing Your Application Name" in Internationalization Programming Topics: Localizing Pathnames.

Document Revision History

Date Notes
2009-10-27 Fixed typo in code listing 5. Added information about NSRunningApplication for Snow Leopard (code listing 4)
2007-09-21 New document that describes how to obtain several versions of the application name in Cocoa.

Posted: 2009-10-27

Did this document help you? Yes It's good, but... Not helpful...