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
|
# The oldest AppleEvent test program.
# Its function has been overtaken by echo.py and tell.py.
import AE
from AppleEvents import *
import Evt
from Events import *
import struct
import aetools
import macfs
import sys
import MacOS
MacOS.SchedParams(1, 0)
def aehandler(request, reply):
tosend = []
print 'request:', aetools.unpackevent(request)
param = request.AEGetParamDesc(keyDirectObject, typeWildCard)
if param.type == typeAEList:
n = param.AECountItems()
print 'List has', n, 'items'
for i in range(1, 1+n):
type, item = param.AEGetNthDesc(i, typeFSS)
data = item.data
print 'item', i, ':', type, item.type, len(data), 'bytes'
vol, dir, fnlen = struct.unpack('hlb', data[:7])
filename = data[7:7+fnlen]
print 'vol:', vol, '; dir:', dir, '; filename:', `filename`
print 'full path:', macfs.FSSpec((vol,dir,filename)).as_pathname()
tosend.append(item)
else:
pass
print 'param:', (param.type, param.data[:20]), param.data[20:] and '...'
if tosend:
passtothink(tosend)
def passtothink(list):
target = AE.AECreateDesc(typeApplSignature, 'KAHL')
event = AE.AECreateAppleEvent(kCoreEventClass,
kAEOpenDocuments,
target,
kAutoGenerateReturnID,
kAnyTransactionID)
aetools.packevent(event, {keyDirectObject: list})
reply = event.AESend(kAENoReply | kAEAlwaysInteract | kAECanSwitchLayer,
kAENormalPriority,
kAEDefaultTimeout)
#print "Reply:", aetools.unpackevent(reply)
return
event = AE.AECreateAppleEvent(kCoreEventClass,
kAEOpenApplication,
target,
kAutoGenerateReturnID,
kAnyTransactionID)
reply = event.AESend(kAENoReply | kAEAlwaysInteract | kAECanSwitchLayer,
kAENormalPriority,
kAEDefaultTimeout)
def unihandler(req, rep):
print 'unihandler'
aehandler(req, rep)
quit = 0
def quithandler(req, rep):
global quit
quit = 1
def corehandler(req, rep):
print 'core event!'
parameters, attributes = aetools.unpackevent(req)
print "event class =", attributes['evcl']
print "event id =", attributes['evid']
print 'parameters:', parameters
# echo the arguments, to see how Script Editor formats them
aetools.packevent(rep, parameters)
def wildhandler(req, rep):
print 'wildcard event!'
parameters, attributes = aetools.unpackevent(req)
print "event class =", attributes['evcl']
print "event id =", attributes['evid']
print 'parameters:', parameters
AE.AEInstallEventHandler(typeAppleEvent, kAEOpenApplication, aehandler)
AE.AEInstallEventHandler(typeAppleEvent, kAEOpenDocuments, aehandler)
AE.AEInstallEventHandler(typeAppleEvent, kAEPrintDocuments, aehandler)
AE.AEInstallEventHandler(typeAppleEvent, kAEQuitApplication, quithandler)
AE.AEInstallEventHandler(typeAppleEvent, typeWildCard, unihandler)
AE.AEInstallEventHandler('core', typeWildCard, corehandler)
#AE.AEInstallEventHandler(typeWildCard, typeWildCard, wildhandler)
def main():
global quit
quit = 0
while not quit:
ok, e = Evt.WaitNextEvent(-1, 60)
if ok:
print 'Event:', e
if e[0] == 23: # kHighLevelEvent
AE.AEProcessAppleEvent(e)
elif e[0] == keyDown and chr(e[1]&0xff) == '.' and e[4]&cmdKey:
raise KeyboardInterrupt, "Command-Period"
else:
MacOS.HandleEvent(e)
if __name__ == '__main__':
main()
print "This module is obsolete -- use echo.py or tell.py ..."
|