Search code examples
macossafari-extension

macOS - Get path to currently running application


Question

Is it possible to determine the location a macOS app was launched from at runtime?

Context

I develop a Safari Extension, and in order for the extension to be enabled the application needs to be present /Applications/. Several users have tried to run the application directly from the DMG file and then complained that the extension doesn't appear in Safari's extension settings. I would like to detect this and alert them that they need to move the file.


Solution

  • You can use NSBundle, specifically the bundlePath property. The documentation says:

    The full pathname of the receiver’s bundle directory.

    And you can use it something like this:

    NSString* bundlePath;
    bundlePath = [[NSBundle mainBundle] bundlePath]
    
    NSLog(@"%@", bundlePath);
    

    There's also bundleURL if you want a NSURL to work with instead of a string.