| 1 | n/a | """Tools for use in AppleEvent clients and servers. |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | pack(x) converts a Python object to an AEDesc object |
|---|
| 4 | n/a | unpack(desc) does the reverse |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | packevent(event, parameters, attributes) sets params and attrs in an AEAppleEvent record |
|---|
| 7 | n/a | unpackevent(event) returns the parameters and attributes from an AEAppleEvent record |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | Plus... Lots of classes and routines that help representing AE objects, |
|---|
| 10 | n/a | ranges, conditionals, logicals, etc., so you can write, e.g.: |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | x = Character(1, Document("foobar")) |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | and pack(x) will create an AE object reference equivalent to AppleScript's |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | character 1 of document "foobar" |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | Some of the stuff that appears to be exported from this module comes from other |
|---|
| 19 | n/a | files: the pack stuff from aepack, the objects from aetypes. |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | """ |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | from warnings import warnpy3k |
|---|
| 25 | n/a | warnpy3k("In 3.x, the aetools module is removed.", stacklevel=2) |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | from types import * |
|---|
| 28 | n/a | from Carbon import AE |
|---|
| 29 | n/a | from Carbon import Evt |
|---|
| 30 | n/a | from Carbon import AppleEvents |
|---|
| 31 | n/a | import MacOS |
|---|
| 32 | n/a | import sys |
|---|
| 33 | n/a | import time |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | from aetypes import * |
|---|
| 36 | n/a | from aepack import packkey, pack, unpack, coerce, AEDescType |
|---|
| 37 | n/a | |
|---|
| 38 | n/a | Error = 'aetools.Error' |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | # Amount of time to wait for program to be launched |
|---|
| 41 | n/a | LAUNCH_MAX_WAIT_TIME=10 |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | # Special code to unpack an AppleEvent (which is *not* a disguised record!) |
|---|
| 44 | n/a | # Note by Jack: No??!? If I read the docs correctly it *is*.... |
|---|
| 45 | n/a | |
|---|
| 46 | n/a | aekeywords = [ |
|---|
| 47 | n/a | 'tran', |
|---|
| 48 | n/a | 'rtid', |
|---|
| 49 | n/a | 'evcl', |
|---|
| 50 | n/a | 'evid', |
|---|
| 51 | n/a | 'addr', |
|---|
| 52 | n/a | 'optk', |
|---|
| 53 | n/a | 'timo', |
|---|
| 54 | n/a | 'inte', # this attribute is read only - will be set in AESend |
|---|
| 55 | n/a | 'esrc', # this attribute is read only |
|---|
| 56 | n/a | 'miss', # this attribute is read only |
|---|
| 57 | n/a | 'from' # new in 1.0.1 |
|---|
| 58 | n/a | ] |
|---|
| 59 | n/a | |
|---|
| 60 | n/a | def missed(ae): |
|---|
| 61 | n/a | try: |
|---|
| 62 | n/a | desc = ae.AEGetAttributeDesc('miss', 'keyw') |
|---|
| 63 | n/a | except AE.Error, msg: |
|---|
| 64 | n/a | return None |
|---|
| 65 | n/a | return desc.data |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | def unpackevent(ae, formodulename=""): |
|---|
| 68 | n/a | parameters = {} |
|---|
| 69 | n/a | try: |
|---|
| 70 | n/a | dirobj = ae.AEGetParamDesc('----', '****') |
|---|
| 71 | n/a | except AE.Error: |
|---|
| 72 | n/a | pass |
|---|
| 73 | n/a | else: |
|---|
| 74 | n/a | parameters['----'] = unpack(dirobj, formodulename) |
|---|
| 75 | n/a | del dirobj |
|---|
| 76 | n/a | # Workaround for what I feel is a bug in OSX 10.2: 'errn' won't show up in missed... |
|---|
| 77 | n/a | try: |
|---|
| 78 | n/a | dirobj = ae.AEGetParamDesc('errn', '****') |
|---|
| 79 | n/a | except AE.Error: |
|---|
| 80 | n/a | pass |
|---|
| 81 | n/a | else: |
|---|
| 82 | n/a | parameters['errn'] = unpack(dirobj, formodulename) |
|---|
| 83 | n/a | del dirobj |
|---|
| 84 | n/a | while 1: |
|---|
| 85 | n/a | key = missed(ae) |
|---|
| 86 | n/a | if not key: break |
|---|
| 87 | n/a | parameters[key] = unpack(ae.AEGetParamDesc(key, '****'), formodulename) |
|---|
| 88 | n/a | attributes = {} |
|---|
| 89 | n/a | for key in aekeywords: |
|---|
| 90 | n/a | try: |
|---|
| 91 | n/a | desc = ae.AEGetAttributeDesc(key, '****') |
|---|
| 92 | n/a | except (AE.Error, MacOS.Error), msg: |
|---|
| 93 | n/a | if msg[0] != -1701 and msg[0] != -1704: |
|---|
| 94 | n/a | raise |
|---|
| 95 | n/a | continue |
|---|
| 96 | n/a | attributes[key] = unpack(desc, formodulename) |
|---|
| 97 | n/a | return parameters, attributes |
|---|
| 98 | n/a | |
|---|
| 99 | n/a | def packevent(ae, parameters = {}, attributes = {}): |
|---|
| 100 | n/a | for key, value in parameters.items(): |
|---|
| 101 | n/a | packkey(ae, key, value) |
|---|
| 102 | n/a | for key, value in attributes.items(): |
|---|
| 103 | n/a | ae.AEPutAttributeDesc(key, pack(value)) |
|---|
| 104 | n/a | |
|---|
| 105 | n/a | # |
|---|
| 106 | n/a | # Support routine for automatically generated Suite interfaces |
|---|
| 107 | n/a | # These routines are also useable for the reverse function. |
|---|
| 108 | n/a | # |
|---|
| 109 | n/a | def keysubst(arguments, keydict): |
|---|
| 110 | n/a | """Replace long name keys by their 4-char counterparts, and check""" |
|---|
| 111 | n/a | ok = keydict.values() |
|---|
| 112 | n/a | for k in arguments.keys(): |
|---|
| 113 | n/a | if k in keydict: |
|---|
| 114 | n/a | v = arguments[k] |
|---|
| 115 | n/a | del arguments[k] |
|---|
| 116 | n/a | arguments[keydict[k]] = v |
|---|
| 117 | n/a | elif k != '----' and k not in ok: |
|---|
| 118 | n/a | raise TypeError, 'Unknown keyword argument: %s'%k |
|---|
| 119 | n/a | |
|---|
| 120 | n/a | def enumsubst(arguments, key, edict): |
|---|
| 121 | n/a | """Substitute a single enum keyword argument, if it occurs""" |
|---|
| 122 | n/a | if key not in arguments or edict is None: |
|---|
| 123 | n/a | return |
|---|
| 124 | n/a | v = arguments[key] |
|---|
| 125 | n/a | ok = edict.values() |
|---|
| 126 | n/a | if v in edict: |
|---|
| 127 | n/a | arguments[key] = Enum(edict[v]) |
|---|
| 128 | n/a | elif not v in ok: |
|---|
| 129 | n/a | raise TypeError, 'Unknown enumerator: %s'%v |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | def decodeerror(arguments): |
|---|
| 132 | n/a | """Create the 'best' argument for a raise MacOS.Error""" |
|---|
| 133 | n/a | errn = arguments['errn'] |
|---|
| 134 | n/a | err_a1 = errn |
|---|
| 135 | n/a | if 'errs' in arguments: |
|---|
| 136 | n/a | err_a2 = arguments['errs'] |
|---|
| 137 | n/a | else: |
|---|
| 138 | n/a | err_a2 = MacOS.GetErrorString(errn) |
|---|
| 139 | n/a | if 'erob' in arguments: |
|---|
| 140 | n/a | err_a3 = arguments['erob'] |
|---|
| 141 | n/a | else: |
|---|
| 142 | n/a | err_a3 = None |
|---|
| 143 | n/a | |
|---|
| 144 | n/a | return (err_a1, err_a2, err_a3) |
|---|
| 145 | n/a | |
|---|
| 146 | n/a | class TalkTo: |
|---|
| 147 | n/a | """An AE connection to an application""" |
|---|
| 148 | n/a | _signature = None # Can be overridden by subclasses |
|---|
| 149 | n/a | _moduleName = None # Can be overridden by subclasses |
|---|
| 150 | n/a | _elemdict = {} # Can be overridden by subclasses |
|---|
| 151 | n/a | _propdict = {} # Can be overridden by subclasses |
|---|
| 152 | n/a | |
|---|
| 153 | n/a | __eventloop_initialized = 0 |
|---|
| 154 | n/a | def __ensure_WMAvailable(klass): |
|---|
| 155 | n/a | if klass.__eventloop_initialized: return 1 |
|---|
| 156 | n/a | if not MacOS.WMAvailable(): return 0 |
|---|
| 157 | n/a | # Workaround for a but in MacOSX 10.2: we must have an event |
|---|
| 158 | n/a | # loop before we can call AESend. |
|---|
| 159 | n/a | Evt.WaitNextEvent(0,0) |
|---|
| 160 | n/a | return 1 |
|---|
| 161 | n/a | __ensure_WMAvailable = classmethod(__ensure_WMAvailable) |
|---|
| 162 | n/a | |
|---|
| 163 | n/a | def __init__(self, signature=None, start=0, timeout=0): |
|---|
| 164 | n/a | """Create a communication channel with a particular application. |
|---|
| 165 | n/a | |
|---|
| 166 | n/a | Addressing the application is done by specifying either a |
|---|
| 167 | n/a | 4-byte signature, an AEDesc or an object that will __aepack__ |
|---|
| 168 | n/a | to an AEDesc. |
|---|
| 169 | n/a | """ |
|---|
| 170 | n/a | self.target_signature = None |
|---|
| 171 | n/a | if signature is None: |
|---|
| 172 | n/a | signature = self._signature |
|---|
| 173 | n/a | if type(signature) == AEDescType: |
|---|
| 174 | n/a | self.target = signature |
|---|
| 175 | n/a | elif type(signature) == InstanceType and hasattr(signature, '__aepack__'): |
|---|
| 176 | n/a | self.target = signature.__aepack__() |
|---|
| 177 | n/a | elif type(signature) == StringType and len(signature) == 4: |
|---|
| 178 | n/a | self.target = AE.AECreateDesc(AppleEvents.typeApplSignature, signature) |
|---|
| 179 | n/a | self.target_signature = signature |
|---|
| 180 | n/a | else: |
|---|
| 181 | n/a | raise TypeError, "signature should be 4-char string or AEDesc" |
|---|
| 182 | n/a | self.send_flags = AppleEvents.kAEWaitReply |
|---|
| 183 | n/a | self.send_priority = AppleEvents.kAENormalPriority |
|---|
| 184 | n/a | if timeout: |
|---|
| 185 | n/a | self.send_timeout = timeout |
|---|
| 186 | n/a | else: |
|---|
| 187 | n/a | self.send_timeout = AppleEvents.kAEDefaultTimeout |
|---|
| 188 | n/a | if start: |
|---|
| 189 | n/a | self._start() |
|---|
| 190 | n/a | |
|---|
| 191 | n/a | def _start(self): |
|---|
| 192 | n/a | """Start the application, if it is not running yet""" |
|---|
| 193 | n/a | try: |
|---|
| 194 | n/a | self.send('ascr', 'noop') |
|---|
| 195 | n/a | except AE.Error: |
|---|
| 196 | n/a | _launch(self.target_signature) |
|---|
| 197 | n/a | for i in range(LAUNCH_MAX_WAIT_TIME): |
|---|
| 198 | n/a | try: |
|---|
| 199 | n/a | self.send('ascr', 'noop') |
|---|
| 200 | n/a | except AE.Error: |
|---|
| 201 | n/a | pass |
|---|
| 202 | n/a | else: |
|---|
| 203 | n/a | break |
|---|
| 204 | n/a | time.sleep(1) |
|---|
| 205 | n/a | |
|---|
| 206 | n/a | def start(self): |
|---|
| 207 | n/a | """Deprecated, used _start()""" |
|---|
| 208 | n/a | self._start() |
|---|
| 209 | n/a | |
|---|
| 210 | n/a | def newevent(self, code, subcode, parameters = {}, attributes = {}): |
|---|
| 211 | n/a | """Create a complete structure for an apple event""" |
|---|
| 212 | n/a | |
|---|
| 213 | n/a | event = AE.AECreateAppleEvent(code, subcode, self.target, |
|---|
| 214 | n/a | AppleEvents.kAutoGenerateReturnID, AppleEvents.kAnyTransactionID) |
|---|
| 215 | n/a | packevent(event, parameters, attributes) |
|---|
| 216 | n/a | return event |
|---|
| 217 | n/a | |
|---|
| 218 | n/a | def sendevent(self, event): |
|---|
| 219 | n/a | """Send a pre-created appleevent, await the reply and unpack it""" |
|---|
| 220 | n/a | if not self.__ensure_WMAvailable(): |
|---|
| 221 | n/a | raise RuntimeError, "No window manager access, cannot send AppleEvent" |
|---|
| 222 | n/a | reply = event.AESend(self.send_flags, self.send_priority, |
|---|
| 223 | n/a | self.send_timeout) |
|---|
| 224 | n/a | parameters, attributes = unpackevent(reply, self._moduleName) |
|---|
| 225 | n/a | return reply, parameters, attributes |
|---|
| 226 | n/a | |
|---|
| 227 | n/a | def send(self, code, subcode, parameters = {}, attributes = {}): |
|---|
| 228 | n/a | """Send an appleevent given code/subcode/pars/attrs and unpack the reply""" |
|---|
| 229 | n/a | return self.sendevent(self.newevent(code, subcode, parameters, attributes)) |
|---|
| 230 | n/a | |
|---|
| 231 | n/a | # |
|---|
| 232 | n/a | # The following events are somehow "standard" and don't seem to appear in any |
|---|
| 233 | n/a | # suite... |
|---|
| 234 | n/a | # |
|---|
| 235 | n/a | def activate(self): |
|---|
| 236 | n/a | """Send 'activate' command""" |
|---|
| 237 | n/a | self.send('misc', 'actv') |
|---|
| 238 | n/a | |
|---|
| 239 | n/a | def _get(self, _object, asfile=None, _attributes={}): |
|---|
| 240 | n/a | """_get: get data from an object |
|---|
| 241 | n/a | Required argument: the object |
|---|
| 242 | n/a | Keyword argument _attributes: AppleEvent attribute dictionary |
|---|
| 243 | n/a | Returns: the data |
|---|
| 244 | n/a | """ |
|---|
| 245 | n/a | _code = 'core' |
|---|
| 246 | n/a | _subcode = 'getd' |
|---|
| 247 | n/a | |
|---|
| 248 | n/a | _arguments = {'----':_object} |
|---|
| 249 | n/a | if asfile: |
|---|
| 250 | n/a | _arguments['rtyp'] = mktype(asfile) |
|---|
| 251 | n/a | |
|---|
| 252 | n/a | _reply, _arguments, _attributes = self.send(_code, _subcode, |
|---|
| 253 | n/a | _arguments, _attributes) |
|---|
| 254 | n/a | if 'errn' in _arguments: |
|---|
| 255 | n/a | raise Error, decodeerror(_arguments) |
|---|
| 256 | n/a | |
|---|
| 257 | n/a | if '----' in _arguments: |
|---|
| 258 | n/a | return _arguments['----'] |
|---|
| 259 | n/a | if asfile: |
|---|
| 260 | n/a | item.__class__ = asfile |
|---|
| 261 | n/a | return item |
|---|
| 262 | n/a | |
|---|
| 263 | n/a | get = _get |
|---|
| 264 | n/a | |
|---|
| 265 | n/a | _argmap_set = { |
|---|
| 266 | n/a | 'to' : 'data', |
|---|
| 267 | n/a | } |
|---|
| 268 | n/a | |
|---|
| 269 | n/a | def _set(self, _object, _attributes={}, **_arguments): |
|---|
| 270 | n/a | """set: Set an object's data. |
|---|
| 271 | n/a | Required argument: the object for the command |
|---|
| 272 | n/a | Keyword argument to: The new value. |
|---|
| 273 | n/a | Keyword argument _attributes: AppleEvent attribute dictionary |
|---|
| 274 | n/a | """ |
|---|
| 275 | n/a | _code = 'core' |
|---|
| 276 | n/a | _subcode = 'setd' |
|---|
| 277 | n/a | |
|---|
| 278 | n/a | keysubst(_arguments, self._argmap_set) |
|---|
| 279 | n/a | _arguments['----'] = _object |
|---|
| 280 | n/a | |
|---|
| 281 | n/a | |
|---|
| 282 | n/a | _reply, _arguments, _attributes = self.send(_code, _subcode, |
|---|
| 283 | n/a | _arguments, _attributes) |
|---|
| 284 | n/a | if _arguments.get('errn', 0): |
|---|
| 285 | n/a | raise Error, decodeerror(_arguments) |
|---|
| 286 | n/a | # XXXX Optionally decode result |
|---|
| 287 | n/a | if '----' in _arguments: |
|---|
| 288 | n/a | return _arguments['----'] |
|---|
| 289 | n/a | |
|---|
| 290 | n/a | set = _set |
|---|
| 291 | n/a | |
|---|
| 292 | n/a | # Magic glue to allow suite-generated classes to function somewhat |
|---|
| 293 | n/a | # like the "application" class in OSA. |
|---|
| 294 | n/a | |
|---|
| 295 | n/a | def __getattr__(self, name): |
|---|
| 296 | n/a | if name in self._elemdict: |
|---|
| 297 | n/a | cls = self._elemdict[name] |
|---|
| 298 | n/a | return DelayedComponentItem(cls, None) |
|---|
| 299 | n/a | if name in self._propdict: |
|---|
| 300 | n/a | cls = self._propdict[name] |
|---|
| 301 | n/a | return cls() |
|---|
| 302 | n/a | raise AttributeError, name |
|---|
| 303 | n/a | |
|---|
| 304 | n/a | # Tiny Finder class, for local use only |
|---|
| 305 | n/a | |
|---|
| 306 | n/a | class _miniFinder(TalkTo): |
|---|
| 307 | n/a | def open(self, _object, _attributes={}, **_arguments): |
|---|
| 308 | n/a | """open: Open the specified object(s) |
|---|
| 309 | n/a | Required argument: list of objects to open |
|---|
| 310 | n/a | Keyword argument _attributes: AppleEvent attribute dictionary |
|---|
| 311 | n/a | """ |
|---|
| 312 | n/a | _code = 'aevt' |
|---|
| 313 | n/a | _subcode = 'odoc' |
|---|
| 314 | n/a | |
|---|
| 315 | n/a | if _arguments: raise TypeError, 'No optional args expected' |
|---|
| 316 | n/a | _arguments['----'] = _object |
|---|
| 317 | n/a | |
|---|
| 318 | n/a | |
|---|
| 319 | n/a | _reply, _arguments, _attributes = self.send(_code, _subcode, |
|---|
| 320 | n/a | _arguments, _attributes) |
|---|
| 321 | n/a | if 'errn' in _arguments: |
|---|
| 322 | n/a | raise Error, decodeerror(_arguments) |
|---|
| 323 | n/a | # XXXX Optionally decode result |
|---|
| 324 | n/a | if '----' in _arguments: |
|---|
| 325 | n/a | return _arguments['----'] |
|---|
| 326 | n/a | #pass |
|---|
| 327 | n/a | |
|---|
| 328 | n/a | _finder = _miniFinder('MACS') |
|---|
| 329 | n/a | |
|---|
| 330 | n/a | def _launch(appfile): |
|---|
| 331 | n/a | """Open a file thru the finder. Specify file by name or fsspec""" |
|---|
| 332 | n/a | _finder.open(_application_file(('ID ', appfile))) |
|---|
| 333 | n/a | |
|---|
| 334 | n/a | |
|---|
| 335 | n/a | class _application_file(ComponentItem): |
|---|
| 336 | n/a | """application file - An application's file on disk""" |
|---|
| 337 | n/a | want = 'appf' |
|---|
| 338 | n/a | |
|---|
| 339 | n/a | _application_file._propdict = { |
|---|
| 340 | n/a | } |
|---|
| 341 | n/a | _application_file._elemdict = { |
|---|
| 342 | n/a | } |
|---|
| 343 | n/a | |
|---|
| 344 | n/a | # Test program |
|---|
| 345 | n/a | # XXXX Should test more, really... |
|---|
| 346 | n/a | |
|---|
| 347 | n/a | def test(): |
|---|
| 348 | n/a | target = AE.AECreateDesc('sign', 'quil') |
|---|
| 349 | n/a | ae = AE.AECreateAppleEvent('aevt', 'oapp', target, -1, 0) |
|---|
| 350 | n/a | print unpackevent(ae) |
|---|
| 351 | n/a | raw_input(":") |
|---|
| 352 | n/a | ae = AE.AECreateAppleEvent('core', 'getd', target, -1, 0) |
|---|
| 353 | n/a | obj = Character(2, Word(1, Document(1))) |
|---|
| 354 | n/a | print obj |
|---|
| 355 | n/a | print repr(obj) |
|---|
| 356 | n/a | packevent(ae, {'----': obj}) |
|---|
| 357 | n/a | params, attrs = unpackevent(ae) |
|---|
| 358 | n/a | print params['----'] |
|---|
| 359 | n/a | raw_input(":") |
|---|
| 360 | n/a | |
|---|
| 361 | n/a | if __name__ == '__main__': |
|---|
| 362 | n/a | test() |
|---|
| 363 | n/a | sys.exit(1) |
|---|