Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect whether an OS X application is already launched

Tags:

Normally an application bundle on OS X can only be started once, however by simply copying the bundle the same application can be launched twice. What's the best strategy to detect and stop this possibility?

On Windows this effect can simply be achieved by the application creating a named resource at launch and then exit if the named resource can't be created, indicating that another process is running that has already created the same resource. These resources are released in a reliable way on Windows when the application quits.

The problem I have seen when researching this is that the APIs on OS X keep state in the file system and thus makes the strategy used on windows unreliable, i.e lingering files after an improper exit can falsely indicate that the application is already running.

The APIs I can use to achieve the same effect on OS X are: posix, carbon and boost.

Ideas?

like image 212
Per Ersson Avatar asked Mar 26 '09 08:03

Per Ersson


People also ask

How do I verify an app on my Mac?

If you're certain that an app you want to install is from a trustworthy source and hasn't been tampered with, you can temporarily override your Mac security settings to open it. Go to Security & Privacy. Click the Open Anyway button in the General pane to confirm your intent to open or install the app.

How do I open an app that Cannot be verified Mac?

Control-click the app icon, then choose Open from the shortcut menu. Click Open. The app is saved as an exception to your security settings, and you can open it in the future by double-clicking it just as you can any registered app.

What is the application environment for OS X?

OS X includes three high-level native development environments that you can use for your application's graphical user interface: Carbon, Cocoa, and Java. These environments are full-featured development environments in their own right, and you can write complete applications in any one of these environments.


1 Answers

This is extremely easy in Snow Leopard:

- (void)deduplicateRunningInstances {     if ([[NSRunningApplication runningApplicationsWithBundleIdentifier:[[NSBundle mainBundle] bundleIdentifier]] count] > 1) {         [[NSAlert alertWithMessageText:[NSString stringWithFormat:@"Another copy of %@ is already running.", [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleNameKey]]                           defaultButton:nil alternateButton:nil otherButton:nil informativeTextWithFormat:@"This copy will now quit."] runModal];          [NSApp terminate:nil];     } } 

See http://blog.jseibert.com/post/1167439217/deduplicating-running-instances-or-how-to-detect-if for more information.

like image 160
Jeff Seibert Avatar answered Oct 29 '22 14:10

Jeff Seibert