Guido van Rossum | 3155923 | 1995-02-05 16:59:27 +0000 | [diff] [blame] | 1 | """Tools for use in AppleEvent clients and servers. |
| 2 | |
| 3 | pack(x) converts a Python object to an AEDesc object |
| 4 | unpack(desc) does the reverse |
| 5 | |
| 6 | packevent(event, parameters, attributes) sets params and attrs in an AEAppleEvent record |
| 7 | unpackevent(event) returns the parameters and attributes from an AEAppleEvent record |
| 8 | |
| 9 | Plus... Lots of classes and routines that help representing AE objects, |
| 10 | ranges, conditionals, logicals, etc., so you can write, e.g.: |
| 11 | |
| 12 | x = Character(1, Document("foobar")) |
| 13 | |
| 14 | and pack(x) will create an AE object reference equivalent to AppleScript's |
| 15 | |
| 16 | character 1 of document "foobar" |
| 17 | |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 18 | Some of the stuff that appears to be exported from this module comes from other |
| 19 | files: the pack stuff from aepack, the objects from aetypes. |
| 20 | |
Guido van Rossum | 3155923 | 1995-02-05 16:59:27 +0000 | [diff] [blame] | 21 | """ |
| 22 | |
| 23 | |
Guido van Rossum | 3155923 | 1995-02-05 16:59:27 +0000 | [diff] [blame] | 24 | from types import * |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 25 | import AE |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 26 | import AppleEvents |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 27 | import MacOS |
Jack Jansen | 20989e8 | 1998-01-16 14:40:10 +0000 | [diff] [blame] | 28 | import sys |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 29 | |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 30 | from aetypes import * |
| 31 | from aepack import pack, unpack, coerce, AEDescType |
Guido van Rossum | 3155923 | 1995-02-05 16:59:27 +0000 | [diff] [blame] | 32 | |
Jack Jansen | c46f56e | 1996-09-20 15:28:28 +0000 | [diff] [blame] | 33 | Error = 'aetools.Error' |
| 34 | |
Guido van Rossum | 3155923 | 1995-02-05 16:59:27 +0000 | [diff] [blame] | 35 | # Special code to unpack an AppleEvent (which is *not* a disguised record!) |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 36 | # Note by Jack: No??!? If I read the docs correctly it *is*.... |
Guido van Rossum | 3155923 | 1995-02-05 16:59:27 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 38 | aekeywords = [ |
| 39 | 'tran', |
| 40 | 'rtid', |
| 41 | 'evcl', |
| 42 | 'evid', |
| 43 | 'addr', |
| 44 | 'optk', |
| 45 | 'timo', |
| 46 | 'inte', # this attribute is read only - will be set in AESend |
| 47 | 'esrc', # this attribute is read only |
| 48 | 'miss', # this attribute is read only |
| 49 | 'from' # new in 1.0.1 |
| 50 | ] |
| 51 | |
| 52 | def missed(ae): |
| 53 | try: |
| 54 | desc = ae.AEGetAttributeDesc('miss', 'keyw') |
| 55 | except AE.Error, msg: |
| 56 | return None |
| 57 | return desc.data |
| 58 | |
| 59 | def unpackevent(ae): |
| 60 | parameters = {} |
Jack Jansen | 90c3c16 | 1999-01-22 13:23:12 +0000 | [diff] [blame] | 61 | try: |
| 62 | dirobj = ae.AEGetParamDesc('----', '****') |
| 63 | except AE.Error: |
| 64 | pass |
| 65 | else: |
| 66 | parameters['----'] = unpack(dirobj) |
| 67 | del dirobj |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 68 | while 1: |
| 69 | key = missed(ae) |
| 70 | if not key: break |
| 71 | parameters[key] = unpack(ae.AEGetParamDesc(key, '****')) |
| 72 | attributes = {} |
| 73 | for key in aekeywords: |
| 74 | try: |
| 75 | desc = ae.AEGetAttributeDesc(key, '****') |
| 76 | except (AE.Error, MacOS.Error), msg: |
Jack Jansen | 20989e8 | 1998-01-16 14:40:10 +0000 | [diff] [blame] | 77 | if msg[0] != -1701 and msg[0] != -1704: |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 78 | raise sys.exc_type, sys.exc_value |
| 79 | continue |
| 80 | attributes[key] = unpack(desc) |
| 81 | return parameters, attributes |
| 82 | |
| 83 | def packevent(ae, parameters = {}, attributes = {}): |
| 84 | for key, value in parameters.items(): |
| 85 | ae.AEPutParamDesc(key, pack(value)) |
| 86 | for key, value in attributes.items(): |
| 87 | ae.AEPutAttributeDesc(key, pack(value)) |
| 88 | |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 89 | # |
| 90 | # Support routine for automatically generated Suite interfaces |
Jack Jansen | 8426477 | 1995-10-03 14:35:58 +0000 | [diff] [blame] | 91 | # These routines are also useable for the reverse function. |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 92 | # |
| 93 | def keysubst(arguments, keydict): |
| 94 | """Replace long name keys by their 4-char counterparts, and check""" |
| 95 | ok = keydict.values() |
| 96 | for k in arguments.keys(): |
| 97 | if keydict.has_key(k): |
| 98 | v = arguments[k] |
| 99 | del arguments[k] |
| 100 | arguments[keydict[k]] = v |
| 101 | elif k != '----' and k not in ok: |
| 102 | raise TypeError, 'Unknown keyword argument: %s'%k |
| 103 | |
| 104 | def enumsubst(arguments, key, edict): |
| 105 | """Substitute a single enum keyword argument, if it occurs""" |
Jack Jansen | 2eda244 | 2000-08-20 19:42:52 +0000 | [diff] [blame^] | 106 | if not arguments.has_key(key) or edict is None: |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 107 | return |
| 108 | v = arguments[key] |
| 109 | ok = edict.values() |
| 110 | if edict.has_key(v): |
| 111 | arguments[key] = edict[v] |
| 112 | elif not v in ok: |
| 113 | raise TypeError, 'Unknown enumerator: %s'%v |
| 114 | |
| 115 | def decodeerror(arguments): |
| 116 | """Create the 'best' argument for a raise MacOS.Error""" |
| 117 | errn = arguments['errn'] |
Jack Jansen | 756a69f | 1997-08-08 15:00:03 +0000 | [diff] [blame] | 118 | err_a1 = errn |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 119 | if arguments.has_key('errs'): |
Jack Jansen | 756a69f | 1997-08-08 15:00:03 +0000 | [diff] [blame] | 120 | err_a2 = arguments['errs'] |
| 121 | else: |
| 122 | err_a2 = MacOS.GetErrorString(errn) |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 123 | if arguments.has_key('erob'): |
Jack Jansen | 756a69f | 1997-08-08 15:00:03 +0000 | [diff] [blame] | 124 | err_a3 = arguments['erob'] |
| 125 | else: |
| 126 | err_a3 = None |
| 127 | |
| 128 | return (err_a1, err_a2, err_a3) |
Guido van Rossum | 3155923 | 1995-02-05 16:59:27 +0000 | [diff] [blame] | 129 | |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 130 | class TalkTo: |
| 131 | """An AE connection to an application""" |
Jack Jansen | 2eda244 | 2000-08-20 19:42:52 +0000 | [diff] [blame^] | 132 | _signature = None # Can be overridden by subclasses |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 133 | |
Jack Jansen | 2eda244 | 2000-08-20 19:42:52 +0000 | [diff] [blame^] | 134 | def __init__(self, signature=None, start=0, timeout=0): |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 135 | """Create a communication channel with a particular application. |
| 136 | |
| 137 | Addressing the application is done by specifying either a |
| 138 | 4-byte signature, an AEDesc or an object that will __aepack__ |
| 139 | to an AEDesc. |
| 140 | """ |
Jack Jansen | c46f56e | 1996-09-20 15:28:28 +0000 | [diff] [blame] | 141 | self.target_signature = None |
Jack Jansen | 2eda244 | 2000-08-20 19:42:52 +0000 | [diff] [blame^] | 142 | if signature is None: |
| 143 | signature = self._signature |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 144 | if type(signature) == AEDescType: |
| 145 | self.target = signature |
| 146 | elif type(signature) == InstanceType and hasattr(signature, '__aepack__'): |
| 147 | self.target = signature.__aepack__() |
Jack Jansen | 7874b5d | 1995-07-29 15:31:10 +0000 | [diff] [blame] | 148 | elif type(signature) == StringType and len(signature) == 4: |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 149 | self.target = AE.AECreateDesc(AppleEvents.typeApplSignature, signature) |
Jack Jansen | c46f56e | 1996-09-20 15:28:28 +0000 | [diff] [blame] | 150 | self.target_signature = signature |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 151 | else: |
| 152 | raise TypeError, "signature should be 4-char string or AEDesc" |
| 153 | self.send_flags = AppleEvents.kAEWaitReply |
| 154 | self.send_priority = AppleEvents.kAENormalPriority |
Jack Jansen | 20989e8 | 1998-01-16 14:40:10 +0000 | [diff] [blame] | 155 | if timeout: |
| 156 | self.send_timeout = timeout |
| 157 | else: |
| 158 | self.send_timeout = AppleEvents.kAEDefaultTimeout |
Jack Jansen | c46f56e | 1996-09-20 15:28:28 +0000 | [diff] [blame] | 159 | if start: |
| 160 | self.start() |
| 161 | |
| 162 | def start(self): |
| 163 | """Start the application, if it is not running yet""" |
Jack Jansen | 20989e8 | 1998-01-16 14:40:10 +0000 | [diff] [blame] | 164 | _launch(self.target_signature) |
Jack Jansen | c46f56e | 1996-09-20 15:28:28 +0000 | [diff] [blame] | 165 | |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 166 | def newevent(self, code, subcode, parameters = {}, attributes = {}): |
| 167 | """Create a complete structure for an apple event""" |
| 168 | |
| 169 | event = AE.AECreateAppleEvent(code, subcode, self.target, |
| 170 | AppleEvents.kAutoGenerateReturnID, AppleEvents.kAnyTransactionID) |
| 171 | packevent(event, parameters, attributes) |
| 172 | return event |
| 173 | |
| 174 | def sendevent(self, event): |
| 175 | """Send a pre-created appleevent, await the reply and unpack it""" |
| 176 | |
| 177 | reply = event.AESend(self.send_flags, self.send_priority, |
| 178 | self.send_timeout) |
| 179 | parameters, attributes = unpackevent(reply) |
| 180 | return reply, parameters, attributes |
| 181 | |
| 182 | def send(self, code, subcode, parameters = {}, attributes = {}): |
| 183 | """Send an appleevent given code/subcode/pars/attrs and unpack the reply""" |
| 184 | return self.sendevent(self.newevent(code, subcode, parameters, attributes)) |
| 185 | |
Jack Jansen | 7e156a7 | 1996-01-29 15:45:09 +0000 | [diff] [blame] | 186 | # |
| 187 | # The following events are somehow "standard" and don't seem to appear in any |
| 188 | # suite... |
| 189 | # |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 190 | def activate(self): |
| 191 | """Send 'activate' command""" |
| 192 | self.send('misc', 'actv') |
Jack Jansen | 7e156a7 | 1996-01-29 15:45:09 +0000 | [diff] [blame] | 193 | |
Jack Jansen | 756a69f | 1997-08-08 15:00:03 +0000 | [diff] [blame] | 194 | def _get(self, _object, as=None, _attributes={}): |
| 195 | """_get: get data from an object |
Jack Jansen | 7e156a7 | 1996-01-29 15:45:09 +0000 | [diff] [blame] | 196 | Required argument: the object |
| 197 | Keyword argument _attributes: AppleEvent attribute dictionary |
| 198 | Returns: the data |
| 199 | """ |
| 200 | _code = 'core' |
| 201 | _subcode = 'getd' |
| 202 | |
| 203 | _arguments = {'----':_object} |
Jack Jansen | 756a69f | 1997-08-08 15:00:03 +0000 | [diff] [blame] | 204 | if as: |
| 205 | _arguments['rtyp'] = mktype(as) |
Jack Jansen | 7e156a7 | 1996-01-29 15:45:09 +0000 | [diff] [blame] | 206 | |
| 207 | _reply, _arguments, _attributes = self.send(_code, _subcode, |
| 208 | _arguments, _attributes) |
| 209 | if _arguments.has_key('errn'): |
Jack Jansen | c46f56e | 1996-09-20 15:28:28 +0000 | [diff] [blame] | 210 | raise Error, decodeerror(_arguments) |
Jack Jansen | 7e156a7 | 1996-01-29 15:45:09 +0000 | [diff] [blame] | 211 | |
| 212 | if _arguments.has_key('----'): |
| 213 | return _arguments['----'] |
Jack Jansen | 20989e8 | 1998-01-16 14:40:10 +0000 | [diff] [blame] | 214 | |
| 215 | # Tiny Finder class, for local use only |
| 216 | |
| 217 | class _miniFinder(TalkTo): |
| 218 | def open(self, _object, _attributes={}, **_arguments): |
| 219 | """open: Open the specified object(s) |
| 220 | Required argument: list of objects to open |
| 221 | Keyword argument _attributes: AppleEvent attribute dictionary |
| 222 | """ |
| 223 | _code = 'aevt' |
| 224 | _subcode = 'odoc' |
| 225 | |
| 226 | if _arguments: raise TypeError, 'No optional args expected' |
| 227 | _arguments['----'] = _object |
| 228 | |
| 229 | |
| 230 | _reply, _arguments, _attributes = self.send(_code, _subcode, |
| 231 | _arguments, _attributes) |
| 232 | if _arguments.has_key('errn'): |
Jack Jansen | a0fcd25 | 1999-01-21 13:34:26 +0000 | [diff] [blame] | 233 | raise Error, decodeerror(_arguments) |
Jack Jansen | 20989e8 | 1998-01-16 14:40:10 +0000 | [diff] [blame] | 234 | # XXXX Optionally decode result |
| 235 | if _arguments.has_key('----'): |
| 236 | return _arguments['----'] |
| 237 | #pass |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 238 | |
Jack Jansen | 20989e8 | 1998-01-16 14:40:10 +0000 | [diff] [blame] | 239 | _finder = _miniFinder('MACS') |
| 240 | |
| 241 | def _launch(appfile): |
| 242 | """Open a file thru the finder. Specify file by name or fsspec""" |
| 243 | _finder.open(_application_file(('ID ', appfile))) |
| 244 | |
| 245 | |
| 246 | class _application_file(ComponentItem): |
| 247 | """application file - An application's file on disk""" |
| 248 | want = 'appf' |
| 249 | |
| 250 | _application_file._propdict = { |
| 251 | } |
| 252 | _application_file._elemdict = { |
| 253 | } |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 254 | |
Guido van Rossum | 3155923 | 1995-02-05 16:59:27 +0000 | [diff] [blame] | 255 | # Test program |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 256 | # XXXX Should test more, really... |
Guido van Rossum | 3155923 | 1995-02-05 16:59:27 +0000 | [diff] [blame] | 257 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 258 | def test(): |
Jack Jansen | 20989e8 | 1998-01-16 14:40:10 +0000 | [diff] [blame] | 259 | target = AE.AECreateDesc('sign', 'quil') |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 260 | ae = AE.AECreateAppleEvent('aevt', 'oapp', target, -1, 0) |
| 261 | print unpackevent(ae) |
Guido van Rossum | 3155923 | 1995-02-05 16:59:27 +0000 | [diff] [blame] | 262 | raw_input(":") |
| 263 | ae = AE.AECreateAppleEvent('core', 'getd', target, -1, 0) |
| 264 | obj = Character(2, Word(1, Document(1))) |
| 265 | print obj |
| 266 | print repr(obj) |
| 267 | packevent(ae, {'----': obj}) |
| 268 | params, attrs = unpackevent(ae) |
| 269 | print params['----'] |
| 270 | raw_input(":") |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 271 | |
| 272 | if __name__ == '__main__': |
| 273 | test() |
Jack Jansen | 20989e8 | 1998-01-16 14:40:10 +0000 | [diff] [blame] | 274 | sys.exit(1) |