The value stored under the NSApplicationName
key of the result of [[NSWorkspace sharedWorkspace] activeApplication]
is not the always the name the user knows the application by. Worse, it’s not always the same as the name for the application that other APIs expect or return. Even fullPathForApplication:
in NSWorkspace sometimes won’t recognize it!
The problem stems from the fact that there are at least five application names floating around, at least in concept: (1) the file name the Finder sees, which in the case of an application package is the package (bundle) name; (2) the name of the executable inside the package, (3) the long name used in many places for display purposes only; (4) the short name used as the application menu title and in a few other places where a long name won’t fit for display purposes; and (5) the process name of a running application. They aren’t always the same, especially in Microsoft and Adobe products.
–From an informative message by Bill Cheeseman.
So instead of relying on NSApplicationName
I now use -[[NSFileManager defaultManager] displayNameAtPath:]
then strip off the filename extension. This should give exactly the filename the user sees. Every time.
NSDictionary *appInfo = [[NSWorkspace sharedWorkspace] activeApplication];
NSString *appPath = [appInfo objectForKey:@"NSApplicationPath"];
NSString *name = [[[NSFileManager defaultManager] displayNameAtPath:appPath] stringByDeletingPathExtension];
And of course, you really should be using bundle identifiers, instead of names, to identify an application. Unfortunately, a very few applications are not bundles. (For example, Microsoft stuff prior to Office 2008), so it might be necessary to fall back on using a name to locate them in a path-independent way.
Creating a custom CFBundleName
in an application’s info.plist
file seems to confuse NSApplicationName
. For this reason I don’t think setting it is a good idea.
UPDATE 2010-01-20: See also, Technical Q&A QA1544: Obtaining the localized application name in Cocoa