aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/tools/python/src/Lib/plat-mac/aetools.py
blob: 41d7a2170947be020dd0fda2f99a5c0b9549281d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
"""Tools for use in AppleEvent clients and servers.

pack(x) converts a Python object to an AEDesc object
unpack(desc) does the reverse

packevent(event, parameters, attributes) sets params and attrs in an AEAppleEvent record
unpackevent(event) returns the parameters and attributes from an AEAppleEvent record

Plus...  Lots of classes and routines that help representing AE objects,
ranges, conditionals, logicals, etc., so you can write, e.g.:

    x = Character(1, Document("foobar"))

and pack(x) will create an AE object reference equivalent to AppleScript's

    character 1 of document "foobar"

Some of the stuff that appears to be exported from this module comes from other
files: the pack stuff from aepack, the objects from aetypes.

"""


from warnings import warnpy3k
warnpy3k("In 3.x, the aetools module is removed.", stacklevel=2)

from types import *
from Carbon import AE
from Carbon import Evt
from Carbon import AppleEvents
import MacOS
import sys
import time

from aetypes import *
from aepack import packkey, pack, unpack, coerce, AEDescType

Error = 'aetools.Error'

# Amount of time to wait for program to be launched
LAUNCH_MAX_WAIT_TIME=10

# Special code to unpack an AppleEvent (which is *not* a disguised record!)
# Note by Jack: No??!? If I read the docs correctly it *is*....

aekeywords = [
    'tran',
    'rtid',
    'evcl',
    'evid',
    'addr',
    'optk',
    'timo',
    'inte', # this attribute is read only - will be set in AESend
    'esrc', # this attribute is read only
    'miss', # this attribute is read only
    'from'  # new in 1.0.1
]

def missed(ae):
    try:
        desc = ae.AEGetAttributeDesc('miss', 'keyw')
    except AE.Error, msg:
        return None
    return desc.data

def unpackevent(ae, formodulename=""):
    parameters = {}
    try:
        dirobj = ae.AEGetParamDesc('----', '****')
    except AE.Error:
        pass
    else:
        parameters['----'] = unpack(dirobj, formodulename)
        del dirobj
    # Workaround for what I feel is a bug in OSX 10.2: 'errn' won't show up in missed...
    try:
        dirobj = ae.AEGetParamDesc('errn', '****')
    except AE.Error:
        pass
    else:
        parameters['errn'] = unpack(dirobj, formodulename)
        del dirobj
    while 1:
        key = missed(ae)
        if not key: break
        parameters[key] = unpack(ae.AEGetParamDesc(key, '****'), formodulename)
    attributes = {}
    for key in aekeywords:
        try:
            desc = ae.AEGetAttributeDesc(key, '****')
        except (AE.Error, MacOS.Error), msg:
            if msg[0] != -1701 and msg[0] != -1704:
                raise
            continue
        attributes[key] = unpack(desc, formodulename)
    return parameters, attributes

def packevent(ae, parameters = {}, attributes = {}):
    for key, value in parameters.items():
        packkey(ae, key, value)
    for key, value in attributes.items():
        ae.AEPutAttributeDesc(key, pack(value))

#
# Support routine for automatically generated Suite interfaces
# These routines are also useable for the reverse function.
#
def keysubst(arguments, keydict):
    """Replace long name keys by their 4-char counterparts, and check"""
    ok = keydict.values()
    for k in arguments.keys():
        if k in keydict:
            v = arguments[k]
            del arguments[k]
            arguments[keydict[k]] = v
        elif k != '----' and k not in ok:
            raise TypeError, 'Unknown keyword argument: %s'%k

def enumsubst(arguments, key, edict):
    """Substitute a single enum keyword argument, if it occurs"""
    if key not in arguments or edict is None:
        return
    v = arguments[key]
    ok = edict.values()
    if v in edict:
        arguments[key] = Enum(edict[v])
    elif not v in ok:
        raise TypeError, 'Unknown enumerator: %s'%v

def decodeerror(arguments):
    """Create the 'best' argument for a raise MacOS.Error"""
    errn = arguments['errn']
    err_a1 = errn
    if 'errs' in arguments:
        err_a2 = arguments['errs']
    else:
        err_a2 = MacOS.GetErrorString(errn)
    if 'erob' in arguments:
        err_a3 = arguments['erob']
    else:
        err_a3 = None

    return (err_a1, err_a2, err_a3)

class TalkTo:
    """An AE connection to an application"""
    _signature = None   # Can be overridden by subclasses
    _moduleName = None  # Can be overridden by subclasses
    _elemdict = {}      # Can be overridden by subclasses
    _propdict = {}      # Can be overridden by subclasses

    __eventloop_initialized = 0
    def __ensure_WMAvailable(klass):
        if klass.__eventloop_initialized: return 1
        if not MacOS.WMAvailable(): return 0
        # Workaround for a but in MacOSX 10.2: we must have an event
        # loop before we can call AESend.
        Evt.WaitNextEvent(0,0)
        return 1
    __ensure_WMAvailable = classmethod(__ensure_WMAvailable)

    def __init__(self, signature=None, start=0, timeout=0):
        """Create a communication channel with a particular application.

        Addressing the application is done by specifying either a
        4-byte signature, an AEDesc or an object that will __aepack__
        to an AEDesc.
        """
        self.target_signature = None
        if signature is None:
            signature = self._signature
        if type(signature) == AEDescType:
            self.target = signature
        elif type(signature) == InstanceType and hasattr(signature, '__aepack__'):
            self.target = signature.__aepack__()
        elif type(signature) == StringType and len(signature) == 4:
            self.target = AE.AECreateDesc(AppleEvents.typeApplSignature, signature)
            self.target_signature = signature
        else:
            raise TypeError, "signature should be 4-char string or AEDesc"
        self.send_flags = AppleEvents.kAEWaitReply
        self.send_priority = AppleEvents.kAENormalPriority
        if timeout:
            self.send_timeout = timeout
        else:
            self.send_timeout = AppleEvents.kAEDefaultTimeout
        if start:
            self._start()

    def _start(self):
        """Start the application, if it is not running yet"""
        try:
            self.send('ascr', 'noop')
        except AE.Error:
            _launch(self.target_signature)
            for i in range(LAUNCH_MAX_WAIT_TIME):
                try:
                    self.send('ascr', 'noop')
                except AE.Error:
                    pass
                else:
                    break
                time.sleep(1)

    def start(self):
        """Deprecated, used _start()"""
        self._start()

    def newevent(self, code, subcode, parameters = {}, attributes = {}):
        """Create a complete structure for an apple event"""

        event = AE.AECreateAppleEvent(code, subcode, self.target,
                  AppleEvents.kAutoGenerateReturnID, AppleEvents.kAnyTransactionID)
        packevent(event, parameters, attributes)
        return event

    def sendevent(self, event):
        """Send a pre-created appleevent, await the reply and unpack it"""
        if not self.__ensure_WMAvailable():
            raise RuntimeError, "No window manager access, cannot send AppleEvent"
        reply = event.AESend(self.send_flags, self.send_priority,
                                  self.send_timeout)
        parameters, attributes = unpackevent(reply, self._moduleName)
        return reply, parameters, attributes

    def send(self, code, subcode, parameters = {}, attributes = {}):
        """Send an appleevent given code/subcode/pars/attrs and unpack the reply"""
        return self.sendevent(self.newevent(code, subcode, parameters, attributes))

    #
    # The following events are somehow "standard" and don't seem to appear in any
    # suite...
    #
    def activate(self):
        """Send 'activate' command"""
        self.send('misc', 'actv')

    def _get(self, _object, asfile=None, _attributes={}):
        """_get: get data from an object
        Required argument: the object
        Keyword argument _attributes: AppleEvent attribute dictionary
        Returns: the data
        """
        _code = 'core'
        _subcode = 'getd'

        _arguments = {'----':_object}
        if asfile:
            _arguments['rtyp'] = mktype(asfile)

        _reply, _arguments, _attributes = self.send(_code, _subcode,
                _arguments, _attributes)
        if 'errn' in _arguments:
            raise Error, decodeerror(_arguments)

        if '----' in _arguments:
            return _arguments['----']
            if asfile:
                item.__class__ = asfile
            return item

    get = _get

    _argmap_set = {
        'to' : 'data',
    }

    def _set(self, _object, _attributes={}, **_arguments):
        """set: Set an object's data.
        Required argument: the object for the command
        Keyword argument to: The new value.
        Keyword argument _attributes: AppleEvent attribute dictionary
        """
        _code = 'core'
        _subcode = 'setd'

        keysubst(_arguments, self._argmap_set)
        _arguments['----'] = _object


        _reply, _arguments, _attributes = self.send(_code, _subcode,
                _arguments, _attributes)
        if _arguments.get('errn', 0):
            raise Error, decodeerror(_arguments)
        # XXXX Optionally decode result
        if '----' in _arguments:
            return _arguments['----']

    set = _set

    # Magic glue to allow suite-generated classes to function somewhat
    # like the "application" class in OSA.

    def __getattr__(self, name):
        if name in self._elemdict:
            cls = self._elemdict[name]
            return DelayedComponentItem(cls, None)
        if name in self._propdict:
            cls = self._propdict[name]
            return cls()
        raise AttributeError, name

# Tiny Finder class, for local use only

class _miniFinder(TalkTo):
    def open(self, _object, _attributes={}, **_arguments):
        """open: Open the specified object(s)
        Required argument: list of objects to open
        Keyword argument _attributes: AppleEvent attribute dictionary
        """
        _code = 'aevt'
        _subcode = 'odoc'

        if _arguments: raise TypeError, 'No optional args expected'
        _arguments['----'] = _object


        _reply, _arguments, _attributes = self.send(_code, _subcode,
                _arguments, _attributes)
        if 'errn' in _arguments:
            raise Error, decodeerror(_arguments)
        # XXXX Optionally decode result
        if '----' in _arguments:
            return _arguments['----']
#pass

_finder = _miniFinder('MACS')

def _launch(appfile):
    """Open a file thru the finder. Specify file by name or fsspec"""
    _finder.open(_application_file(('ID  ', appfile)))


class _application_file(ComponentItem):
    """application file - An application's file on disk"""
    want = 'appf'

_application_file._propdict = {
}
_application_file._elemdict = {
}

# Test program
# XXXX Should test more, really...

def test():
    target = AE.AECreateDesc('sign', 'quil')
    ae = AE.AECreateAppleEvent('aevt', 'oapp', target, -1, 0)
    print unpackevent(ae)
    raw_input(":")
    ae = AE.AECreateAppleEvent('core', 'getd', target, -1, 0)
    obj = Character(2, Word(1, Document(1)))
    print obj
    print repr(obj)
    packevent(ae, {'----': obj})
    params, attrs = unpackevent(ae)
    print params['----']
    raw_input(":")

if __name__ == '__main__':
    test()
    sys.exit(1)