def _newevent(self, suite, code, args): evt = NSAppleEventDescriptor.appleEventWithEventClass_eventID_targetDescriptor_returnID_transactionID_( fourcharcode(suite), fourcharcode(code), NSAppleEventDescriptor.nullDescriptor(), 0, 0) evt.setDescriptor_forKeyword_(self._codecs.pack(args), fourcharcode(kae.keyDirectObject)) return evt
def processMessages(): # Matches just the error messages from the compiler: # a file name, followed by a colon and line number, an optional column number, # and then the error message. # Since BBEdit doesn't accept column numbers, it doesn't try to parse # other positional feedback from the compiler. # To avoid false matches, this version doesn't allow spaces in file names. pat = re.compile(r"^([^: ]+):(\d+)(?:[.:-]\d+)*[:\s]+(.+)$") # Distinguish warning from errors. warning = re.compile(r"warning", re.IGNORECASE) warning_kind = NSAppleEventDescriptor.descriptorWithEnumCode_( fourCharCode('Wrng')) error_kind = NSAppleEventDescriptor.descriptorWithEnumCode_( fourCharCode('Err ')) # Accumulate the set of errors found on stdin. entries = [] for line in sys.stdin: sys.stdout.write(line) # echo everything match = pat.match(line) if match: file, line, message = match.groups() kind = warning_kind if warning.match(message) else error_kind file = os.path.abspath(file) # BBEdit returns error -1701 for nonexistent files. if os.path.exists(file): entries.append( dict(result_kind=kind, result_file=file, result_line=int(line), message=message)) return entries
def packdict(self, val): record = NSAppleEventDescriptor.recordDescriptor() usrf = desctype = None for key, value in val.items(): if isinstance(key, AEType): if key.code == kae.pClass and isinstance( value, AEType ): # AS packs records that contain a 'class' property by coercing the packed record to the descriptor type specified by the property's value (assuming it's an AEType) desctype = value else: record.setDescriptor_forKeyword_(self.pack(value), fourcharcode(key.code)) else: if not usrf: usrf = NSAppleEventDescriptor.listDescriptor() usrf.insertDescriptor_atIndex_(self.pack(key), 0) usrf.insertDescriptor_atIndex_(self.pack(value), 0) if usrf: record.setDescriptor_forKeyword_(usrf, self.kUSRF) if desctype: newrecord = record.coerceToDescriptorType_( fourcharcode(desctype.code)) if newrecord: record = newrecord else: # coercion failed for some reason, so pack as normal key-value pair record.setDescriptor_forKeyword_(self.pack(desctype), fourcharcode(key.code)) return record
def unpack(self, desc: NSAppleEventDescriptor) -> Any: """Unpack Apple event descriptor. Returns Python value or the original NSAppleEventDescriptor if no decoder is found. """ decoder = self.decoders.get(desc.descriptorType()) if decoder: # Unpack known type. return decoder(desc) # If it is a record-like descriptor, unpack as dict # with an extra AEType(b'pcls') key containing the descriptor type. rec = desc.coerceToDescriptorType_(four_characters_code(aeobjects.typeAERecord)) if rec: rec = self.unpack_ae_record(rec) rec[AEType(aeobjects.pClass)] = AEType(struct.pack(">I", desc.descriptorType())) return rec # Return descriptor as-is. return desc
def packdict(self, val): record = NSAppleEventDescriptor.recordDescriptor() usrf = desctype = None for key, value in val.items(): if isinstance(key, AEType): if key.code == kae.pClass and isinstance(value, AEType): # AS packs records that contain a 'class' property by coercing the packed record to the descriptor type specified by the property's value (assuming it's an AEType) desctype = value else: record.setDescriptor_forKeyword_(self.pack(value), fourcharcode(key.code)) else: if not usrf: usrf = NSAppleEventDescriptor.listDescriptor() usrf.insertDescriptor_atIndex_(self.pack(key), 0) usrf.insertDescriptor_atIndex_(self.pack(value), 0) if usrf: record.setDescriptor_forKeyword_(usrf, self.kUSRF) if desctype: newrecord = record.coerceToDescriptorType_(fourcharcode(desctype.code)) if newrecord: record = newrecord else: # coercion failed for some reason, so pack as normal key-value pair record.setDescriptor_forKeyword_(self.pack(desctype), fourcharcode(key.code)) return record
def call(self, name, *args): """ Call the specified user-defined handler. name : str -- the handler's name (case-sensitive) args : anything -- arguments to pass to script, if any; see documentation for supported types Result : anything | None -- the script's return value, if any Notes: - The handler's name must be a user-defined identifier, not an AppleScript keyword; e.g. 'myCount' is acceptable; 'count' is not. - AppleScript will ignore excess arguments. Passing insufficient arguments will result in an error. - If execution fails, a ScriptError is raised. """ evt = self._newevent(kae.kASAppleScriptSuite, kae.kASPrepositionalSubroutine, args) evt.setDescriptor_forKeyword_(NSAppleEventDescriptor.descriptorWithString_(name), fourcharcode(kae.keyASSubroutineName)) return self._unpackresult(*self._script.executeAppleEvent_error_(evt, None))
def __init__(self): # Clients may add/remove/replace encoder and decoder items: self.encoders = { NSAppleEventDescriptor.class__(): self.packdesc, type(None): self.packnone, bool: self.packbool, int: self.packint, float: self.packfloat, bytes: self.packbytes, str: self.packstr, list: self.packlist, tuple: self.packlist, dict: self.packdict, datetime.datetime: self.packdatetime, AEType: self.packtype, AEEnum: self.packenum, } if sys.version_info.major < 3: # 2.7 compatibility self.encoders[unicode] = self.packstr self.decoders = { fourcharcode(k): v for k, v in { kae.typeNull: self.unpacknull, kae.typeBoolean: self.unpackboolean, kae.typeFalse: self.unpackboolean, kae.typeTrue: self.unpackboolean, kae.typeSInt32: self.unpacksint32, kae.typeIEEE64BitFloatingPoint: self.unpackfloat64, kae.typeUTF8Text: self.unpackunicodetext, kae.typeUTF16ExternalRepresentation: self.unpackunicodetext, kae.typeUnicodeText: self.unpackunicodetext, kae.typeLongDateTime: self.unpacklongdatetime, kae.typeAEList: self.unpackaelist, kae.typeAERecord: self.unpackaerecord, kae.typeAlias: self.unpackfile, kae.typeFSS: self.unpackfile, kae.typeFSRef: self.unpackfile, kae.typeFileURL: self.unpackfile, kae.typeType: self.unpacktype, kae.typeEnumeration: self.unpackenumeration, }.items() }
def __init__(self): # Clients may add/remove/replace encoder and decoder items: self.encoders = { NSAppleEventDescriptor.class__(): self.pack_description, type(None): self.pack_none, bool: self.pack_bool, int: self.pack_int, float: self.pack_float, bytes: self.pack_bytes, str: self.pack_str, list: self.pack_list, tuple: self.pack_list, dict: self.pack_dict, datetime.datetime: self.pack_datetime, AEType: self.pack_type, AEEnum: self.pack_enum, } self.decoders = { four_characters_code(k): v for k, v in { aeobjects.typeNull: self.unpack_null, aeobjects.typeBoolean: self.unpack_boolean, aeobjects.typeFalse: self.unpack_boolean, aeobjects.typeTrue: self.unpack_boolean, aeobjects.typeSInt32: self.unpack_s_int32, aeobjects.typeIEEE64BitFloatingPoint: self.unpack_float64, aeobjects.typeUTF8Text: self.unpack_unicode_text, aeobjects.typeUTF16ExternalRepresentation: self.unpack_unicode_text, aeobjects.typeUnicodeText: self.unpack_unicode_text, aeobjects.typeLongDateTime: self.unpack_long_datetime, aeobjects.typeAEList: self.unpack_ae_list, aeobjects.typeAERecord: self.unpack_ae_record, aeobjects.typeAlias: self.unpack_file, aeobjects.typeFSS: self.unpack_file, aeobjects.typeFSRef: self.unpack_file, aeobjects.typeFileURL: self.unpack_file, aeobjects.typeType: self.unpack_type, aeobjects.typeEnumeration: self.unpack_enumeration, }.items() }
def __init__(self): # Clients may add/remove/replace encoder and decoder items: self.encoders = { NSAppleEventDescriptor.class__(): self.packdesc, type(None): self.packnone, bool: self.packbool, int: self.packint, float: self.packfloat, bytes: self.packbytes, str: self.packstr, list: self.packlist, tuple: self.packlist, dict: self.packdict, datetime.datetime: self.packdatetime, AEType: self.packtype, AEEnum: self.packenum, } if sys.version_info.major < 3: # 2.7 compatibility self.encoders[unicode] = self.packstr self.decoders = {fourcharcode(k): v for k, v in { kae.typeNull: self.unpacknull, kae.typeBoolean: self.unpackboolean, kae.typeFalse: self.unpackboolean, kae.typeTrue: self.unpackboolean, kae.typeSInt32: self.unpacksint32, kae.typeIEEE64BitFloatingPoint: self.unpackfloat64, kae.typeUTF8Text: self.unpackunicodetext, kae.typeUTF16ExternalRepresentation: self.unpackunicodetext, kae.typeUnicodeText: self.unpackunicodetext, kae.typeLongDateTime: self.unpacklongdatetime, kae.typeAEList: self.unpackaelist, kae.typeAERecord: self.unpackaerecord, kae.typeAlias: self.unpackfile, kae.typeFSS: self.unpackfile, kae.typeFSRef: self.unpackfile, kae.typeFileURL: self.unpackfile, kae.typeType: self.unpacktype, kae.typeEnumeration: self.unpackenumeration, }.items()}
def packnone(self, val): return NSAppleEventDescriptor.nullDescriptor()
def _packbytes(self, desctype, data): return NSAppleEventDescriptor.descriptorWithDescriptorType_bytes_length_( fourcharcode(desctype), data, len(data))
def packlist(self, val): lst = NSAppleEventDescriptor.listDescriptor() for item in val: lst.insertDescriptor_atIndex_(self.pack(item), 0) return lst
def packenum(self, val): return NSAppleEventDescriptor.descriptorWithEnumCode_( fourcharcode(val.code))
def packstr(self, val): return NSAppleEventDescriptor.descriptorWithString_(val)
def packbool(self, val): return NSAppleEventDescriptor.descriptorWithBoolean_(int(val))
def _pack_bytes(description_type, data): return NSAppleEventDescriptor.descriptorWithDescriptorType_bytes_length_( four_characters_code(description_type), data, len(data) )
def packenum(self, val): return NSAppleEventDescriptor.descriptorWithEnumCode_(fourcharcode(val.code))
def packtype(self, val): return NSAppleEventDescriptor.descriptorWithTypeCode_(fourcharcode(val.code))
def packint(self, val): if (-2**31) <= val < (2**31): return NSAppleEventDescriptor.descriptorWithInt32_(val) else: return self.pack(float(val))
def pack_type(val): return NSAppleEventDescriptor.descriptorWithTypeCode_(four_characters_code(val.code))
def pack_enum(val): return NSAppleEventDescriptor.descriptorWithEnumCode_(four_characters_code(val.code))
def packtype(self, val): return NSAppleEventDescriptor.descriptorWithTypeCode_( fourcharcode(val.code))
def pack_none(): return NSAppleEventDescriptor.nullDescriptor()