(py-outdent-p): new function
[python/dscho.git] / Mac / Lib / test / echo.py
blob20b1d77832d9b1ea5cbc269c9564a87e78f88b3f
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 addpack
10 addpack.addpack('Demo')
11 addpack.addpack('bgen')
12 addpack.addpack('ae')
13 addpack.addpack('evt')
14 #addpack.addpack('menu')
15 addpack.addpack('win')
17 import sys
18 sys.stdout = sys.stderr
19 import traceback
20 import MacOS
21 import AE
22 from AppleEvents import *
23 import Evt
24 from Events import *
25 import Menu
26 import Dlg
27 import Win
28 from Windows import *
29 import Qd
31 import aetools
32 import EasyDialogs
34 kHighLevelEvent = 23 # Not defined anywhere for Python yet?
37 def main():
38 echo = EchoServer()
39 yield = MacOS.EnableAppswitch(-1) # Disable Python's own "event handling"
40 try:
41 echo.mainloop(everyEvent, 0)
42 finally:
43 MacOS.EnableAppswitch(yield) # Let Python have a go at events
44 echo.close()
47 class EchoServer:
49 suites = ['aevt', 'core']
51 def __init__(self):
52 self.active = 0
53 for suite in self.suites:
54 AE.AEInstallEventHandler(suite, typeWildCard, self.aehandler)
55 self.active = 1
56 self.appleid = 1
57 Menu.ClearMenuBar()
58 self.applemenu = applemenu = Menu.NewMenu(self.appleid, "\024")
59 applemenu.AppendMenu("All about echo...;(-")
60 applemenu.AddResMenu('DRVR')
61 applemenu.InsertMenu(0)
62 Menu.DrawMenuBar()
64 def __del__(self):
65 self.close()
67 def close(self):
68 if self.active:
69 self.active = 0
70 for suite in self.suites:
71 AE.AERemoveEventHandler(suite, typeWildCard)
73 def mainloop(self, mask = everyEvent, timeout = 60*60):
74 while 1:
75 self.dooneevent(mask, timeout)
77 def dooneevent(self, mask = everyEvent, timeout = 60*60):
78 got, event = Evt.WaitNextEvent(mask, timeout)
79 if got:
80 self.lowlevelhandler(event)
82 def lowlevelhandler(self, event):
83 what, message, when, where, modifiers = event
84 h, v = where
85 if what == kHighLevelEvent:
86 msg = "High Level Event: %s %s" % \
87 (`code(message)`, `code(h | (v<<16))`)
88 try:
89 AE.AEProcessAppleEvent(event)
90 except AE.Error, err:
91 EasyDialogs.Message(msg + "\015AEProcessAppleEvent error: %s" % str(err))
92 traceback.print_exc()
93 else:
94 EasyDialogs.Message(msg + "\015OK!")
95 elif what == keyDown:
96 c = chr(message & charCodeMask)
97 if c == '.' and modifiers & cmdKey:
98 raise KeyboardInterrupt, "Command-period"
99 MacOS.HandleEvent(event)
100 elif what == mouseDown:
101 partcode, window = Win.FindWindow(where)
102 if partcode == inMenuBar:
103 result = Menu.MenuSelect(where)
104 id = (result>>16) & 0xffff # Hi word
105 item = result & 0xffff # Lo word
106 if id == self.appleid:
107 if item == 1:
108 EasyDialogs.Message("Echo -- echo AppleEvents")
109 elif item > 1:
110 name = self.applemenu.GetItem(item)
111 Qd.OpenDeskAcc(name)
112 elif what <> autoKey:
113 print "Event:", (eventname(what), message, when, (h, v), modifiers)
114 ## MacOS.HandleEvent(event)
116 def aehandler(self, request, reply):
117 print "Apple Event",
118 parameters, attributes = aetools.unpackevent(request)
119 print "class =", `attributes['evcl'].type`,
120 print "id =", `attributes['evid'].type`
121 print "Parameters:"
122 keys = parameters.keys()
123 keys.sort()
124 for key in keys:
125 print "%s: %.150s" % (`key`, `parameters[key]`)
126 print " :", str(parameters[key])
127 print "Attributes:"
128 keys = attributes.keys()
129 keys.sort()
130 for key in keys:
131 print "%s: %.150s" % (`key`, `attributes[key]`)
132 aetools.packevent(reply, parameters)
135 _eventnames = {
136 keyDown: 'keyDown',
137 autoKey: 'autoKey',
138 mouseDown: 'mouseDown',
139 mouseUp: 'mouseUp',
140 updateEvt: 'updateEvt',
141 diskEvt: 'diskEvt',
142 activateEvt: 'activateEvt',
143 osEvt: 'osEvt',
146 def eventname(what):
147 if _eventnames.has_key(what): return _eventnames[what]
148 else: return `what`
150 def code(x):
151 "Convert a long int to the 4-character code it really is"
152 s = ''
153 for i in range(4):
154 x, c = divmod(x, 256)
155 s = chr(c) + s
156 return s
159 if __name__ == '__main__':
160 main()