Skip to content

Instantly share code, notes, and snippets.

@chrisgrande
Created March 8, 2019 01:30
Show Gist options
  • Save chrisgrande/b5d05239d36dcf1015e94bac155047d4 to your computer and use it in GitHub Desktop.
Save chrisgrande/b5d05239d36dcf1015e94bac155047d4 to your computer and use it in GitHub Desktop.
Check if app is running, if not, launch it
#!/usr/bin/python
from AppKit import NSWorkspace, NSBundle, NSRunningApplication
import sys
# app to look for and launch if not running
app_name = 'OneDrive'
app_bundle_id = 'com.microsoft.OneDrive'
# hide the python rocket ship
bundle = NSBundle.mainBundle()
info = bundle.localizedInfoDictionary() or bundle.infoDictionary()
info['LSUIElement'] = '1'
# get the shared workspace object
workspace = NSWorkspace.sharedWorkspace()
# check if app is running - if not - launch it
if NSRunningApplication.runningApplicationsWithBundleIdentifier_(app_bundle_id):
print('%s is Running!' % app_name)
sys.exit(0)
else:
print('%s is NOT running... launching...' % app_name)
workspace.launchApplication_(app_name)
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment