Example #1
0
 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
Example #2
0
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
Example #3
0
 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
Example #4
0
 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
Example #5
0
	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
Example #6
0
	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))
Example #7
0
    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()
        }
Example #8
0
	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))
Example #9
0
    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()
        }
Example #10
0
	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()}
Example #11
0
 def packnone(self, val):
     return NSAppleEventDescriptor.nullDescriptor()
Example #12
0
 def _packbytes(self, desctype, data):
     return NSAppleEventDescriptor.descriptorWithDescriptorType_bytes_length_(
         fourcharcode(desctype), data, len(data))
Example #13
0
	def _packbytes(self, desctype, data):
		return NSAppleEventDescriptor.descriptorWithDescriptorType_bytes_length_(
			fourcharcode(desctype), data, len(data))
Example #14
0
	def packlist(self, val):
		lst = NSAppleEventDescriptor.listDescriptor()
		for item in val:
			lst.insertDescriptor_atIndex_(self.pack(item), 0)
		return lst
Example #15
0
 def packenum(self, val):
     return NSAppleEventDescriptor.descriptorWithEnumCode_(
         fourcharcode(val.code))
Example #16
0
	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
Example #17
0
 def packstr(self, val):
     return NSAppleEventDescriptor.descriptorWithString_(val)
Example #18
0
	def packbool(self, val):
		return NSAppleEventDescriptor.descriptorWithBoolean_(int(val))
Example #19
0
 def _pack_bytes(description_type, data):
     return NSAppleEventDescriptor.descriptorWithDescriptorType_bytes_length_(
         four_characters_code(description_type), data, len(data)
     )
Example #20
0
	def packenum(self, val): 
		return NSAppleEventDescriptor.descriptorWithEnumCode_(fourcharcode(val.code))
Example #21
0
	def packtype(self, val):
		return NSAppleEventDescriptor.descriptorWithTypeCode_(fourcharcode(val.code))
Example #22
0
	def packint(self, val):
		if (-2**31) <= val < (2**31):
			return NSAppleEventDescriptor.descriptorWithInt32_(val)
		else:
			return self.pack(float(val))
Example #23
0
 def packbool(self, val):
     return NSAppleEventDescriptor.descriptorWithBoolean_(int(val))
Example #24
0
 def packint(self, val):
     if (-2**31) <= val < (2**31):
         return NSAppleEventDescriptor.descriptorWithInt32_(val)
     else:
         return self.pack(float(val))
Example #25
0
 def pack_type(val):
     return NSAppleEventDescriptor.descriptorWithTypeCode_(four_characters_code(val.code))
Example #26
0
 def packlist(self, val):
     lst = NSAppleEventDescriptor.listDescriptor()
     for item in val:
         lst.insertDescriptor_atIndex_(self.pack(item), 0)
     return lst
Example #27
0
 def pack_enum(val):
     return NSAppleEventDescriptor.descriptorWithEnumCode_(four_characters_code(val.code))
Example #28
0
 def packtype(self, val):
     return NSAppleEventDescriptor.descriptorWithTypeCode_(
         fourcharcode(val.code))
Example #29
0
	def packnone(self, val):
		return NSAppleEventDescriptor.nullDescriptor()
Example #30
0
 def pack_none():
     return NSAppleEventDescriptor.nullDescriptor()
Example #31
0
	def packstr(self, val):
		return NSAppleEventDescriptor.descriptorWithString_(val)