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
This is a version of seb's for Swift 3.0: If another instance of the app with the same bundle ID is already running, show an alert, activate the other instance and quit the duplicate instance.
func applicationDidFinishLaunching(aNotification: NSNotification) {
/* Check if another instance of this app is running. */
let bundleID = Bundle.main.bundleIdentifier!
if NSRunningApplication.runningApplications(withBundleIdentifier: bundleID).count > 1 {
/* Show alert. */
let alert = NSAlert()
alert.addButton(withTitle: "OK")
let appName = Bundle.main.object(forInfoDictionaryKey: kCFBundleNameKey as String) as! String
alert.messageText = "Another copy of \(appName) is already running."
alert.informativeText = "This copy will now quit."
alert.alertStyle = NSAlert.Style.critical
alert.runModal()
/* Activate the other instance and terminate this instance. */
let apps = NSRunningApplication.runningApplications(withBundleIdentifier: bundleID)
for app in apps {
if app != NSRunningApplication.current {
app.activate(options: [.activateAllWindows, .activateIgnoringOtherApps])
break
}
}
NSApp.terminate(nil)
}
/* ... */
}