I want to open one application instance only once, so I just do like
NSArray *apps = [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.my.app"]; in main.m
When I open the app from the console, the count of apps array will be 0. However, when I double click on the app, it will be 1
So can anyone tell me what’s the difference between the double click and console open? or give me another way to check if there’s already one instance running?
I want to open one application instance only once, so I just do like
Share
that command queries the workspace and that is updated ‘delayed’
when the app is started from finder, it is launched via the
NSWorkspace, so the workspace is updated right awaywhen the app is started via the console/xcode it is not started via
NSWorkspace, so that class returns 0 at the start. After theNSApplicationof your process is up, the workspace is informed and its 1.=> it is always correct in
- (void)applicationDidFinishLaunching:(NSNotification *)aNotificationso either wait for NSApplication to start and THEN kill it (like you do now, but later)
OR
see Preventing multiple process instances on Linux for a way to do it without cocoa
OR
you look at launchd which can do this 🙂 http://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html