1 """aetypes - Python objects representing various AE types."""
3 from warnings
import warnpy3k
4 warnpy3k("In 3.x, the aetypes module is removed.", stacklevel
=2)
6 from Carbon
.AppleEvents
import *
12 # convoluted, since there are cyclic dependencies between this file and
15 def pack(*args
, **kwargs
):
16 from aepack
import pack
17 return pack( *args
, **kwargs
)
20 """'nice' representation of an object"""
21 if type(s
) is StringType
: return repr(s
)
25 """An uninterpreted AE object"""
27 def __init__(self
, type, data
):
32 return "Unknown(%r, %r)" % (self
.type, self
.data
)
35 return pack(self
.data
, self
.type)
38 """An AE enumeration value"""
40 def __init__(self
, enum
):
41 self
.enum
= "%-4.4s" % str(enum
)
44 return "Enum(%r)" % (self
.enum
,)
47 return string
.strip(self
.enum
)
50 return pack(self
.enum
, typeEnumeration
)
53 return isinstance(x
, Enum
)
56 if IsEnum(enum
): return enum
59 # Jack changed the way this is done
61 def __init__(self
, of
, pos
):
66 return "InsertionLoc(%r, %r)" % (self
.of
, self
.pos
)
69 rec
= {'kobj': self
.of
, 'kpos': self
.pos
}
70 return pack(rec
, forcetype
='insl')
72 # Convenience functions for dsp:
74 return InsertionLoc(of
, Enum('bgng'))
77 return InsertionLoc(of
, Enum('end '))
80 """An AE boolean value"""
82 def __init__(self
, bool):
83 self
.bool = (not not bool)
86 return "Boolean(%r)" % (self
.bool,)
95 return pack(struct
.pack('b', self
.bool), 'bool')
98 return isinstance(x
, Boolean
)
101 if IsBoolean(bool): return bool
105 """An AE 4-char typename object"""
107 def __init__(self
, type):
108 self
.type = "%-4.4s" % str(type)
111 return "Type(%r)" % (self
.type,)
114 return string
.strip(self
.type)
116 def __aepack__(self
):
117 return pack(self
.type, typeType
)
120 return isinstance(x
, Type
)
123 if IsType(type): return type
128 """An AE 4-char keyword object"""
130 def __init__(self
, keyword
):
131 self
.keyword
= "%-4.4s" % str(keyword
)
134 return "Keyword(%r)" % repr(self
.keyword
)
137 return string
.strip(self
.keyword
)
139 def __aepack__(self
):
140 return pack(self
.keyword
, typeKeyword
)
143 return isinstance(x
, Keyword
)
146 """An AE range object"""
148 def __init__(self
, start
, stop
):
153 return "Range(%r, %r)" % (self
.start
, self
.stop
)
156 return "%s thru %s" % (nice(self
.start
), nice(self
.stop
))
158 def __aepack__(self
):
159 return pack({'star': self
.start
, 'stop': self
.stop
}, 'rang')
162 return isinstance(x
, Range
)
165 """An AE Comparison"""
167 def __init__(self
, obj1
, relo
, obj2
):
169 self
.relo
= "%-4.4s" % str(relo
)
173 return "Comparison(%r, %r, %r)" % (self
.obj1
, self
.relo
, self
.obj2
)
176 return "%s %s %s" % (nice(self
.obj1
), string
.strip(self
.relo
), nice(self
.obj2
))
178 def __aepack__(self
):
179 return pack({'obj1': self
.obj1
,
180 'relo': mkenum(self
.relo
),
185 return isinstance(x
, Comparison
)
187 class NComparison(Comparison
):
188 # The class attribute 'relo' must be set in a subclass
190 def __init__(self
, obj1
, obj2
):
191 Comparison
.__init
__(obj1
, self
.relo
, obj2
)
196 def __init__(self
, abso
):
198 self
.abso
= "%-4.4s" % str(abso
)
201 return "Ordinal(%r)" % (self
.abso
,)
204 return "%s" % (string
.strip(self
.abso
))
206 def __aepack__(self
):
207 return pack(self
.abso
, 'abso')
210 return isinstance(x
, Ordinal
)
212 class NOrdinal(Ordinal
):
213 # The class attribute 'abso' must be set in a subclass
216 Ordinal
.__init
__(self
, self
.abso
)
219 """An AE logical expression object"""
221 def __init__(self
, logc
, term
):
222 self
.logc
= "%-4.4s" % str(logc
)
226 return "Logical(%r, %r)" % (self
.logc
, self
.term
)
229 if type(self
.term
) == ListType
and len(self
.term
) == 2:
230 return "%s %s %s" % (nice(self
.term
[0]),
231 string
.strip(self
.logc
),
234 return "%s(%s)" % (string
.strip(self
.logc
), nice(self
.term
))
236 def __aepack__(self
):
237 return pack({'logc': mkenum(self
.logc
), 'term': self
.term
}, 'logi')
240 return isinstance(x
, Logical
)
243 """An AE object respresenting text in a certain style"""
245 def __init__(self
, style
, text
):
250 return "StyledText(%r, %r)" % (self
.style
, self
.text
)
255 def __aepack__(self
):
256 return pack({'ksty': self
.style
, 'ktxt': self
.text
}, 'STXT')
259 return isinstance(x
, StyledText
)
262 """An AE text object with style, script and language specified"""
264 def __init__(self
, script
, style
, text
):
270 return "AEText(%r, %r, %r)" % (self
.script
, self
.style
, self
.text
)
275 def __aepack__(self
):
276 return pack({keyAEScriptTag
: self
.script
, keyAEStyles
: self
.style
,
277 keyAEText
: self
.text
}, typeAEText
)
280 return isinstance(x
, AEText
)
283 """A text object with script and language specified"""
285 def __init__(self
, script
, language
, text
):
287 self
.language
= language
291 return "IntlText(%r, %r, %r)" % (self
.script
, self
.language
, self
.text
)
296 def __aepack__(self
):
297 return pack(struct
.pack('hh', self
.script
, self
.language
)+self
.text
,
301 return isinstance(x
, IntlText
)
303 class IntlWritingCode
:
304 """An object representing script and language"""
306 def __init__(self
, script
, language
):
308 self
.language
= language
311 return "IntlWritingCode(%r, %r)" % (self
.script
, self
.language
)
314 return "script system %d, language %d"%(self
.script
, self
.language
)
316 def __aepack__(self
):
317 return pack(struct
.pack('hh', self
.script
, self
.language
),
320 def IsIntlWritingCode(x
):
321 return isinstance(x
, IntlWritingCode
)
326 def __init__(self
, v
, h
):
331 return "QDPoint(%r, %r)" % (self
.v
, self
.h
)
334 return "(%d, %d)"%(self
.v
, self
.h
)
336 def __aepack__(self
):
337 return pack(struct
.pack('hh', self
.v
, self
.h
),
341 return isinstance(x
, QDPoint
)
346 def __init__(self
, v0
, h0
, v1
, h1
):
353 return "QDRectangle(%r, %r, %r, %r)" % (self
.v0
, self
.h0
, self
.v1
, self
.h1
)
356 return "(%d, %d)-(%d, %d)"%(self
.v0
, self
.h0
, self
.v1
, self
.h1
)
358 def __aepack__(self
):
359 return pack(struct
.pack('hhhh', self
.v0
, self
.h0
, self
.v1
, self
.h1
),
362 def IsQDRectangle(x
):
363 return isinstance(x
, QDRectangle
)
368 def __init__(self
, r
, g
, b
):
374 return "RGBColor(%r, %r, %r)" % (self
.r
, self
.g
, self
.b
)
377 return "0x%x red, 0x%x green, 0x%x blue"% (self
.r
, self
.g
, self
.b
)
379 def __aepack__(self
):
380 return pack(struct
.pack('hhh', self
.r
, self
.g
, self
.b
),
384 return isinstance(x
, RGBColor
)
386 class ObjectSpecifier
:
388 """A class for constructing and manipulation AE object specifiers in python.
390 An object specifier is actually a record with four fields:
395 'want' type 4-char class code of thing we want,
396 e.g. word, paragraph or property
398 'form' enum how we specify which 'want' thing(s) we want,
399 e.g. by index, by range, by name, or by property specifier
401 'seld' any which thing(s) we want,
402 e.g. its index, its name, or its property specifier
404 'from' object the object in which it is contained,
405 or null, meaning look for it in the application
407 Note that we don't call this class plain "Object", since that name
408 is likely to be used by the application.
411 def __init__(self
, want
, form
, seld
, fr
= None):
418 s
= "ObjectSpecifier(%r, %r, %r" % (self
.want
, self
.form
, self
.seld
)
420 s
= s
+ ", %r)" % (self
.fr
,)
425 def __aepack__(self
):
426 return pack({'want': mktype(self
.want
),
427 'form': mkenum(self
.form
),
432 def IsObjectSpecifier(x
):
433 return isinstance(x
, ObjectSpecifier
)
436 # Backwards compatibility, sigh...
437 class Property(ObjectSpecifier
):
439 def __init__(self
, which
, fr
= None, want
='prop'):
440 ObjectSpecifier
.__init
__(self
, want
, 'prop', mktype(which
), fr
)
444 return "Property(%r, %r)" % (self
.seld
.type, self
.fr
)
446 return "Property(%r)" % (self
.seld
.type,)
450 return "Property %s of %s" % (str(self
.seld
), str(self
.fr
))
452 return "Property %s" % str(self
.seld
)
455 class NProperty(ObjectSpecifier
):
456 # Subclasses *must* self baseclass attributes:
457 # want is the type of this property
458 # which is the property name of this property
460 def __init__(self
, fr
= None):
466 ObjectSpecifier
.__init
__(self
, self
.want
, 'prop',
467 mktype(self
.which
), fr
)
470 rv
= "Property(%r" % (self
.seld
.type,)
472 rv
= rv
+ ", fr=%r" % (self
.fr
,)
473 if self
.want
!= 'prop':
474 rv
= rv
+ ", want=%r" % (self
.want
,)
479 return "Property %s of %s" % (str(self
.seld
), str(self
.fr
))
481 return "Property %s" % str(self
.seld
)
484 class SelectableItem(ObjectSpecifier
):
486 def __init__(self
, want
, seld
, fr
= None):
492 elif IsComparison(seld
) or IsLogical(seld
):
495 # Breakout: specify both form and seld in a tuple
496 # (if you want ID or rele or somesuch)
500 ObjectSpecifier
.__init
__(self
, want
, form
, seld
, fr
)
503 class ComponentItem(SelectableItem
):
504 # Derived classes *must* set the *class attribute* 'want' to some constant
505 # Also, dictionaries _propdict and _elemdict must be set to map property
506 # and element names to the correct classes
510 def __init__(self
, which
, fr
= None):
511 SelectableItem
.__init
__(self
, self
.want
, which
, fr
)
515 return "%s(%r)" % (self
.__class
__.__name
__, self
.seld
)
516 return "%s(%r, %r)" % (self
.__class
__.__name
__, self
.seld
, self
.fr
)
520 if type(seld
) == StringType
:
523 start
, stop
= seld
.start
, seld
.stop
524 if type(start
) == InstanceType
== type(stop
) and \
525 start
.__class
__ == self
.__class
__ == stop
.__class
__:
526 ss
= str(start
.seld
) + " thru " + str(stop
.seld
)
531 s
= "%s %s" % (self
.__class
__.__name
__, ss
)
532 if self
.fr
: s
= s
+ " of %s" % str(self
.fr
)
535 def __getattr__(self
, name
):
536 if name
in self
._elemdict
:
537 cls
= self
._elemdict
[name
]
538 return DelayedComponentItem(cls
, self
)
539 if name
in self
._propdict
:
540 cls
= self
._propdict
[name
]
542 raise AttributeError, name
545 class DelayedComponentItem
:
546 def __init__(self
, compclass
, fr
):
547 self
.compclass
= compclass
550 def __call__(self
, which
):
551 return self
.compclass(which
, self
.fr
)
554 return "%s(???, %r)" % (self
.__class
__.__name
__, self
.fr
)
557 return "selector for element %s of %s"%(self
.__class
__.__name
__, str(self
.fr
))
560 class %s(ComponentItem): want = '%s'
563 exec template
% ("Text", 'text')
564 exec template
% ("Character", 'cha ')
565 exec template
% ("Word", 'cwor')
566 exec template
% ("Line", 'clin')
567 exec template
% ("paragraph", 'cpar')
568 exec template
% ("Window", 'cwin')
569 exec template
% ("Document", 'docu')
570 exec template
% ("File", 'file')
571 exec template
% ("InsertionPoint", 'cins')