def run(command): termAddress = AE.AECreateDesc(typeApplicationBundleID, 'com.apple.Terminal') theEvent = AE.AECreateAppleEvent(kAECoreSuite, kAEDoScript, termAddress, kAutoGenerateReturnID, kAnyTransactionID) commandDesc = AE.AECreateDesc(typeChar, command) theEvent.AEPutParamDesc(kAECommandClass, commandDesc) try: theEvent.AESend(SEND_MODE, kAENormalPriority, kAEDefaultTimeout) except AE.Error as why: if why[0] != -600: raise os.system(START_TERMINAL) time.sleep(1) theEvent.AESend(SEND_MODE, kAENormalPriority, kAEDefaultTimeout)
def __init__(self, signature=None, start=0, timeout=0): """Create a communication channel with a particular application. Addressing the application is done by specifying either a 4-byte signature, an AEDesc or an object that will __aepack__ to an AEDesc. """ self.target_signature = None if signature is None: signature = self._signature if type(signature) == AEDescType: self.target = signature elif type(signature) == InstanceType and hasattr(signature, '__aepack__'): self.target = signature.__aepack__() elif type(signature) == StringType and len(signature) == 4: self.target = AE.AECreateDesc(AppleEvents.typeApplSignature, signature) self.target_signature = signature else: raise TypeError, "signature should be 4-char string or AEDesc" self.send_flags = AppleEvents.kAEWaitReply self.send_priority = AppleEvents.kAENormalPriority if timeout: self.send_timeout = timeout else: self.send_timeout = AppleEvents.kAEDefaultTimeout if start: self._start()
def run(command): """Run a shell command in a new Terminal.app window.""" termAddress = AE.AECreateDesc(typeApplSignature, TERMINAL_SIG) theEvent = AE.AECreateAppleEvent(kAECoreSuite, kAEDoScript, termAddress, kAutoGenerateReturnID, kAnyTransactionID) commandDesc = AE.AECreateDesc(typeChar, command) theEvent.AEPutParamDesc(kAECommandClass, commandDesc) try: theEvent.AESend(SEND_MODE, kAENormalPriority, kAEDefaultTimeout) except AE.Error, why: if why[0] != -600: # Terminal.app not yet running raise os.system(START_TERMINAL) time.sleep(1) theEvent.AESend(SEND_MODE, kAENormalPriority, kAEDefaultTimeout)
def mac_show(bundle_id): target = AE.AECreateDesc(AppleEvents.typeApplicationBundleID, bundle_id) activateEvent = AE.AECreateAppleEvent('misc', 'actv', target, AppleEvents.kAutoGenerateReturnID, AppleEvents.kAnyTransactionID) activateEvent.AESend(AppleEvents.kAEWaitReply, AppleEvents.kAENormalPriority, AppleEvents.kAEDefaultTimeout)
def test(): target = AE.AECreateDesc('sign', 'quil') ae = AE.AECreateAppleEvent('aevt', 'oapp', target, -1, 0) print unpackevent(ae) raw_input(":") ae = AE.AECreateAppleEvent('core', 'getd', target, -1, 0) obj = Character(2, Word(1, Document(1))) print obj print repr(obj) packevent(ae, {'----': obj}) params, attrs = unpackevent(ae) print params['----'] raw_input(":")
def main(self): pool = NSAutoreleasePool.alloc().init() NSLog("editing thread started for %s" % self.path) NSLog("shell command: %@", EDITOR_COMMAND % {'linenum': self.linenum, 'filename': self.path.replace("'","'\"'\"'")}) stdin = open('/dev/null','r') stdout = open('/dev/null','w') subprocess.call(EDITOR_COMMAND % {'linenum': self.linenum, 'filename': self.path.replace("'","'\"'\"'")}, shell=True, stdin=stdin, stdout=stdout, stderr=stdout) #os.system(EDITOR_COMMAND % {'linenum':self.linenum, 'filename':self.path}) # Send the "file closed" ODB event. if self.odb_app: NSLog("sending file closed event to %s, %s" % (self.odb_app, type(self.odb_app))) target = AE.AECreateDesc(AppleEvents.typeApplSignature, self.odb_app[::-1]) # For strange endianness reasons, have to reverse this event = AE.AECreateAppleEvent(kODBEditorSuite, kAEClosedFile, target, AppleEvents.kAutoGenerateReturnID, AppleEvents.kAnyTransactionID) if self.odb_token: event.AEPutParamDesc(keySenderToken, pack(self.odb_token, typeWildcard)) fsr = Carbon.File.FSPathMakeRef(self.path)[0].encode('utf-8') event.AEPutParamDesc(AppleEvents.keyDirectObject, pack(fsr)) event.AESend(AppleEvents.kAENoReply, AppleEvents.kAENormalPriority, AppleEvents.kAEDefaultTimeout) #evt_app = NSAppleEventDescriptor.descriptorWithTypeCode_(fourcc(self.odb_app)) #evt = NSAppleEventDescriptor.appleEventWithEventClass_eventID_targetDescriptor_returnID_transactionID_(fourcc(kODBEditorSuite), # fourcc(kAEClosedFile), # evt_app, # -1, # kAutoGenerateReturnID # 0) # kAnyTransactionID #if self.odb_token: # evt_tok = NSAppleEventDescriptor.descriptorWithDescriptorType_data_(fourcc('****'), self.odb_token) # evt.setParamDescriptor_forKeyword_(evt_tok, fourcc(keySenderToken)) # #fsr = objc.FSRef.from_pathname(self.path).data #evt_path = NSAppleEventDescriptor.descriptorWithDescriptorType_bytes_length_(fourcc('fsrf'),fsr,len(fsr)) #evt.setParamDescriptor_forKeyword_(evt_path, fourcc('----')) NSLog("editing thread finished for %s" % self.path) del pool
def __init__(self, signature=None, start=0, timeout=0): self.target_signature = None if signature is None: signature = self._signature if type(signature) == AEDescType: self.target = signature elif type(signature) == InstanceType and hasattr(signature, '__aepack__'): self.target = signature.__aepack__() elif type(signature) == StringType and len(signature) == 4: self.target = AE.AECreateDesc(AppleEvents.typeApplSignature, signature) self.target_signature = signature else: raise TypeError, 'signature should be 4-char string or AEDesc' self.send_flags = AppleEvents.kAEWaitReply self.send_priority = AppleEvents.kAENormalPriority if timeout: self.send_timeout = timeout else: self.send_timeout = AppleEvents.kAEDefaultTimeout if start: self._start() return
"""Tools for use in AppleEvent clients and servers.
"""Tools for use in AppleEvent clients and servers:
def pack(x, forcetype=None): """Pack a python object into an AE descriptor""" if forcetype: if type(x) is StringType: return AE.AECreateDesc(forcetype, x) else: return pack(x).AECoerceDesc(forcetype) if x is None: return AE.AECreateDesc('null', '') if isinstance(x, AEDescType): return x if isinstance(x, FSSType): return AE.AECreateDesc('fss ', x.data) if isinstance(x, FSRefType): return AE.AECreateDesc('fsrf', x.data) if isinstance(x, AliasType): return AE.AECreateDesc('alis', x.data) if isinstance(x, IntType): return AE.AECreateDesc('long', struct.pack('l', x)) if isinstance(x, FloatType): return AE.AECreateDesc('doub', struct.pack('d', x)) if isinstance(x, StringType): return AE.AECreateDesc('TEXT', x) if isinstance(x, UnicodeType): data = x.encode('utf16') if data[:2] == '\xfe\xff': data = data[2:] return AE.AECreateDesc('utxt', data) if isinstance(x, ListType): list = AE.AECreateList('', 0) for item in x: list.AEPutDesc(0, pack(item)) return list if isinstance(x, DictionaryType): record = AE.AECreateList('', 1) for key, value in x.items(): packkey(record, key, value) #record.AEPutParamDesc(key, pack(value)) return record if type(x) == types.ClassType and issubclass(x, ObjectSpecifier): # Note: we are getting a class object here, not an instance return AE.AECreateDesc('type', x.want) if hasattr(x, '__aepack__'): return x.__aepack__() if hasattr(x, 'which'): return AE.AECreateDesc('TEXT', x.which) if hasattr(x, 'want'): return AE.AECreateDesc('TEXT', x.want) return AE.AECreateDesc('TEXT', repr(x)) # Copout
def findwindow(bundleid): """Finds a window identified by the provided bundle identifier""" return AE.AECreateDesc(AppleEvents.typeApplicationBundleID, bundleid)
def pack(x, forcetype=None): if forcetype: if type(x) is StringType: return AE.AECreateDesc(forcetype, x) else: return pack(x).AECoerceDesc(forcetype) if x is None: return AE.AECreateDesc('null', '') elif isinstance(x, AEDescType): return x elif isinstance(x, FSSType): return AE.AECreateDesc('fss ', x.data) elif isinstance(x, FSRefType): return AE.AECreateDesc('fsrf', x.data) elif isinstance(x, AliasType): return AE.AECreateDesc('alis', x.data) elif isinstance(x, IntType): return AE.AECreateDesc('long', struct.pack('l', x)) elif isinstance(x, FloatType): return AE.AECreateDesc('doub', struct.pack('d', x)) elif isinstance(x, StringType): return AE.AECreateDesc('TEXT', x) elif isinstance(x, UnicodeType): data = x.encode('utf16') if data[:2] == '\xfe\xff': data = data[2:] return AE.AECreateDesc('utxt', data) elif isinstance(x, ListType): list = AE.AECreateList('', 0) for item in x: list.AEPutDesc(0, pack(item)) return list elif isinstance(x, DictionaryType): record = AE.AECreateList('', 1) for key, value in x.items(): packkey(record, key, value) return record elif type(x) == types.ClassType and issubclass(x, ObjectSpecifier): return AE.AECreateDesc('type', x.want) elif hasattr(x, '__aepack__'): return x.__aepack__() elif hasattr(x, 'which'): return AE.AECreateDesc('TEXT', x.which) else: return AE.AECreateDesc('TEXT', x.want) if hasattr( x, 'want') else AE.AECreateDesc('TEXT', repr(x))
"""