def processexistsfordesc(desc): """Does an application process specified by the given AEAddressDesc exist? Returns false if process doesn't exist OR remote Apple events aren't allowed. Note: this will send a 'launch' Apple event to the target application. """ try: # This will usually raise error -1708 if process is running, and various errors # if the process doesn't exist/can't be reached. If app is running but busy, # AESendMessage() may return a timeout error (this should be -1712, but # -609 is often returned instead for some reason). Event(desc, 'ascrnoop').send() except ae.MacOSError, err: return err.args[0] not in [ -600, -905 ] # -600 = no process; -905 = no network access
###################################################################### # PRIVATE ###################################################################### _kLaunchContinue = 0x4000 _kLaunchNoFileFlags = 0x0800 _kLaunchDontSwitch = 0x0200 _kNoProcess = 0 _kCurrentProcess = 2 _nulladdressdesc = ae.newdesc( kae.typeProcessSerialNumber, struct.pack('LL', 0, _kNoProcess) ) # ae.newappleevent complains if you pass None as address, so we give it one to throw away _launchevent = Event(_nulladdressdesc, 'ascrnoop').AEM_event _runevent = Event(_nulladdressdesc, 'aevtoapp').AEM_event ####### def _launchapplication(path, event): try: return ae.launchapplication( path, event, _kLaunchContinue + _kLaunchNoFileFlags + _kLaunchDontSwitch) except ae.MacOSError, err: raise CantLaunchApplicationError(err.args[0], path) ######################################################################