Skip to content

Instantly share code, notes, and snippets.

@xbee
Created August 3, 2019 04:50
Show Gist options
  • Select an option

  • Save xbee/8ce5087a49ccbcb89636a69aeb4a92f7 to your computer and use it in GitHub Desktop.

Select an option

Save xbee/8ce5087a49ccbcb89636a69aeb4a92f7 to your computer and use it in GitHub Desktop.

Revisions

  1. xbee created this gist Aug 3, 2019.
    25 changes: 25 additions & 0 deletions single.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    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)
    }
    /* ... */
    }