Improved some error messages for command line processing.
[python/dscho.git] / Mac / Lib / test / tae.py
blob4789672494fb28c49f8e01caf74e91f0ec72a59b
1 # The oldest AppleEvent test program.
2 # Its function has been overtaken by echo.py and tell.py.
4 import AE
5 from AppleEvents import *
6 import Evt
7 from Events import *
8 import struct
9 import aetools
10 import macfs
11 import sys
12 import MacOS
14 MacOS.SchedParams(1, 0)
16 def aehandler(request, reply):
17 tosend = []
18 print 'request:', aetools.unpackevent(request)
19 param = request.AEGetParamDesc(keyDirectObject, typeWildCard)
20 if param.type == typeAEList:
21 n = param.AECountItems()
22 print 'List has', n, 'items'
23 for i in range(1, 1+n):
24 type, item = param.AEGetNthDesc(i, typeFSS)
25 data = item.data
26 print 'item', i, ':', type, item.type, len(data), 'bytes'
27 vol, dir, fnlen = struct.unpack('hlb', data[:7])
28 filename = data[7:7+fnlen]
29 print 'vol:', vol, '; dir:', dir, '; filename:', `filename`
30 print 'full path:', macfs.FSSpec((vol,dir,filename)).as_pathname()
31 tosend.append(item)
32 else:
33 pass
34 print 'param:', (param.type, param.data[:20]), param.data[20:] and '...'
35 if tosend:
36 passtothink(tosend)
39 def passtothink(list):
40 target = AE.AECreateDesc(typeApplSignature, 'KAHL')
41 event = AE.AECreateAppleEvent(kCoreEventClass,
42 kAEOpenDocuments,
43 target,
44 kAutoGenerateReturnID,
45 kAnyTransactionID)
46 aetools.packevent(event, {keyDirectObject: list})
47 reply = event.AESend(kAENoReply | kAEAlwaysInteract | kAECanSwitchLayer,
48 kAENormalPriority,
49 kAEDefaultTimeout)
50 #print "Reply:", aetools.unpackevent(reply)
51 return
52 event = AE.AECreateAppleEvent(kCoreEventClass,
53 kAEOpenApplication,
54 target,
55 kAutoGenerateReturnID,
56 kAnyTransactionID)
57 reply = event.AESend(kAENoReply | kAEAlwaysInteract | kAECanSwitchLayer,
58 kAENormalPriority,
59 kAEDefaultTimeout)
61 def unihandler(req, rep):
62 print 'unihandler'
63 aehandler(req, rep)
65 quit = 0
66 def quithandler(req, rep):
67 global quit
68 quit = 1
70 def corehandler(req, rep):
71 print 'core event!'
72 parameters, attributes = aetools.unpackevent(req)
73 print "event class =", attributes['evcl']
74 print "event id =", attributes['evid']
75 print 'parameters:', parameters
76 # echo the arguments, to see how Script Editor formats them
77 aetools.packevent(rep, parameters)
79 def wildhandler(req, rep):
80 print 'wildcard event!'
81 parameters, attributes = aetools.unpackevent(req)
82 print "event class =", attributes['evcl']
83 print "event id =", attributes['evid']
84 print 'parameters:', parameters
86 AE.AEInstallEventHandler(typeAppleEvent, kAEOpenApplication, aehandler)
87 AE.AEInstallEventHandler(typeAppleEvent, kAEOpenDocuments, aehandler)
88 AE.AEInstallEventHandler(typeAppleEvent, kAEPrintDocuments, aehandler)
89 AE.AEInstallEventHandler(typeAppleEvent, kAEQuitApplication, quithandler)
90 AE.AEInstallEventHandler(typeAppleEvent, typeWildCard, unihandler)
91 AE.AEInstallEventHandler('core', typeWildCard, corehandler)
92 #AE.AEInstallEventHandler(typeWildCard, typeWildCard, wildhandler)
95 def main():
96 global quit
97 quit = 0
98 while not quit:
99 ok, e = Evt.WaitNextEvent(-1, 60)
100 if ok:
101 print 'Event:', e
102 if e[0] == 23: # kHighLevelEvent
103 AE.AEProcessAppleEvent(e)
104 elif e[0] == keyDown and chr(e[1]&0xff) == '.' and e[4]&cmdKey:
105 raise KeyboardInterrupt, "Command-Period"
106 else:
107 MacOS.HandleEvent(e)
109 if __name__ == '__main__':
110 main()
112 print "This module is obsolete -- use echo.py or tell.py ..."