Search code examples
objective-cmacoscocoabundlensapplication

Cocoa: Can I prevent duplicated launching of one same application?


For example, a user puts my application on his/her desktop. Then he/she copied (instead of moving) it to the /Application folder.

If the one at ~/Desktop has been launched, how can I prevent duplicated launching of the one at ~/Application? Any simple way?

Or if the user launches the one in /Application, I can detect the pre-launched app, and then switch to that immediately?


Solution

  • How about getting a list of all running applications and quitting immediately if your app detects there's another instance already running?

    You can get a list of all active apps via NSWorkspace and runningApplications:

    NSWorkspace * ws = [NSWorkspace sharedWorkspace];
    NSArray *allRunningApps = [ws runningApplications];
    

    It'll return an array of NSRunningApplication instances so all you'd have to do is check the list for e.g. your app's ID via the bundleIdentifier method.