Troy
Troy

Reputation: 21892

How can I programmatically determine if my OSX app was launched from the Applications dir?

I've built an app for OSX and built a .dmg installer for it. However, some users have trouble following the HUGE arrow in the background image telling them to drag the app to the /Applications folder. ;-)

I would be nice if it's possible to do the following:

  1. somehow detect if the app was launched from the /Applications dir
  2. if it wasn't, offer to automatically move the app from the dmg to the /Applications dir, for the user, and launch from there
  3. if #2 isn't something that the OS would allow, display an alert and then close down the app.

I can figure out #3 on my own, but I'm wondering if #1 and #2 are possible and how I might go about them.

Upvotes: 3

Views: 1429

Answers (3)

theMikeSwan
theMikeSwan

Reputation: 4729

Start here https://github.com/potionfactory/LetsMove/ and check out some of the forks if it isn't quite what you want. Pretty sure there is another library that does this as well but I can't find it right now

Upvotes: 1

Michael Domino
Michael Domino

Reputation: 419

For Unix in general, the first argument to the application (argv[0]), is a pointer to the absolute path to the application binary. Check the path for "/Applications". Also, if the app has been launched from the mounted dmg, the path will begin with "/Volumes" if it has been mounted by double-clicking it from the Finder.

You can see the difference using ps:

Launched from the mounted dmg (using Identity Finder.app as an example):

$ ps aux|grep Identity michael 52525 0.0 0.5 926072 80804 ?? U 12:14PM 0:02.92 /Volumes/Identity Finder/Identity Finder.app/Contents/MacOS/Identity Finder

Launched from /Applications:

$ ps aux|grep Identity michael 52562 0.0 0.7 976212 116736 ?? S 12:16PM 0:03.24 /Applications/Identity Finder.app/Contents/MacOS/Identity Finder

Upvotes: 0

Darren
Darren

Reputation: 25619

[NSBundle mainBundle] represents your application bundle. You can use this to get the app's URL.

You can then use NSURL to get the volume information for the app's URL and determine if it was launched from within the DMG. E.g.:

NSURL* bundleUrl = [[NSBundle mainBundle] bundleURL];
NSString* volumeName = nil;
if ([bundleUrl getResourceValue:&volumeName forKey:NSURLVolumeNameKey error:nil])
{
    if ([volumeName isEqual:@"My App Installer"]) // or whatever your DMG volume name is
    {
        [self tryMigratingToAppsFolder];
    }
}

Or, you could just check if it's not in /Applications. Keep in mind that some people install apps in different locations.

A full list of NSURL's available volume resources is available here.

Upvotes: 1

Related Questions