blob: 616f7120820fa6034a526ad2a5d9c19880a92cb1 [file] [log] [blame]
Guido van Rossum31559231995-02-05 16:59:27 +00001"""'echo' -- an AppleEvent handler which handles all events the same.
2
3It replies to each event by echoing the parameter back to the client.
4This is a good way to find out how the Script Editor formats AppleEvents,
5especially to figure out all the different forms an object specifier
6can have (without having to rely on Apple's implementation).
7"""
8
Guido van Rossum97842951995-02-19 15:59:49 +00009import addpack
10addpack.addpack('Demo')
11addpack.addpack('bgen')
12addpack.addpack('ae')
13addpack.addpack('evt')
14
15import sys
16sys.stdout = sys.stderr
17import traceback
18import MacOS
Guido van Rossum31559231995-02-05 16:59:27 +000019import AE
20from AppleEvents import *
21import Evt
22from Events import *
23import aetools
Guido van Rossum31559231995-02-05 16:59:27 +000024
25kHighLevelEvent = 23 # Not defined anywhere for Python yet?
26
27
28def main():
29 echo = EchoServer()
30 MacOS.EnableAppswitch(0) # Disable Python's own "event handling"
31 try:
32 echo.mainloop()
33 finally:
34 MacOS.EnableAppswitch(1) # Let Python have a go at events
35 echo.close()
36
37
38class EchoServer:
39
40 suites = ['aevt', 'core']
41
42 def __init__(self):
43 self.active = 0
44 for suite in self.suites:
45 AE.AEInstallEventHandler(suite, typeWildCard, self.aehandler)
46 self.active = 1
47
48 def __del__(self):
49 self.close()
50
51 def close(self):
52 if self.active:
53 self.active = 0
54 for suite in self.suites:
55 AE.AERemoveEventHandler(suite, typeWildCard)
56
57 def mainloop(self, mask = everyEvent, timeout = 60*60):
58 while 1:
Guido van Rossum97842951995-02-19 15:59:49 +000059 self.dooneevent(mask, timeout)
60
61 def dooneevent(self, mask = everyEvent, timeout = 60*60):
Guido van Rossum31559231995-02-05 16:59:27 +000062 got, event = Evt.WaitNextEvent(mask, timeout)
63 if got:
64 self.lowlevelhandler(event)
65
66 def lowlevelhandler(self, event):
67 what, message, when, (h, v), modifiers = event
68 if what == kHighLevelEvent:
69 print "High Level Event:", `code(message)`, `code(h | (v<<16))`
70 try:
71 AE.AEProcessAppleEvent(event)
72 except AE.Error, msg:
73 print "AEProcessAppleEvent error:"
74 traceback.print_exc()
75 elif what == keyDown:
76 c = chr(message & charCodeMask)
77 if c == '.' and modifiers & cmdKey:
78 raise KeyboardInterrupt, "Command-period"
79 MacOS.HandleEvent(event)
80 elif what <> autoKey:
81 print "Event:", (eventname(what), message, when, (h, v), modifiers)
82 MacOS.HandleEvent(event)
83
84 def aehandler(self, request, reply):
85 print "Apple Event",
86 parameters, attributes = aetools.unpackevent(request)
87 print "class =", `attributes['evcl'].type`,
88 print "id =", `attributes['evid'].type`
89 print "Parameters:"
90 keys = parameters.keys()
91 keys.sort()
92 for key in keys:
93 print "%s: %.150s" % (`key`, `parameters[key]`)
94 print " :", str(parameters[key])
95 print "Attributes:"
96 keys = attributes.keys()
97 keys.sort()
98 for key in keys:
99 print "%s: %.150s" % (`key`, `attributes[key]`)
100 aetools.packevent(reply, parameters)
101
102
103_eventnames = {
104 keyDown: 'keyDown',
105 autoKey: 'autoKey',
106 mouseDown: 'mouseDown',
107 mouseUp: 'mouseUp',
108 updateEvt: 'updateEvt',
109 diskEvt: 'diskEvt',
110 activateEvt: 'activateEvt',
111 osEvt: 'osEvt',
112}
113
114def eventname(what):
115 if _eventnames.has_key(what): return _eventnames[what]
116 else: return `what`
117
118def code(x):
119 "Convert a long int to the 4-character code it really is"
120 s = ''
121 for i in range(4):
122 x, c = divmod(x, 256)
123 s = chr(c) + s
124 return s
125
126
127if __name__ == '__main__':
128 main()