annotate Mac/Lib/toolbox/aetools.py @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents 9d4450350451
children 83de5539f8e4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2287
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
1 """Tools for use in AppleEvent clients and servers.
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
2
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
3 pack(x) converts a Python object to an AEDesc object
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
4 unpack(desc) does the reverse
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
5
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
6 packevent(event, parameters, attributes) sets params and attrs in an AEAppleEvent record
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
7 unpackevent(event) returns the parameters and attributes from an AEAppleEvent record
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
8
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
9 Plus... Lots of classes and routines that help representing AE objects,
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
10 ranges, conditionals, logicals, etc., so you can write, e.g.:
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
11
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
12 x = Character(1, Document("foobar"))
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
13
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
14 and pack(x) will create an AE object reference equivalent to AppleScript's
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
15
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
16 character 1 of document "foobar"
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
17
2834
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
18 Some of the stuff that appears to be exported from this module comes from other
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
19 files: the pack stuff from aepack, the objects from aetypes.
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
20
2287
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
21 """
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
22
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
23
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
24 from types import *
2267
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 import AE
2834
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
26 import AppleEvents
2267
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 import MacOS
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28
2834
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
29 from aetypes import *
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
30 from aepack import pack, unpack, coerce, AEDescType
2267
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
4321
9d4450350451 - Added optional "start" argument to TalkTo initialializer, which
Jack Jansen <jack.jansen@cwi.nl>
parents: 3370
diff changeset
32 Error = 'aetools.Error'
9d4450350451 - Added optional "start" argument to TalkTo initialializer, which
Jack Jansen <jack.jansen@cwi.nl>
parents: 3370
diff changeset
33
2287
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
34 # Special code to unpack an AppleEvent (which is *not* a disguised record!)
2834
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
35 # Note by Jack: No??!? If I read the docs correctly it *is*....
2267
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 aekeywords = [
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 'tran',
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 'rtid',
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 'evcl',
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 'evid',
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 'addr',
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 'optk',
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 'timo',
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 'inte', # this attribute is read only - will be set in AESend
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 'esrc', # this attribute is read only
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 'miss', # this attribute is read only
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 'from' # new in 1.0.1
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 ]
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 def missed(ae):
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 try:
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 desc = ae.AEGetAttributeDesc('miss', 'keyw')
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 except AE.Error, msg:
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 return None
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 return desc.data
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 def unpackevent(ae):
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 parameters = {}
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 while 1:
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 key = missed(ae)
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 if not key: break
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 parameters[key] = unpack(ae.AEGetParamDesc(key, '****'))
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 attributes = {}
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 for key in aekeywords:
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 try:
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 desc = ae.AEGetAttributeDesc(key, '****')
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 except (AE.Error, MacOS.Error), msg:
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 if msg[0] != -1701:
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 raise sys.exc_type, sys.exc_value
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 continue
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 attributes[key] = unpack(desc)
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 return parameters, attributes
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
74
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 def packevent(ae, parameters = {}, attributes = {}):
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 for key, value in parameters.items():
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 ae.AEPutParamDesc(key, pack(value))
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 for key, value in attributes.items():
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 ae.AEPutAttributeDesc(key, pack(value))
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
80
2834
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
81 #
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
82 # Support routine for automatically generated Suite interfaces
3138
f1f18c55d136 Modified/recreated to use keyword arguments
Jack Jansen <jack.jansen@cwi.nl>
parents: 2879
diff changeset
83 # These routines are also useable for the reverse function.
2834
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
84 #
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
85 def keysubst(arguments, keydict):
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
86 """Replace long name keys by their 4-char counterparts, and check"""
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
87 ok = keydict.values()
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
88 for k in arguments.keys():
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
89 if keydict.has_key(k):
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
90 v = arguments[k]
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
91 del arguments[k]
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
92 arguments[keydict[k]] = v
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
93 elif k != '----' and k not in ok:
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
94 raise TypeError, 'Unknown keyword argument: %s'%k
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
95
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
96 def enumsubst(arguments, key, edict):
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
97 """Substitute a single enum keyword argument, if it occurs"""
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
98 if not arguments.has_key(key):
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
99 return
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
100 v = arguments[key]
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
101 ok = edict.values()
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
102 if edict.has_key(v):
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
103 arguments[key] = edict[v]
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
104 elif not v in ok:
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
105 raise TypeError, 'Unknown enumerator: %s'%v
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
106
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
107 def decodeerror(arguments):
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
108 """Create the 'best' argument for a raise MacOS.Error"""
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
109 errn = arguments['errn']
4321
9d4450350451 - Added optional "start" argument to TalkTo initialializer, which
Jack Jansen <jack.jansen@cwi.nl>
parents: 3370
diff changeset
110 errarg = (errn, )
2834
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
111 if arguments.has_key('errs'):
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
112 errarg = errarg + (arguments['errs'],)
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
113 if arguments.has_key('erob'):
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
114 errarg = errarg + (arguments['erob'],)
4321
9d4450350451 - Added optional "start" argument to TalkTo initialializer, which
Jack Jansen <jack.jansen@cwi.nl>
parents: 3370
diff changeset
115 if len(errarg) == 1:
9d4450350451 - Added optional "start" argument to TalkTo initialializer, which
Jack Jansen <jack.jansen@cwi.nl>
parents: 3370
diff changeset
116 errarg = errarg + ('Server returned error code %d'%errn, )
2834
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
117 return errarg
2287
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
118
2834
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
119 class TalkTo:
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
120 """An AE connection to an application"""
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
121
4321
9d4450350451 - Added optional "start" argument to TalkTo initialializer, which
Jack Jansen <jack.jansen@cwi.nl>
parents: 3370
diff changeset
122 def __init__(self, signature, start=0):
2834
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
123 """Create a communication channel with a particular application.
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
124
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
125 Addressing the application is done by specifying either a
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
126 4-byte signature, an AEDesc or an object that will __aepack__
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
127 to an AEDesc.
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
128 """
4321
9d4450350451 - Added optional "start" argument to TalkTo initialializer, which
Jack Jansen <jack.jansen@cwi.nl>
parents: 3370
diff changeset
129 self.target_signature = None
2834
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
130 if type(signature) == AEDescType:
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
131 self.target = signature
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
132 elif type(signature) == InstanceType and hasattr(signature, '__aepack__'):
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
133 self.target = signature.__aepack__()
2879
3637da4c98cb minor fix
Jack Jansen <jack.jansen@cwi.nl>
parents: 2834
diff changeset
134 elif type(signature) == StringType and len(signature) == 4:
2834
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
135 self.target = AE.AECreateDesc(AppleEvents.typeApplSignature, signature)
4321
9d4450350451 - Added optional "start" argument to TalkTo initialializer, which
Jack Jansen <jack.jansen@cwi.nl>
parents: 3370
diff changeset
136 self.target_signature = signature
2834
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
137 else:
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
138 raise TypeError, "signature should be 4-char string or AEDesc"
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
139 self.send_flags = AppleEvents.kAEWaitReply
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
140 self.send_priority = AppleEvents.kAENormalPriority
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
141 self.send_timeout = AppleEvents.kAEDefaultTimeout
4321
9d4450350451 - Added optional "start" argument to TalkTo initialializer, which
Jack Jansen <jack.jansen@cwi.nl>
parents: 3370
diff changeset
142 if start:
9d4450350451 - Added optional "start" argument to TalkTo initialializer, which
Jack Jansen <jack.jansen@cwi.nl>
parents: 3370
diff changeset
143 self.start()
9d4450350451 - Added optional "start" argument to TalkTo initialializer, which
Jack Jansen <jack.jansen@cwi.nl>
parents: 3370
diff changeset
144
9d4450350451 - Added optional "start" argument to TalkTo initialializer, which
Jack Jansen <jack.jansen@cwi.nl>
parents: 3370
diff changeset
145 def start(self):
9d4450350451 - Added optional "start" argument to TalkTo initialializer, which
Jack Jansen <jack.jansen@cwi.nl>
parents: 3370
diff changeset
146 """Start the application, if it is not running yet"""
9d4450350451 - Added optional "start" argument to TalkTo initialializer, which
Jack Jansen <jack.jansen@cwi.nl>
parents: 3370
diff changeset
147 import findertools
9d4450350451 - Added optional "start" argument to TalkTo initialializer, which
Jack Jansen <jack.jansen@cwi.nl>
parents: 3370
diff changeset
148 import macfs
9d4450350451 - Added optional "start" argument to TalkTo initialializer, which
Jack Jansen <jack.jansen@cwi.nl>
parents: 3370
diff changeset
149
9d4450350451 - Added optional "start" argument to TalkTo initialializer, which
Jack Jansen <jack.jansen@cwi.nl>
parents: 3370
diff changeset
150 fss = macfs.FindApplication(self.target_signature)
9d4450350451 - Added optional "start" argument to TalkTo initialializer, which
Jack Jansen <jack.jansen@cwi.nl>
parents: 3370
diff changeset
151 findertools.launch(fss)
9d4450350451 - Added optional "start" argument to TalkTo initialializer, which
Jack Jansen <jack.jansen@cwi.nl>
parents: 3370
diff changeset
152
2834
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
153 def newevent(self, code, subcode, parameters = {}, attributes = {}):
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
154 """Create a complete structure for an apple event"""
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
155
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
156 event = AE.AECreateAppleEvent(code, subcode, self.target,
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
157 AppleEvents.kAutoGenerateReturnID, AppleEvents.kAnyTransactionID)
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
158 packevent(event, parameters, attributes)
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
159 return event
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
160
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
161 def sendevent(self, event):
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
162 """Send a pre-created appleevent, await the reply and unpack it"""
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
163
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
164 reply = event.AESend(self.send_flags, self.send_priority,
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
165 self.send_timeout)
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
166 parameters, attributes = unpackevent(reply)
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
167 return reply, parameters, attributes
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
168
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
169 def send(self, code, subcode, parameters = {}, attributes = {}):
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
170 """Send an appleevent given code/subcode/pars/attrs and unpack the reply"""
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
171 return self.sendevent(self.newevent(code, subcode, parameters, attributes))
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
172
3370
05084ee54e54 Added missing get() method
Jack Jansen <jack.jansen@cwi.nl>
parents: 3138
diff changeset
173 #
05084ee54e54 Added missing get() method
Jack Jansen <jack.jansen@cwi.nl>
parents: 3138
diff changeset
174 # The following events are somehow "standard" and don't seem to appear in any
05084ee54e54 Added missing get() method
Jack Jansen <jack.jansen@cwi.nl>
parents: 3138
diff changeset
175 # suite...
05084ee54e54 Added missing get() method
Jack Jansen <jack.jansen@cwi.nl>
parents: 3138
diff changeset
176 #
2834
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
177 def activate(self):
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
178 """Send 'activate' command"""
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
179 self.send('misc', 'actv')
3370
05084ee54e54 Added missing get() method
Jack Jansen <jack.jansen@cwi.nl>
parents: 3138
diff changeset
180
05084ee54e54 Added missing get() method
Jack Jansen <jack.jansen@cwi.nl>
parents: 3138
diff changeset
181 def get(self, _object, _attributes={}):
05084ee54e54 Added missing get() method
Jack Jansen <jack.jansen@cwi.nl>
parents: 3138
diff changeset
182 """get: get data from an object
05084ee54e54 Added missing get() method
Jack Jansen <jack.jansen@cwi.nl>
parents: 3138
diff changeset
183 Required argument: the object
05084ee54e54 Added missing get() method
Jack Jansen <jack.jansen@cwi.nl>
parents: 3138
diff changeset
184 Keyword argument _attributes: AppleEvent attribute dictionary
05084ee54e54 Added missing get() method
Jack Jansen <jack.jansen@cwi.nl>
parents: 3138
diff changeset
185 Returns: the data
05084ee54e54 Added missing get() method
Jack Jansen <jack.jansen@cwi.nl>
parents: 3138
diff changeset
186 """
05084ee54e54 Added missing get() method
Jack Jansen <jack.jansen@cwi.nl>
parents: 3138
diff changeset
187 _code = 'core'
05084ee54e54 Added missing get() method
Jack Jansen <jack.jansen@cwi.nl>
parents: 3138
diff changeset
188 _subcode = 'getd'
05084ee54e54 Added missing get() method
Jack Jansen <jack.jansen@cwi.nl>
parents: 3138
diff changeset
189
05084ee54e54 Added missing get() method
Jack Jansen <jack.jansen@cwi.nl>
parents: 3138
diff changeset
190 _arguments = {'----':_object}
05084ee54e54 Added missing get() method
Jack Jansen <jack.jansen@cwi.nl>
parents: 3138
diff changeset
191
05084ee54e54 Added missing get() method
Jack Jansen <jack.jansen@cwi.nl>
parents: 3138
diff changeset
192
05084ee54e54 Added missing get() method
Jack Jansen <jack.jansen@cwi.nl>
parents: 3138
diff changeset
193 _reply, _arguments, _attributes = self.send(_code, _subcode,
05084ee54e54 Added missing get() method
Jack Jansen <jack.jansen@cwi.nl>
parents: 3138
diff changeset
194 _arguments, _attributes)
05084ee54e54 Added missing get() method
Jack Jansen <jack.jansen@cwi.nl>
parents: 3138
diff changeset
195 if _arguments.has_key('errn'):
4321
9d4450350451 - Added optional "start" argument to TalkTo initialializer, which
Jack Jansen <jack.jansen@cwi.nl>
parents: 3370
diff changeset
196 raise Error, decodeerror(_arguments)
3370
05084ee54e54 Added missing get() method
Jack Jansen <jack.jansen@cwi.nl>
parents: 3138
diff changeset
197
05084ee54e54 Added missing get() method
Jack Jansen <jack.jansen@cwi.nl>
parents: 3138
diff changeset
198 if _arguments.has_key('----'):
05084ee54e54 Added missing get() method
Jack Jansen <jack.jansen@cwi.nl>
parents: 3138
diff changeset
199 return _arguments['----']
2834
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
200
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
201
2287
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
202 # Test program
2834
4dea3f5545cf Split aepack and aetypes off from aetools (it was getting too big)
Jack Jansen <jack.jansen@cwi.nl>
parents: 2719
diff changeset
203 # XXXX Should test more, really...
2287
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
204
2267
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 def test():
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 target = AE.AECreateDesc('sign', 'KAHL')
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 ae = AE.AECreateAppleEvent('aevt', 'oapp', target, -1, 0)
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 print unpackevent(ae)
2287
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
209 raw_input(":")
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
210 ae = AE.AECreateAppleEvent('core', 'getd', target, -1, 0)
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
211 obj = Character(2, Word(1, Document(1)))
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
212 print obj
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
213 print repr(obj)
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
214 packevent(ae, {'----': obj})
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
215 params, attrs = unpackevent(ae)
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
216 print params['----']
540f8444ef8a more support for different kinds of objects
Guido van Rossum <guido@python.org>
parents: 2267
diff changeset
217 raw_input(":")
2267
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
218
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 if __name__ == '__main__':
7411b6a06920 Committed a more or less working version.
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 test()