1 """aetypes - Python objects representing various AE types."""
3 from AppleEvents
import *
4 from AERegistry
import *
5 from AEObjects
import *
11 # convoluted, since there are cyclic dependencies between this file and
15 from aepack
import pack
16 return apply(pack
, args
)
18 def IsSubclass(cls
, base
):
19 """Test whether CLASS1 is the same as or a subclass of CLASS2"""
20 # Loop to optimize for single inheritance
22 if cls
is base
: return 1
23 if len(cls
.__bases
__) <> 1: break
24 cls
= cls
.__bases
__[0]
25 # Recurse to cope with multiple inheritance
26 for c
in cls
.__bases
__:
27 if IsSubclass(c
, base
): return 1
30 def IsInstance(x
, cls
):
31 """Test whether OBJECT is an instance of (a subclass of) CLASS"""
32 return type(x
) is InstanceType
and IsSubclass(x
.__class
__, cls
)
35 """'nice' representation of an object"""
36 if type(s
) is StringType
: return repr(s
)
40 """An uninterpreted AE object"""
42 def __init__(self
, type, data
):
47 return "Unknown(%s, %s)" % (`self
.type`
, `self
.data`
)
50 return pack(self
.data
, self
.type)
53 """An AE enumeration value"""
55 def __init__(self
, enum
):
56 self
.enum
= "%-4.4s" % str(enum
)
59 return "Enum(%s)" % `self
.enum`
62 return string
.strip(self
.enum
)
65 return pack(self
.enum
, typeEnumeration
)
68 return IsInstance(x
, Enum
)
71 if IsEnum(enum
): return enum
75 """An AE 4-char typename object"""
77 def __init__(self
, type):
78 self
.type = "%-4.4s" % str(type)
81 return "Type(%s)" % `self
.type`
84 return string
.strip(self
.type)
87 return pack(self
.type, typeType
)
90 return IsInstance(x
, Type
)
93 if IsType(type): return type
98 """An AE 4-char keyword object"""
100 def __init__(self
, keyword
):
101 self
.keyword
= "%-4.4s" % str(keyword
)
104 return "Keyword(%s)" % `self
.keyword`
107 return string
.strip(self
.keyword
)
109 def __aepack__(self
):
110 return pack(self
.keyword
, typeKeyword
)
113 return IsInstance(x
, Keyword
)
116 """An AE range object"""
118 def __init__(self
, start
, stop
):
123 return "Range(%s, %s)" % (`self
.start`
, `self
.stop`
)
126 return "%s thru %s" % (nice(self
.start
), nice(self
.stop
))
128 def __aepack__(self
):
129 return pack({'star': self
.start
, 'stop': self
.stop
}, 'rang')
132 return IsInstance(x
, Range
)
135 """An AE Comparison"""
137 def __init__(self
, obj1
, relo
, obj2
):
139 self
.relo
= "%-4.4s" % str(relo
)
143 return "Comparison(%s, %s, %s)" % (`self
.obj1`
, `self
.relo`
, `self
.obj2`
)
146 return "%s %s %s" % (nice(self
.obj1
), string
.strip(self
.relo
), nice(self
.obj2
))
148 def __aepack__(self
):
149 return pack({'obj1': self
.obj1
,
150 'relo': mkenum(self
.relo
),
155 return IsInstance(x
, Comparison
)
158 """An AE logical expression object"""
160 def __init__(self
, logc
, term
):
161 self
.logc
= "%-4.4s" % str(logc
)
165 return "Logical(%s, %s)" % (`self
.logc`
, `self
.term`
)
168 if type(self
.term
) == ListType
and len(self
.term
) == 2:
169 return "%s %s %s" % (nice(self
.term
[0]),
170 string
.strip(self
.logc
),
173 return "%s(%s)" % (string
.strip(self
.logc
), nice(self
.term
))
175 def __aepack__(self
):
176 return pack({'logc': mkenum(self
.logc
), 'term': self
.term
}, 'logi')
179 return IsInstance(x
, Logical
)
182 """An AE object respresenting text in a certain style"""
184 def __init__(self
, style
, text
):
189 return "StyledText(%s, %s)" % (`self
.style`
, `self
.text`
)
194 def __aepack__(self
):
195 return pack({'ksty': self
.style
, 'ktxt': self
.text
}, 'STXT')
198 return IsInstance(x
, StyledText
)
201 """An AE text object with style, script and language specified"""
203 def __init__(self
, script
, style
, text
):
209 return "AEText(%s, %s, %s)" % (`self
.script`
, `self
.style`
, `self
.text`
)
214 def __aepack__(self
):
215 return pack({keyAEScriptTag
: self
.script
, keyAEStyles
: self
.style
,
216 keyAEText
: self
.text
}, typeAEText
)
219 return IsInstance(x
, AEText
)
222 """A text object with script and language specified"""
224 def __init__(self
, script
, language
, text
):
226 self
.language
= language
230 return "IntlText(%s, %s, %s)" % (`self
.script`
, `self
.language`
, `self
.text`
)
235 def __aepack__(self
):
236 return pack(struct
.pack('hh', self
.script
, self
.language
)+self
.text
,
240 return IsInstance(x
, IntlText
)
242 class IntlWritingCode
:
243 """An object representing script and language"""
245 def __init__(self
, script
, language
):
247 self
.language
= language
250 return "IntlWritingCode(%s, %s)" % (`self
.script`
, `self
.language`
)
253 return "script system %d, language %d"%(self
.script
, self
.language
)
255 def __aepack__(self
):
256 return pack(struct
.pack('hh', self
.script
, self
.language
),
259 def IsIntlWritingCode(x
):
260 return IsInstance(x
, IntlWritingCode
)
265 def __init__(self
, v
, h
):
270 return "QDPoint(%s, %s)" % (`self
.v`
, `self
.h`
)
273 return "(%d, %d)"%(self
.v
, self
.h
)
275 def __aepack__(self
):
276 return pack(struct
.pack('hh', self
.v
, self
.h
),
280 return IsInstance(x
, QDPoint
)
285 def __init__(self
, v0
, h0
, v1
, h1
):
292 return "QDRectangle(%s, %s, %s, %s)" % (`self
.v0`
, `self
.h0`
,
293 `self
.v1`
, `self
.h1`
)
296 return "(%d, %d)-(%d, %d)"%(self
.v0
, self
.h0
, self
.v1
, self
.h1
)
298 def __aepack__(self
):
299 return pack(struct
.pack('hhhh', self
.v0
, self
.h0
, self
.v1
, self
.h1
),
302 def IsQDRectangle(x
):
303 return IsInstance(x
, QDRectangle
)
308 def __init__(self
, r
, g
, b
):
314 return "RGBColor(%s, %s, %s)" % (`self
.r`
, `self
.g`
, `self
.b`
)
317 return "0x%x red, 0x%x green, 0x%x blue"% (self
.r
, self
.g
, self
.b
)
319 def __aepack__(self
):
320 return pack(struct
.pack('hhh', self
.r
, self
.g
, self
.b
),
324 return IsInstance(x
, RGBColor
)
326 class ObjectSpecifier
:
328 """A class for constructing and manipulation AE object specifiers in python.
330 An object specifier is actually a record with four fields:
335 'want' type what kind of thing we want,
336 e.g. word, paragraph or property
338 'form' enum how we specify the thing(s) we want,
339 e.g. by index, by range, by name, or by property specifier
341 'seld' any which thing(s) we want,
342 e.g. its index, its name, or its property specifier
344 'from' object the object in which it is contained,
345 or null, meaning look for it in the application
347 Note that we don't call this class plain "Object", since that name
348 is likely to be used by the application.
351 def __init__(self
, want
, form
, seld
, fr
= None):
358 s
= "ObjectSpecifier(%s, %s, %s" % (`self
.want`
, `self
.form`
, `self
.seld`
)
360 s
= s
+ ", %s)" % `self
.fr`
365 def __aepack__(self
):
366 return pack({'want': mktype(self
.want
),
367 'form': mkenum(self
.form
),
373 def IsObjectSpecifier(x
):
374 return IsInstance(x
, ObjectSpecifier
)
377 class Property(ObjectSpecifier
):
379 def __init__(self
, which
, fr
= None):
380 ObjectSpecifier
.__init
__(self
, 'prop', 'prop', mkenum(which
), fr
)
384 return "Property(%s, %s)" % (`self
.seld
.enum`
, `self
.fr`
)
386 return "Property(%s)" % `self
.seld
.enum`
390 return "Property %s of %s" % (str(self
.seld
), str(self
.fr
))
392 return "Property %s" % str(self
.seld
)
395 class SelectableItem(ObjectSpecifier
):
397 def __init__(self
, want
, seld
, fr
= None):
403 elif IsComparison(seld
) or IsLogical(seld
):
407 ObjectSpecifier
.__init
__(self
, want
, form
, seld
, fr
)
410 class ComponentItem(SelectableItem
):
411 # Derived classes *must* set the *class attribute* 'want' to some constant
413 def __init__(self
, which
, fr
= None):
414 SelectableItem
.__init
__(self
, self
.want
, which
, fr
)
418 return "%s(%s)" % (self
.__class
__.__name
__, `self
.seld`
)
419 return "%s(%s, %s)" % (self
.__class
__.__name
__, `self
.seld`
, `self
.fr`
)
423 if type(seld
) == StringType
:
426 start
, stop
= seld
.start
, seld
.stop
427 if type(start
) == InstanceType
== type(stop
) and \
428 start
.__class
__ == self
.__class
__ == stop
.__class
__:
429 ss
= str(start
.seld
) + " thru " + str(stop
.seld
)
434 s
= "%s %s" % (self
.__class
__.__name
__, ss
)
435 if self
.fr
: s
= s
+ " of %s" % str(self
.fr
)
440 class %s(ComponentItem): want = '%s'
443 exec template
% ("Text", 'text')
444 exec template
% ("Character", 'cha ')
445 exec template
% ("Word", 'cwor')
446 exec template
% ("Line", 'clin')
447 exec template
% ("Paragraph", 'cpar')
448 exec template
% ("Window", 'cwin')
449 exec template
% ("Document", 'docu')
450 exec template
% ("File", 'file')
451 exec template
% ("InsertionPoint", 'cins')