def force_quit_applicaiton(bid): """force an application to quit for emergency workflows""" # use API to assign a variable for the running API so we can FORCE terminate it apps = NSRunningApplication.runningApplicationsWithBundleIdentifier_(bid) # API returns an array always, must iterate through it for app in apps: # terminate the app app.forceTerminate()
def check_if_running(bid): """Test to see if an app is running by bundle ID""" # macOS API to check if an app bundle is running or not app = NSRunningApplication.runningApplicationsWithBundleIdentifier_(bid) # return True if running, False if not if app: return True if not app: return False
def wait_for_userspace(): """this function will check to see if the Dock and Finder are present to ensure user space has fully loaded. This will stop that race condition""" # give the system a few seconds to log into user space time.sleep(10.0) # check to see if still in the "mac buddy" user context while USER == '_mbsetupuser': logging.info('detected Mac Buddy user context...') time.sleep(1.0) if USER != '_mbsetupuser': break # test to make sure both the Finder and Dock are running by bundle ID to ensure user space is fully loaded app1 = None app2 = None while not all([app1, app2]): app1 = NSRunningApplication.runningApplicationsWithBundleIdentifier_('com.apple.dock') app2 = NSRunningApplication.runningApplicationsWithBundleIdentifier_('com.apple.finder') logging.info('waiting for apps to appear running..') time.sleep(0.5)
def quit_application(bid): """quits apps using NSRunningApplication""" # use API to assign a variable for the running API so we can terminate it apps = NSRunningApplication.runningApplicationsWithBundleIdentifier_(bid) # API returns an array always, must iterate through it for app in apps: # terminate the app app.terminate() # if the app does not terminate in 3 seconds gracefully force it time.sleep(3) if not app.isTerminated(): app.forceTerminate()
def wait_for_userspace(): """this function will check to see if the Dock and Finder are present to ensure user space has fully loaded. This will stop that race condition""" # give the system a few seconds to log into user space time.sleep(7.0) # check to see if still in the "mac buddy" user context while USER == "_mbsetupuser": user, uid, gid = SCDynamicStoreCopyConsoleUser(None, None, None) logging.info("detected Mac Buddy user context...") time.sleep(1.0) if user != "_mbsetupuser": break # test to make sure both the Finder and Dock are running by bundle ID to ensure user space is fully loaded app1 = None app2 = None while not all([app1, app2]): app1 = NSRunningApplication.runningApplicationsWithBundleIdentifier_( "com.apple.dock") app2 = NSRunningApplication.runningApplicationsWithBundleIdentifier_( "com.apple.finder") logging.info("waiting for apps to appear running..") time.sleep(0.5)