1 """aetypes - Python objects representing various AE types."""
3 from Carbon
.AppleEvents
import *
9 # convoluted, since there are cyclic dependencies between this file and
12 def pack(*args
, **kwargs
):
13 from aepack
import pack
14 return pack( *args
, **kwargs
)
17 """'nice' representation of an object"""
18 if type(s
) is StringType
: return repr(s
)
22 """An uninterpreted AE object"""
24 def __init__(self
, type, data
):
29 return "Unknown(%s, %s)" % (`self
.type`
, `self
.data`
)
32 return pack(self
.data
, self
.type)
35 """An AE enumeration value"""
37 def __init__(self
, enum
):
38 self
.enum
= "%-4.4s" % str(enum
)
41 return "Enum(%s)" % `self
.enum`
44 return string
.strip(self
.enum
)
47 return pack(self
.enum
, typeEnumeration
)
50 return isinstance(x
, Enum
)
53 if IsEnum(enum
): return enum
56 # Jack changed the way this is done
58 def __init__(self
, of
, pos
):
63 return "InsertionLoc(%s, %s)" % (`self
.of`
, `self
.pos`
)
66 rec
= {'kobj': self
.of
, 'kpos': self
.pos
}
67 return pack(rec
, forcetype
='insl')
69 # Convenience functions for dsp:
71 return InsertionLoc(of
, Enum('bgng'))
74 return InsertionLoc(of
, Enum('end '))
77 """An AE boolean value"""
79 def __init__(self
, bool):
80 self
.bool = (not not bool)
83 return "Boolean(%s)" % `self
.bool`
92 return pack(struct
.pack('b', self
.bool), 'bool')
95 return isinstance(x
, Boolean
)
98 if IsBoolean(bool): return bool
102 """An AE 4-char typename object"""
104 def __init__(self
, type):
105 self
.type = "%-4.4s" % str(type)
108 return "Type(%s)" % `self
.type`
111 return string
.strip(self
.type)
113 def __aepack__(self
):
114 return pack(self
.type, typeType
)
117 return isinstance(x
, Type
)
120 if IsType(type): return type
125 """An AE 4-char keyword object"""
127 def __init__(self
, keyword
):
128 self
.keyword
= "%-4.4s" % str(keyword
)
131 return "Keyword(%s)" % `self
.keyword`
134 return string
.strip(self
.keyword
)
136 def __aepack__(self
):
137 return pack(self
.keyword
, typeKeyword
)
140 return isinstance(x
, Keyword
)
143 """An AE range object"""
145 def __init__(self
, start
, stop
):
150 return "Range(%s, %s)" % (`self
.start`
, `self
.stop`
)
153 return "%s thru %s" % (nice(self
.start
), nice(self
.stop
))
155 def __aepack__(self
):
156 return pack({'star': self
.start
, 'stop': self
.stop
}, 'rang')
159 return isinstance(x
, Range
)
162 """An AE Comparison"""
164 def __init__(self
, obj1
, relo
, obj2
):
166 self
.relo
= "%-4.4s" % str(relo
)
170 return "Comparison(%s, %s, %s)" % (`self
.obj1`
, `self
.relo`
, `self
.obj2`
)
173 return "%s %s %s" % (nice(self
.obj1
), string
.strip(self
.relo
), nice(self
.obj2
))
175 def __aepack__(self
):
176 return pack({'obj1': self
.obj1
,
177 'relo': mkenum(self
.relo
),
182 return isinstance(x
, Comparison
)
184 class NComparison(Comparison
):
185 # The class attribute 'relo' must be set in a subclass
187 def __init__(self
, obj1
, obj2
):
188 Comparison
.__init
__(obj1
, self
.relo
, obj2
)
193 def __init__(self
, abso
):
195 self
.abso
= "%-4.4s" % str(abso
)
198 return "Ordinal(%s)" % (`self
.abso`
)
201 return "%s" % (string
.strip(self
.abso
))
203 def __aepack__(self
):
204 return pack(self
.abso
, 'abso')
207 return isinstance(x
, Ordinal
)
209 class NOrdinal(Ordinal
):
210 # The class attribute 'abso' must be set in a subclass
213 Ordinal
.__init
__(self
, self
.abso
)
216 """An AE logical expression object"""
218 def __init__(self
, logc
, term
):
219 self
.logc
= "%-4.4s" % str(logc
)
223 return "Logical(%s, %s)" % (`self
.logc`
, `self
.term`
)
226 if type(self
.term
) == ListType
and len(self
.term
) == 2:
227 return "%s %s %s" % (nice(self
.term
[0]),
228 string
.strip(self
.logc
),
231 return "%s(%s)" % (string
.strip(self
.logc
), nice(self
.term
))
233 def __aepack__(self
):
234 return pack({'logc': mkenum(self
.logc
), 'term': self
.term
}, 'logi')
237 return isinstance(x
, Logical
)
240 """An AE object respresenting text in a certain style"""
242 def __init__(self
, style
, text
):
247 return "StyledText(%s, %s)" % (`self
.style`
, `self
.text`
)
252 def __aepack__(self
):
253 return pack({'ksty': self
.style
, 'ktxt': self
.text
}, 'STXT')
256 return isinstance(x
, StyledText
)
259 """An AE text object with style, script and language specified"""
261 def __init__(self
, script
, style
, text
):
267 return "AEText(%s, %s, %s)" % (`self
.script`
, `self
.style`
, `self
.text`
)
272 def __aepack__(self
):
273 return pack({keyAEScriptTag
: self
.script
, keyAEStyles
: self
.style
,
274 keyAEText
: self
.text
}, typeAEText
)
277 return isinstance(x
, AEText
)
280 """A text object with script and language specified"""
282 def __init__(self
, script
, language
, text
):
284 self
.language
= language
288 return "IntlText(%s, %s, %s)" % (`self
.script`
, `self
.language`
, `self
.text`
)
293 def __aepack__(self
):
294 return pack(struct
.pack('hh', self
.script
, self
.language
)+self
.text
,
298 return isinstance(x
, IntlText
)
300 class IntlWritingCode
:
301 """An object representing script and language"""
303 def __init__(self
, script
, language
):
305 self
.language
= language
308 return "IntlWritingCode(%s, %s)" % (`self
.script`
, `self
.language`
)
311 return "script system %d, language %d"%(self
.script
, self
.language
)
313 def __aepack__(self
):
314 return pack(struct
.pack('hh', self
.script
, self
.language
),
317 def IsIntlWritingCode(x
):
318 return isinstance(x
, IntlWritingCode
)
323 def __init__(self
, v
, h
):
328 return "QDPoint(%s, %s)" % (`self
.v`
, `self
.h`
)
331 return "(%d, %d)"%(self
.v
, self
.h
)
333 def __aepack__(self
):
334 return pack(struct
.pack('hh', self
.v
, self
.h
),
338 return isinstance(x
, QDPoint
)
343 def __init__(self
, v0
, h0
, v1
, h1
):
350 return "QDRectangle(%s, %s, %s, %s)" % (`self
.v0`
, `self
.h0`
,
351 `self
.v1`
, `self
.h1`
)
354 return "(%d, %d)-(%d, %d)"%(self
.v0
, self
.h0
, self
.v1
, self
.h1
)
356 def __aepack__(self
):
357 return pack(struct
.pack('hhhh', self
.v0
, self
.h0
, self
.v1
, self
.h1
),
360 def IsQDRectangle(x
):
361 return isinstance(x
, QDRectangle
)
366 def __init__(self
, r
, g
, b
):
372 return "RGBColor(%s, %s, %s)" % (`self
.r`
, `self
.g`
, `self
.b`
)
375 return "0x%x red, 0x%x green, 0x%x blue"% (self
.r
, self
.g
, self
.b
)
377 def __aepack__(self
):
378 return pack(struct
.pack('hhh', self
.r
, self
.g
, self
.b
),
382 return isinstance(x
, RGBColor
)
384 class ObjectSpecifier
:
386 """A class for constructing and manipulation AE object specifiers in python.
388 An object specifier is actually a record with four fields:
393 'want' type 4-char class code of thing we want,
394 e.g. word, paragraph or property
396 'form' enum how we specify which 'want' thing(s) we want,
397 e.g. by index, by range, by name, or by property specifier
399 'seld' any which thing(s) we want,
400 e.g. its index, its name, or its property specifier
402 'from' object the object in which it is contained,
403 or null, meaning look for it in the application
405 Note that we don't call this class plain "Object", since that name
406 is likely to be used by the application.
409 def __init__(self
, want
, form
, seld
, fr
= None):
416 s
= "ObjectSpecifier(%s, %s, %s" % (`self
.want`
, `self
.form`
, `self
.seld`
)
418 s
= s
+ ", %s)" % `self
.fr`
423 def __aepack__(self
):
424 return pack({'want': mktype(self
.want
),
425 'form': mkenum(self
.form
),
430 def IsObjectSpecifier(x
):
431 return isinstance(x
, ObjectSpecifier
)
434 # Backwards compatability, sigh...
435 class Property(ObjectSpecifier
):
437 def __init__(self
, which
, fr
= None, want
='prop'):
438 ObjectSpecifier
.__init
__(self
, want
, 'prop', mktype(which
), fr
)
442 return "Property(%s, %s)" % (`self
.seld
.type`
, `self
.fr`
)
444 return "Property(%s)" % `self
.seld
.type`
448 return "Property %s of %s" % (str(self
.seld
), str(self
.fr
))
450 return "Property %s" % str(self
.seld
)
453 class NProperty(ObjectSpecifier
):
454 # Subclasses *must* self baseclass attributes:
455 # want is the type of this property
456 # which is the property name of this property
458 def __init__(self
, fr
= None):
464 ObjectSpecifier
.__init
__(self
, self
.want
, 'prop',
465 mktype(self
.which
), fr
)
468 rv
= "Property(%s"%`self
.seld
.type`
470 rv
= rv
+ ", fr=%s" % `self
.fr`
471 if self
.want
!= 'prop':
472 rv
= rv
+ ", want=%s" % `self
.want`
477 return "Property %s of %s" % (str(self
.seld
), str(self
.fr
))
479 return "Property %s" % str(self
.seld
)
482 class SelectableItem(ObjectSpecifier
):
484 def __init__(self
, want
, seld
, fr
= None):
490 elif IsComparison(seld
) or IsLogical(seld
):
493 # Breakout: specify both form and seld in a tuple
494 # (if you want ID or rele or somesuch)
498 ObjectSpecifier
.__init
__(self
, want
, form
, seld
, fr
)
501 class ComponentItem(SelectableItem
):
502 # Derived classes *must* set the *class attribute* 'want' to some constant
503 # Also, dictionaries _propdict and _elemdict must be set to map property
504 # and element names to the correct classes
508 def __init__(self
, which
, fr
= None):
509 SelectableItem
.__init
__(self
, self
.want
, which
, fr
)
513 return "%s(%s)" % (self
.__class
__.__name
__, `self
.seld`
)
514 return "%s(%s, %s)" % (self
.__class
__.__name
__, `self
.seld`
, `self
.fr`
)
518 if type(seld
) == StringType
:
521 start
, stop
= seld
.start
, seld
.stop
522 if type(start
) == InstanceType
== type(stop
) and \
523 start
.__class
__ == self
.__class
__ == stop
.__class
__:
524 ss
= str(start
.seld
) + " thru " + str(stop
.seld
)
529 s
= "%s %s" % (self
.__class
__.__name
__, ss
)
530 if self
.fr
: s
= s
+ " of %s" % str(self
.fr
)
533 def __getattr__(self
, name
):
534 if self
._elemdict
.has_key(name
):
535 cls
= self
._elemdict
[name
]
536 return DelayedComponentItem(cls
, self
)
537 if self
._propdict
.has_key(name
):
538 cls
= self
._propdict
[name
]
540 raise AttributeError, name
543 class DelayedComponentItem
:
544 def __init__(self
, compclass
, fr
):
545 self
.compclass
= compclass
548 def __call__(self
, which
):
549 return self
.compclass(which
, self
.fr
)
552 return "%s(???, %s)" % (self
.__class
__.__name
__, `self
.fr`
)
555 return "selector for element %s of %s"%(self
.__class
__.__name
__, str(self
.fr
))
558 class %s(ComponentItem): want = '%s'
561 exec template
% ("Text", 'text')
562 exec template
% ("Character", 'cha ')
563 exec template
% ("Word", 'cwor')
564 exec template
% ("Line", 'clin')
565 exec template
% ("paragraph", 'cpar')
566 exec template
% ("Window", 'cwin')
567 exec template
% ("Document", 'docu')
568 exec template
% ("File", 'file')
569 exec template
% ("InsertionPoint", 'cins')