Search code examples
objective-ccocoaterminate

Terminating Another App Running - Cocoa


How can I terminate another app that is running in cooca. Let's say I have iTunes running, and I type in quit in my app, it would quit itunes. "iTunes" is just an example, it could be anything the user wants. I can open any app from my application, but I want to know how to close any app running.

thanks

kevin


Solution

  • If you're running on Mac OS X 10.6, Snow Leopard, you can use the new NSRunningApplication terminate method.

    This example shows you how to terminate all other running instances of your app:

    func terminateOtherRunningInstances() {
        NSWorkspace.shared.runningApplications.filter { runningApplication in
            runningApplication.bundleIdentifier == Bundle.main.bundleIdentifier
            && runningApplication.processIdentifier != ProcessInfo().processIdentifier
        }.forEach { otherRunningInstance in
            otherRunningInstance.terminate()
        }
    }