1 | """MiniAEFrame - A minimal AppleEvent Application framework.
|
---|
2 |
|
---|
3 | There are two classes:
|
---|
4 | AEServer -- a mixin class offering nice AE handling.
|
---|
5 | MiniApplication -- a very minimal alternative to FrameWork.py,
|
---|
6 | only suitable for the simplest of AppleEvent servers.
|
---|
7 | """
|
---|
8 |
|
---|
9 | import sys
|
---|
10 | import traceback
|
---|
11 | import MacOS
|
---|
12 | from Carbon import AE
|
---|
13 | from Carbon.AppleEvents import *
|
---|
14 | from Carbon import Evt
|
---|
15 | from Carbon.Events import *
|
---|
16 | from Carbon import Menu
|
---|
17 | from Carbon import Win
|
---|
18 | from Carbon.Windows import *
|
---|
19 | from Carbon import Qd
|
---|
20 |
|
---|
21 | import aetools
|
---|
22 | import EasyDialogs
|
---|
23 |
|
---|
24 | kHighLevelEvent = 23 # Not defined anywhere for Python yet?
|
---|
25 |
|
---|
26 |
|
---|
27 | class MiniApplication:
|
---|
28 |
|
---|
29 | """A minimal FrameWork.Application-like class"""
|
---|
30 |
|
---|
31 | def __init__(self):
|
---|
32 | self.quitting = 0
|
---|
33 | # Initialize menu
|
---|
34 | self.appleid = 1
|
---|
35 | self.quitid = 2
|
---|
36 | Menu.ClearMenuBar()
|
---|
37 | self.applemenu = applemenu = Menu.NewMenu(self.appleid, "\024")
|
---|
38 | applemenu.AppendMenu("%s;(-" % self.getaboutmenutext())
|
---|
39 | if MacOS.runtimemodel == 'ppc':
|
---|
40 | applemenu.AppendResMenu('DRVR')
|
---|
41 | applemenu.InsertMenu(0)
|
---|
42 | self.quitmenu = Menu.NewMenu(self.quitid, "File")
|
---|
43 | self.quitmenu.AppendMenu("Quit")
|
---|
44 | self.quitmenu.SetItemCmd(1, ord("Q"))
|
---|
45 | self.quitmenu.InsertMenu(0)
|
---|
46 | Menu.DrawMenuBar()
|
---|
47 |
|
---|
48 | def __del__(self):
|
---|
49 | self.close()
|
---|
50 |
|
---|
51 | def close(self):
|
---|
52 | pass
|
---|
53 |
|
---|
54 | def mainloop(self, mask = everyEvent, timeout = 60*60):
|
---|
55 | while not self.quitting:
|
---|
56 | self.dooneevent(mask, timeout)
|
---|
57 |
|
---|
58 | def _quit(self):
|
---|
59 | self.quitting = 1
|
---|
60 |
|
---|
61 | def dooneevent(self, mask = everyEvent, timeout = 60*60):
|
---|
62 | got, event = Evt.WaitNextEvent(mask, timeout)
|
---|
63 | if got:
|
---|
64 | self.lowlevelhandler(event)
|
---|
65 |
|
---|
66 | def lowlevelhandler(self, event):
|
---|
67 | what, message, when, where, modifiers = event
|
---|
68 | h, v = where
|
---|
69 | if what == kHighLevelEvent:
|
---|
70 | msg = "High Level Event: %r %r" % (code(message), code(h | (v<<16)))
|
---|
71 | try:
|
---|
72 | AE.AEProcessAppleEvent(event)
|
---|
73 | except AE.Error, err:
|
---|
74 | print 'AE error: ', err
|
---|
75 | print 'in', msg
|
---|
76 | traceback.print_exc()
|
---|
77 | return
|
---|
78 | elif what == keyDown:
|
---|
79 | c = chr(message & charCodeMask)
|
---|
80 | if modifiers & cmdKey:
|
---|
81 | if c == '.':
|
---|
82 | raise KeyboardInterrupt, "Command-period"
|
---|
83 | if c == 'q':
|
---|
84 | if hasattr(MacOS, 'OutputSeen'):
|
---|
85 | MacOS.OutputSeen()
|
---|
86 | self.quitting = 1
|
---|
87 | return
|
---|
88 | elif what == mouseDown:
|
---|
89 | partcode, window = Win.FindWindow(where)
|
---|
90 | if partcode == inMenuBar:
|
---|
91 | result = Menu.MenuSelect(where)
|
---|
92 | id = (result>>16) & 0xffff # Hi word
|
---|
93 | item = result & 0xffff # Lo word
|
---|
94 | if id == self.appleid:
|
---|
95 | if item == 1:
|
---|
96 | EasyDialogs.Message(self.getabouttext())
|
---|
97 | elif item > 1 and hasattr(Menu, 'OpenDeskAcc'):
|
---|
98 | name = self.applemenu.GetMenuItemText(item)
|
---|
99 | Menu.OpenDeskAcc(name)
|
---|
100 | elif id == self.quitid and item == 1:
|
---|
101 | if hasattr(MacOS, 'OutputSeen'):
|
---|
102 | MacOS.OutputSeen()
|
---|
103 | self.quitting = 1
|
---|
104 | Menu.HiliteMenu(0)
|
---|
105 | return
|
---|
106 | # Anything not handled is passed to Python/SIOUX
|
---|
107 | if hasattr(MacOS, 'HandleEvent'):
|
---|
108 | MacOS.HandleEvent(event)
|
---|
109 | else:
|
---|
110 | print "Unhandled event:", event
|
---|
111 |
|
---|
112 | def getabouttext(self):
|
---|
113 | return self.__class__.__name__
|
---|
114 |
|
---|
115 | def getaboutmenutext(self):
|
---|
116 | return "About %s\311" % self.__class__.__name__
|
---|
117 |
|
---|
118 |
|
---|
119 | class AEServer:
|
---|
120 |
|
---|
121 | def __init__(self):
|
---|
122 | self.ae_handlers = {}
|
---|
123 |
|
---|
124 | def installaehandler(self, classe, type, callback):
|
---|
125 | AE.AEInstallEventHandler(classe, type, self.callback_wrapper)
|
---|
126 | self.ae_handlers[(classe, type)] = callback
|
---|
127 |
|
---|
128 | def close(self):
|
---|
129 | for classe, type in self.ae_handlers.keys():
|
---|
130 | AE.AERemoveEventHandler(classe, type)
|
---|
131 |
|
---|
132 | def callback_wrapper(self, _request, _reply):
|
---|
133 | _parameters, _attributes = aetools.unpackevent(_request)
|
---|
134 | _class = _attributes['evcl'].type
|
---|
135 | _type = _attributes['evid'].type
|
---|
136 |
|
---|
137 | if self.ae_handlers.has_key((_class, _type)):
|
---|
138 | _function = self.ae_handlers[(_class, _type)]
|
---|
139 | elif self.ae_handlers.has_key((_class, '****')):
|
---|
140 | _function = self.ae_handlers[(_class, '****')]
|
---|
141 | elif self.ae_handlers.has_key(('****', '****')):
|
---|
142 | _function = self.ae_handlers[('****', '****')]
|
---|
143 | else:
|
---|
144 | raise 'Cannot happen: AE callback without handler', (_class, _type)
|
---|
145 |
|
---|
146 | # XXXX Do key-to-name mapping here
|
---|
147 |
|
---|
148 | _parameters['_attributes'] = _attributes
|
---|
149 | _parameters['_class'] = _class
|
---|
150 | _parameters['_type'] = _type
|
---|
151 | if _parameters.has_key('----'):
|
---|
152 | _object = _parameters['----']
|
---|
153 | del _parameters['----']
|
---|
154 | # The try/except that used to be here can mask programmer errors.
|
---|
155 | # Let the program crash, the programmer can always add a **args
|
---|
156 | # to the formal parameter list.
|
---|
157 | rv = _function(_object, **_parameters)
|
---|
158 | else:
|
---|
159 | #Same try/except comment as above
|
---|
160 | rv = _function(**_parameters)
|
---|
161 |
|
---|
162 | if rv == None:
|
---|
163 | aetools.packevent(_reply, {})
|
---|
164 | else:
|
---|
165 | aetools.packevent(_reply, {'----':rv})
|
---|
166 |
|
---|
167 |
|
---|
168 | def code(x):
|
---|
169 | "Convert a long int to the 4-character code it really is"
|
---|
170 | s = ''
|
---|
171 | for i in range(4):
|
---|
172 | x, c = divmod(x, 256)
|
---|
173 | s = chr(c) + s
|
---|
174 | return s
|
---|
175 |
|
---|
176 | class _Test(AEServer, MiniApplication):
|
---|
177 | """Mini test application, handles required events"""
|
---|
178 |
|
---|
179 | def __init__(self):
|
---|
180 | MiniApplication.__init__(self)
|
---|
181 | AEServer.__init__(self)
|
---|
182 | self.installaehandler('aevt', 'oapp', self.open_app)
|
---|
183 | self.installaehandler('aevt', 'quit', self.quit)
|
---|
184 | self.installaehandler('****', '****', self.other)
|
---|
185 | self.mainloop()
|
---|
186 |
|
---|
187 | def quit(self, **args):
|
---|
188 | self._quit()
|
---|
189 |
|
---|
190 | def open_app(self, **args):
|
---|
191 | pass
|
---|
192 |
|
---|
193 | def other(self, _object=None, _class=None, _type=None, **args):
|
---|
194 | print 'AppleEvent', (_class, _type), 'for', _object, 'Other args:', args
|
---|
195 |
|
---|
196 |
|
---|
197 | if __name__ == '__main__':
|
---|
198 | _Test()
|
---|