2 Python script a module to comunicate with apple events
17 #from aetypes import InstanceType
18 from aepack
import AEDescType
28 Error
= 'PythonScript.Error'
38 def __getattr__(self
, name
):
40 return DCItem(name
, self
)
45 if self
.form
!= 'prop':
49 elif baetypes
.IsRange(self
.seld
):
51 elif baetypes
.IsComparison(self
.seld
) or baetypes
.IsLogical(self
.seld
):
54 # Breakout: specify both form and seld in a tuple
55 # (if you want ID or rele or somesuch)
56 self
.form
, self
.seld
= self
.seld
62 if self
.seld
in ordinal
.keys():
63 self
.seld
= baetypes
.Ordinal(ordinal
[self
.seld
])
66 s
= "baetypes.ObjectSpecifier(%s, %s, %s" % (`self
.want`
, `self
.form`
, `self
.seld`
)
68 s
= s
+ ", %s)" % `self
.fr`
77 def template(self
, seld
=None, fr
=None):
81 def template1(self
, which
, fr
=None):
87 def __init__(self
, comp
, fr
):
91 def __call__(self
, which
=None):
93 self
.compclass
= eval('PsClass.%s' % self
.compclass
)
96 self
.compclass
= eval('PsProperties.%s' % self
.compclass
)
97 except AttributeError:
98 self
.compclass
= eval('PsClass.%s' % self
.compclass
)
99 return self
.compclass(which
, self
.fr
)
108 class PsEnumerations
:
112 def PsScript(sig
=None, Timeout
=0, Ignoring
=0):
115 target
, sig
= Signature(sig
)
116 pyscript
= getaete
.Getaete(sig
)
118 target
, sig
= Signature('Pyth')
119 pyscript
= getaete
.Getaete()
120 setattr(PyScript
, 'timeout', Timeout
)
121 setattr(PyScript
, 'ignoring', Ignoring
)
122 setattr(PyScript
, 'target', target
)
123 for key
, value
in pyscript
[0].items():
124 setattr(PsEvents
, key
, value
)
125 for key
, value
in pyscript
[1].items():
126 CreateClass(key
, 'PsClasses', value
)
128 CreateProperty(val
[0], 'PsClasses', `val
[1]`
)
132 if val
[0] not in elements
.keys():
133 elements
[val
[0]] = val
[1]
134 elif len(val
[1]) > len(elements
[val
[0]]):
135 elements
[val
[0]] = val
[1]
137 for key
, value
in pyscript
[2].items():
139 setattr(PsEnumerations
, val
[0], val
[1])
141 def CreateClass(newClassName
, superClassName
, value
):
142 parentDict
= PsClass
.__dict
__
143 exec "class %s(%s): pass" % (newClassName
, superClassName
) in \
144 globals(), parentDict
145 newClassObj
= parentDict
[newClassName
]
146 newClassObj
.__init
__ = template
147 exec "setattr(newClassObj, 'want', %s)" % `value
[0]`
148 if value
[2] and value
[2][0][0] == 'every':
149 exec "setattr(newClassObj, 'plur', 1)"
151 def CreateProperty(newClassName
, superClassName
, value
):
152 parentDict
= PsProperties
.__dict
__
153 exec "class %s(%s): pass" % (newClassName
, superClassName
) in \
154 globals(), parentDict
155 newClassObj
= parentDict
[newClassName
]
156 if newClassName
== 'Every':
157 value
= "baetypes.mkOrdinal('every')"
158 newClassObj
.__init
__ = template1
159 exec "setattr(newClassObj, 'seld', %s)" % value
161 def Signature(signature
):
162 if type(signature
) == AEDescType
:
164 elif type(signature
) == InstanceType
and hasattr(signature
, '__aepack__'):
165 target
= signature
.__aepack
__()
166 elif type(signature
) == StringType
:
167 if len(signature
) == 4:
168 target
= AE
.AECreateDesc(AppleEvents
.typeApplSignature
, signature
)
169 target_signature
= signature
171 #This should ready be made persistant, so PythonScript 'remembered' where applications were
172 fss
, ok
= macfs
.PromptGetFile('Find the aplication %s' % signature
, 'APPL')
174 target_signature
= fss
.GetCreatorType()[0]
175 target
= AE
.AECreateDesc(AppleEvents
.typeApplSignature
, target_signature
)
177 raise TypeError, "signature should be 4-char string or AEDesc"
178 return target
, target_signature
183 class PyScript(PsEvents
):
184 def __init__(self
, name
, obj
=None, **args
):
185 desc
, code
, subcode
, rply
, message
, keywds
= name
187 # print 'subcode', subcode
189 # print 'message', message
190 # print 'keywds', keywds
195 self
.subcode
= subcode
199 self
.arguments
= self
.keyargs(keywds
, args
)
200 self
.arguments
['----'] = self
.keyfms(message
[0], obj
)
202 ##XXXX Eudora needs this XXXX##
203 if self
.arguments
['----'] == None:
204 del self
.arguments
['----']
205 # print 'arguments', self.arguments
206 if self
.ignoring
or rply
[0] == 'null':
207 self
.send_flags
= AppleEvents
.kAENoReply
209 self
.send_flags
= AppleEvents
.kAEWaitReply
210 self
.send_priority
= AppleEvents
.kAENormalPriority
212 self
.send_timeout
= self
.timeout
214 self
.send_timeout
= AppleEvents
.kAEDefaultTimeout
217 def keyargs(self
, ats
, args
):
218 # print 'keyargs', ats, args
220 for arg
in args
.keys():
223 output
[at
[1]] = self
.keyfms(at
[2][0], args
[arg
])
226 def keyfms(self
, key
, value
):
227 # print 'keyfms', 'key', key, `value`
228 if key
== 'obj ' or key
== 'insl':
235 return baetypes
.mkboolean(value
)
238 val
= eval('PsClass.%s()' % value
)
239 return baetypes
.mktype(str(val
))
241 return baetypes
.mktype(value
)
243 print "I don't know what to put here -- script.keyargs"
247 def newevent(self
, code
, subcode
, parameters
= {}, attributes
= {}):
248 """Create a complete structure for an apple event"""
249 # print code, subcode, parameters, attributes
250 event
= AE
.AECreateAppleEvent(code
, subcode
, self
.target
,
251 AppleEvents
.kAutoGenerateReturnID
, AppleEvents
.kAnyTransactionID
)
252 baetools
.packevent(event
, parameters
, attributes
)
255 def sendevent(self
, event
):
256 """Send a pre-created appleevent, await the reply and unpack it"""
258 reply
= event
.AESend(self
.send_flags
, self
.send_priority
,
260 parameters
, attributes
= baetools
.unpackevent(reply
)
261 return reply
, parameters
, attributes
263 def send(self
, code
, subcode
, parameters
= {}, attributes
= {}):
264 """Send an appleevent given code/subcode/pars/attrs and unpack the reply"""
265 # print code, subcode, parameters, attributes
266 return self
.sendevent(self
.newevent(code
, subcode
, parameters
, attributes
))
269 _reply
, _arguments
, _attributes
= self
.send(self
.code
, self
.subcode
, self
.arguments
, self
.attributes
)
271 if _arguments
.has_key('errn'):
272 raise baetools
.Error
, baetools
.decodeerror(_arguments
)
273 # XXXX Optionally decode result
274 if _arguments
.has_key('----'):
275 return str(_arguments
['----'])
282 Simp
= 'Hermit:Applications:SimpleText'
283 PsScript('MACS', Timeout
=60*60*3)
284 # PsScript('CSOm', Timeout=60*60*3)
285 # PsScript('', Timeout=60*60*3)
286 # PyScript('macsoup')
289 # print PsProperties.__dict__
290 # y = script(ev.Open, File('Hermit:Desktop Folder:Lincolnshire Imp'), using=Application_file(Simp))
291 # print baetypes.NProperty('prop', 'prop', 'pnam', baetypes.ObjectSpecifier('cdis', 'indx', 1, None))
292 # y = PyScript(ev.Get, Disk("Hermit").Folder(7).File(1).Name())
293 # y = PyScript(ev.Get, Disk("Hermit").Size(), As='Integer')
294 # y = PyScript(ev.Get, ps.Desktopobject(1).Startup_disk())
295 # y = PyScript(ev.Get, Mailbox(1).File(), as='TEXT')
296 # print 'y', y, type(y)
298 if __name__
== '__main__':