How to detect whether an OS X application is already launched

前端 未结 9 1621
自闭症患者
自闭症患者 2020-12-14 02:46

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 t

9条回答
  •  没有蜡笔的小新
    2020-12-14 02:49

    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.

提交回复
热议问题