Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Two examples of launching an application and quitting it using `NSRunningApplication.terminate()`
#!/usr/bin/python
import AppKit
from time import sleep
from Foundation import NSURL
xcode_url = NSURL.fileURLWithPath_isDirectory_('/Applications/Xcode.app', True)
workspace = AppKit.NSWorkspace.sharedWorkspace()
workspace.launchApplicationAtURL_options_configuration_error_(xcode_url, AppKit.NSWorkspaceLaunchDefault, {}, None)
# Sleeping to just give it time to launch
sleep(10)
# naively assume that we get at least one result back from `runningApplicationsWithBundleIdentifier`
running_xcode = AppKit.NSRunningApplication.runningApplicationsWithBundleIdentifier_('com.apple.dt.Xcode')[0]
running_xcode.terminate()
#!/usr/bin/python
# Alternate example using subprocess.Popen and finding the NSRunningApplication by PID
import AppKit
from time import sleep
from Foundation import NSURL
proc = subprocess.Popen(['/Applications/Xcode.app/Contents/MacOS/Xcode'])
# Sleeping to just give it time to launch
sleep(10)
# Find the running app by the pid we got from Popen()
running_xcode = NSRunningApplication.runningApplicationWithProcessIdentifier_(proc.pid)
running_xcode.terminate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment