Move setting of ioready 'wait' earlier in call chain, to
[python/dscho.git] / Mac / Lib / test / echo.py
blobf84e13baf44ec2bb373fefcb6749a984e667e498
1 """'echo' -- an AppleEvent handler which handles all events the same.
3 It replies to each event by echoing the parameter back to the client.
4 This is a good way to find out how the Script Editor formats AppleEvents,
5 especially to figure out all the different forms an object specifier
6 can have (without having to rely on Apple's implementation).
7 """
9 import sys
10 sys.stdout = sys.stderr
11 import traceback
12 import MacOS
13 from Carbon import AE
14 from Carbon.AppleEvents import *
15 from Carbon import Evt
16 from Carbon.Events import *
17 from Carbon import Menu
18 from Carbon import Dlg
19 from Carbon import Win
20 from Carbon.Windows import *
21 from Carbon import Qd
23 import aetools
24 import EasyDialogs
26 kHighLevelEvent = 23 # Not defined anywhere for Python yet?
28 def mymessage(str):
29 err = AE.AEInteractWithUser(kAEDefaultTimeout)
30 if err:
31 print str
32 EasyDialogs.Message(str)
34 def main():
35 echo = EchoServer()
36 saveparams = MacOS.SchedParams(0, 0) # Disable Python's own "event handling"
37 try:
38 echo.mainloop(everyEvent, 0)
39 finally:
40 apply(MacOS.SchedParams, saveparams) # Let Python have a go at events
41 echo.close()
44 class EchoServer:
46 #suites = ['aevt', 'core', 'reqd']
47 suites = ['****']
49 def __init__(self):
50 self.active = 0
51 for suite in self.suites:
52 AE.AEInstallEventHandler(suite, typeWildCard, self.aehandler)
53 print (suite, typeWildCard, self.aehandler)
54 self.active = 1
55 self.appleid = 1
56 Menu.ClearMenuBar()
57 self.applemenu = applemenu = Menu.NewMenu(self.appleid, "\024")
58 applemenu.AppendMenu("All about echo...;(-")
59 applemenu.InsertMenu(0)
60 Menu.DrawMenuBar()
62 def __del__(self):
63 self.close()
65 def close(self):
66 if self.active:
67 self.active = 0
68 for suite in self.suites:
69 AE.AERemoveEventHandler(suite, typeWildCard)
71 def mainloop(self, mask = everyEvent, timeout = 60*60):
72 while 1:
73 self.dooneevent(mask, timeout)
75 def dooneevent(self, mask = everyEvent, timeout = 60*60):
76 got, event = Evt.WaitNextEvent(mask, timeout)
77 if got:
78 self.lowlevelhandler(event)
80 def lowlevelhandler(self, event):
81 what, message, when, where, modifiers = event
82 h, v = where
83 if what == kHighLevelEvent:
84 msg = "High Level Event: %s %s" % \
85 (`code(message)`, `code(h | (v<<16))`)
86 try:
87 AE.AEProcessAppleEvent(event)
88 except AE.Error, err:
89 mymessage(msg + "\015AEProcessAppleEvent error: %s" % str(err))
90 traceback.print_exc()
91 else:
92 mymessage(msg + "\015OK!")
93 elif what == keyDown:
94 c = chr(message & charCodeMask)
95 if c == '.' and modifiers & cmdKey:
96 raise KeyboardInterrupt, "Command-period"
97 MacOS.HandleEvent(event)
98 elif what == mouseDown:
99 partcode, window = Win.FindWindow(where)
100 if partcode == inMenuBar:
101 result = Menu.MenuSelect(where)
102 id = (result>>16) & 0xffff # Hi word
103 item = result & 0xffff # Lo word
104 if id == self.appleid:
105 if item == 1:
106 mymessage("Echo -- echo AppleEvents")
107 elif what <> autoKey:
108 print "Event:", (eventname(what), message, when, (h, v), modifiers)
109 ## MacOS.HandleEvent(event)
111 def aehandler(self, request, reply):
112 print "Apple Event!"
113 parameters, attributes = aetools.unpackevent(request)
114 print "class =", `attributes['evcl'].type`,
115 print "id =", `attributes['evid'].type`
116 print "Parameters:"
117 keys = parameters.keys()
118 keys.sort()
119 for key in keys:
120 print "%s: %.150s" % (`key`, `parameters[key]`)
121 print " :", str(parameters[key])
122 print "Attributes:"
123 keys = attributes.keys()
124 keys.sort()
125 for key in keys:
126 print "%s: %.150s" % (`key`, `attributes[key]`)
127 aetools.packevent(reply, parameters)
130 _eventnames = {
131 keyDown: 'keyDown',
132 autoKey: 'autoKey',
133 mouseDown: 'mouseDown',
134 mouseUp: 'mouseUp',
135 updateEvt: 'updateEvt',
136 diskEvt: 'diskEvt',
137 activateEvt: 'activateEvt',
138 osEvt: 'osEvt',
141 def eventname(what):
142 if _eventnames.has_key(what): return _eventnames[what]
143 else: return `what`
145 def code(x):
146 "Convert a long int to the 4-character code it really is"
147 s = ''
148 for i in range(4):
149 x, c = divmod(x, 256)
150 s = chr(c) + s
151 return s
154 if __name__ == '__main__':
155 main()