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 |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 28 | |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 29 | from aetypes import * |
| 30 | from aepack import pack, unpack, coerce, AEDescType |
Guido van Rossum | 3155923 | 1995-02-05 16:59:27 +0000 | [diff] [blame] | 31 | |
Jack Jansen | c46f56e | 1996-09-20 15:28:28 +0000 | [diff] [blame] | 32 | Error = 'aetools.Error' |
| 33 | |
Guido van Rossum | 3155923 | 1995-02-05 16:59:27 +0000 | [diff] [blame] | 34 | # Special code to unpack an AppleEvent (which is *not* a disguised record!) |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 35 | # 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] | 36 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 37 | aekeywords = [ |
| 38 | 'tran', |
| 39 | 'rtid', |
| 40 | 'evcl', |
| 41 | 'evid', |
| 42 | 'addr', |
| 43 | 'optk', |
| 44 | 'timo', |
| 45 | 'inte', # this attribute is read only - will be set in AESend |
| 46 | 'esrc', # this attribute is read only |
| 47 | 'miss', # this attribute is read only |
| 48 | 'from' # new in 1.0.1 |
| 49 | ] |
| 50 | |
| 51 | def missed(ae): |
| 52 | try: |
| 53 | desc = ae.AEGetAttributeDesc('miss', 'keyw') |
| 54 | except AE.Error, msg: |
| 55 | return None |
| 56 | return desc.data |
| 57 | |
| 58 | def unpackevent(ae): |
| 59 | parameters = {} |
| 60 | while 1: |
| 61 | key = missed(ae) |
| 62 | if not key: break |
| 63 | parameters[key] = unpack(ae.AEGetParamDesc(key, '****')) |
| 64 | attributes = {} |
| 65 | for key in aekeywords: |
| 66 | try: |
| 67 | desc = ae.AEGetAttributeDesc(key, '****') |
| 68 | except (AE.Error, MacOS.Error), msg: |
| 69 | if msg[0] != -1701: |
| 70 | raise sys.exc_type, sys.exc_value |
| 71 | continue |
| 72 | attributes[key] = unpack(desc) |
| 73 | return parameters, attributes |
| 74 | |
| 75 | def packevent(ae, parameters = {}, attributes = {}): |
| 76 | for key, value in parameters.items(): |
| 77 | ae.AEPutParamDesc(key, pack(value)) |
| 78 | for key, value in attributes.items(): |
| 79 | ae.AEPutAttributeDesc(key, pack(value)) |
| 80 | |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 81 | # |
| 82 | # Support routine for automatically generated Suite interfaces |
Jack Jansen | 8426477 | 1995-10-03 14:35:58 +0000 | [diff] [blame] | 83 | # These routines are also useable for the reverse function. |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 84 | # |
| 85 | def keysubst(arguments, keydict): |
| 86 | """Replace long name keys by their 4-char counterparts, and check""" |
| 87 | ok = keydict.values() |
| 88 | for k in arguments.keys(): |
| 89 | if keydict.has_key(k): |
| 90 | v = arguments[k] |
| 91 | del arguments[k] |
| 92 | arguments[keydict[k]] = v |
| 93 | elif k != '----' and k not in ok: |
| 94 | raise TypeError, 'Unknown keyword argument: %s'%k |
| 95 | |
| 96 | def enumsubst(arguments, key, edict): |
| 97 | """Substitute a single enum keyword argument, if it occurs""" |
| 98 | if not arguments.has_key(key): |
| 99 | return |
| 100 | v = arguments[key] |
| 101 | ok = edict.values() |
| 102 | if edict.has_key(v): |
| 103 | arguments[key] = edict[v] |
| 104 | elif not v in ok: |
| 105 | raise TypeError, 'Unknown enumerator: %s'%v |
| 106 | |
| 107 | def decodeerror(arguments): |
| 108 | """Create the 'best' argument for a raise MacOS.Error""" |
| 109 | errn = arguments['errn'] |
Jack Jansen | 756a69f | 1997-08-08 15:00:03 +0000 | [diff] [blame] | 110 | err_a1 = errn |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 111 | if arguments.has_key('errs'): |
Jack Jansen | 756a69f | 1997-08-08 15:00:03 +0000 | [diff] [blame] | 112 | err_a2 = arguments['errs'] |
| 113 | else: |
| 114 | err_a2 = MacOS.GetErrorString(errn) |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 115 | if arguments.has_key('erob'): |
Jack Jansen | 756a69f | 1997-08-08 15:00:03 +0000 | [diff] [blame] | 116 | err_a3 = arguments['erob'] |
| 117 | else: |
| 118 | err_a3 = None |
| 119 | |
| 120 | return (err_a1, err_a2, err_a3) |
Guido van Rossum | 3155923 | 1995-02-05 16:59:27 +0000 | [diff] [blame] | 121 | |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 122 | class TalkTo: |
| 123 | """An AE connection to an application""" |
| 124 | |
Jack Jansen | c46f56e | 1996-09-20 15:28:28 +0000 | [diff] [blame] | 125 | def __init__(self, signature, start=0): |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 126 | """Create a communication channel with a particular application. |
| 127 | |
| 128 | Addressing the application is done by specifying either a |
| 129 | 4-byte signature, an AEDesc or an object that will __aepack__ |
| 130 | to an AEDesc. |
| 131 | """ |
Jack Jansen | c46f56e | 1996-09-20 15:28:28 +0000 | [diff] [blame] | 132 | self.target_signature = None |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 133 | if type(signature) == AEDescType: |
| 134 | self.target = signature |
| 135 | elif type(signature) == InstanceType and hasattr(signature, '__aepack__'): |
| 136 | self.target = signature.__aepack__() |
Jack Jansen | 7874b5d | 1995-07-29 15:31:10 +0000 | [diff] [blame] | 137 | elif type(signature) == StringType and len(signature) == 4: |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 138 | self.target = AE.AECreateDesc(AppleEvents.typeApplSignature, signature) |
Jack Jansen | c46f56e | 1996-09-20 15:28:28 +0000 | [diff] [blame] | 139 | self.target_signature = signature |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 140 | else: |
| 141 | raise TypeError, "signature should be 4-char string or AEDesc" |
| 142 | self.send_flags = AppleEvents.kAEWaitReply |
| 143 | self.send_priority = AppleEvents.kAENormalPriority |
| 144 | self.send_timeout = AppleEvents.kAEDefaultTimeout |
Jack Jansen | c46f56e | 1996-09-20 15:28:28 +0000 | [diff] [blame] | 145 | if start: |
| 146 | self.start() |
| 147 | |
| 148 | def start(self): |
| 149 | """Start the application, if it is not running yet""" |
| 150 | import findertools |
| 151 | import macfs |
| 152 | |
| 153 | fss = macfs.FindApplication(self.target_signature) |
| 154 | findertools.launch(fss) |
| 155 | |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 156 | def newevent(self, code, subcode, parameters = {}, attributes = {}): |
| 157 | """Create a complete structure for an apple event""" |
| 158 | |
| 159 | event = AE.AECreateAppleEvent(code, subcode, self.target, |
| 160 | AppleEvents.kAutoGenerateReturnID, AppleEvents.kAnyTransactionID) |
| 161 | packevent(event, parameters, attributes) |
| 162 | return event |
| 163 | |
| 164 | def sendevent(self, event): |
| 165 | """Send a pre-created appleevent, await the reply and unpack it""" |
| 166 | |
| 167 | reply = event.AESend(self.send_flags, self.send_priority, |
| 168 | self.send_timeout) |
| 169 | parameters, attributes = unpackevent(reply) |
| 170 | return reply, parameters, attributes |
| 171 | |
| 172 | def send(self, code, subcode, parameters = {}, attributes = {}): |
| 173 | """Send an appleevent given code/subcode/pars/attrs and unpack the reply""" |
| 174 | return self.sendevent(self.newevent(code, subcode, parameters, attributes)) |
| 175 | |
Jack Jansen | 7e156a7 | 1996-01-29 15:45:09 +0000 | [diff] [blame] | 176 | # |
| 177 | # The following events are somehow "standard" and don't seem to appear in any |
| 178 | # suite... |
| 179 | # |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 180 | def activate(self): |
| 181 | """Send 'activate' command""" |
| 182 | self.send('misc', 'actv') |
Jack Jansen | 7e156a7 | 1996-01-29 15:45:09 +0000 | [diff] [blame] | 183 | |
Jack Jansen | 756a69f | 1997-08-08 15:00:03 +0000 | [diff] [blame] | 184 | def _get(self, _object, as=None, _attributes={}): |
| 185 | """_get: get data from an object |
Jack Jansen | 7e156a7 | 1996-01-29 15:45:09 +0000 | [diff] [blame] | 186 | Required argument: the object |
| 187 | Keyword argument _attributes: AppleEvent attribute dictionary |
| 188 | Returns: the data |
| 189 | """ |
| 190 | _code = 'core' |
| 191 | _subcode = 'getd' |
| 192 | |
| 193 | _arguments = {'----':_object} |
Jack Jansen | 756a69f | 1997-08-08 15:00:03 +0000 | [diff] [blame] | 194 | if as: |
| 195 | _arguments['rtyp'] = mktype(as) |
Jack Jansen | 7e156a7 | 1996-01-29 15:45:09 +0000 | [diff] [blame] | 196 | |
| 197 | _reply, _arguments, _attributes = self.send(_code, _subcode, |
| 198 | _arguments, _attributes) |
| 199 | if _arguments.has_key('errn'): |
Jack Jansen | c46f56e | 1996-09-20 15:28:28 +0000 | [diff] [blame] | 200 | raise Error, decodeerror(_arguments) |
Jack Jansen | 7e156a7 | 1996-01-29 15:45:09 +0000 | [diff] [blame] | 201 | |
| 202 | if _arguments.has_key('----'): |
| 203 | return _arguments['----'] |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 204 | |
| 205 | |
Guido van Rossum | 3155923 | 1995-02-05 16:59:27 +0000 | [diff] [blame] | 206 | # Test program |
Jack Jansen | 40775ba | 1995-07-17 11:42:23 +0000 | [diff] [blame] | 207 | # XXXX Should test more, really... |
Guido van Rossum | 3155923 | 1995-02-05 16:59:27 +0000 | [diff] [blame] | 208 | |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 209 | def test(): |
| 210 | target = AE.AECreateDesc('sign', 'KAHL') |
| 211 | ae = AE.AECreateAppleEvent('aevt', 'oapp', target, -1, 0) |
| 212 | print unpackevent(ae) |
Guido van Rossum | 3155923 | 1995-02-05 16:59:27 +0000 | [diff] [blame] | 213 | raw_input(":") |
| 214 | ae = AE.AECreateAppleEvent('core', 'getd', target, -1, 0) |
| 215 | obj = Character(2, Word(1, Document(1))) |
| 216 | print obj |
| 217 | print repr(obj) |
| 218 | packevent(ae, {'----': obj}) |
| 219 | params, attrs = unpackevent(ae) |
| 220 | print params['----'] |
| 221 | raw_input(":") |
Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame] | 222 | |
| 223 | if __name__ == '__main__': |
| 224 | test() |