Search code examples
macospathbundle-identifiernsrunningapplication

Access any apps bundle identifier


For my app, I have the user choose the path for a specific application of their computer. From that path (i.e. /Applications/Itunes.app) I would like to get the bundle identifier, and create an NSRunningApplication instance from it ([NSRunningApplication runningApplicationsWithBundleIdentifier:(nonnull NSString *)]; How would I go about getting this bundle identifier?


Solution

  • Use the core foundation function CFBundleGetIdentifier

    Create the bundle reference from the path to the bundle with CFURLCreateFromFileSystemRepresentation and call CFBundleCreate

    url = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, bundlePath, bundleLength, true);
    bundle = CFBundleCreate(kCFAllocatorDefault, url);
    
    CFStringRef identifier = CFBundleGetIdentifier(bundle)