Guido van Rossum | 3155923 | 1995-02-05 16:59:27 +0000 | [diff] [blame^] | 1 | """'echo' -- an AppleEvent handler which handles all events the same. |
| 2 | |
| 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 | """ |
| 8 | |
| 9 | import AE |
| 10 | from AppleEvents import * |
| 11 | import Evt |
| 12 | from Events import * |
| 13 | import aetools |
| 14 | import sys |
| 15 | import MacOS |
| 16 | import traceback |
| 17 | |
| 18 | kHighLevelEvent = 23 # Not defined anywhere for Python yet? |
| 19 | |
| 20 | |
| 21 | def main(): |
| 22 | echo = EchoServer() |
| 23 | MacOS.EnableAppswitch(0) # Disable Python's own "event handling" |
| 24 | try: |
| 25 | echo.mainloop() |
| 26 | finally: |
| 27 | MacOS.EnableAppswitch(1) # Let Python have a go at events |
| 28 | echo.close() |
| 29 | |
| 30 | |
| 31 | class EchoServer: |
| 32 | |
| 33 | suites = ['aevt', 'core'] |
| 34 | |
| 35 | def __init__(self): |
| 36 | self.active = 0 |
| 37 | for suite in self.suites: |
| 38 | AE.AEInstallEventHandler(suite, typeWildCard, self.aehandler) |
| 39 | self.active = 1 |
| 40 | |
| 41 | def __del__(self): |
| 42 | self.close() |
| 43 | |
| 44 | def close(self): |
| 45 | if self.active: |
| 46 | self.active = 0 |
| 47 | for suite in self.suites: |
| 48 | AE.AERemoveEventHandler(suite, typeWildCard) |
| 49 | |
| 50 | def mainloop(self, mask = everyEvent, timeout = 60*60): |
| 51 | while 1: |
| 52 | got, event = Evt.WaitNextEvent(mask, timeout) |
| 53 | if got: |
| 54 | self.lowlevelhandler(event) |
| 55 | |
| 56 | def lowlevelhandler(self, event): |
| 57 | what, message, when, (h, v), modifiers = event |
| 58 | if what == kHighLevelEvent: |
| 59 | print "High Level Event:", `code(message)`, `code(h | (v<<16))` |
| 60 | try: |
| 61 | AE.AEProcessAppleEvent(event) |
| 62 | except AE.Error, msg: |
| 63 | print "AEProcessAppleEvent error:" |
| 64 | traceback.print_exc() |
| 65 | elif what == keyDown: |
| 66 | c = chr(message & charCodeMask) |
| 67 | if c == '.' and modifiers & cmdKey: |
| 68 | raise KeyboardInterrupt, "Command-period" |
| 69 | MacOS.HandleEvent(event) |
| 70 | elif what <> autoKey: |
| 71 | print "Event:", (eventname(what), message, when, (h, v), modifiers) |
| 72 | MacOS.HandleEvent(event) |
| 73 | |
| 74 | def aehandler(self, request, reply): |
| 75 | print "Apple Event", |
| 76 | parameters, attributes = aetools.unpackevent(request) |
| 77 | print "class =", `attributes['evcl'].type`, |
| 78 | print "id =", `attributes['evid'].type` |
| 79 | print "Parameters:" |
| 80 | keys = parameters.keys() |
| 81 | keys.sort() |
| 82 | for key in keys: |
| 83 | print "%s: %.150s" % (`key`, `parameters[key]`) |
| 84 | print " :", str(parameters[key]) |
| 85 | print "Attributes:" |
| 86 | keys = attributes.keys() |
| 87 | keys.sort() |
| 88 | for key in keys: |
| 89 | print "%s: %.150s" % (`key`, `attributes[key]`) |
| 90 | aetools.packevent(reply, parameters) |
| 91 | |
| 92 | |
| 93 | _eventnames = { |
| 94 | keyDown: 'keyDown', |
| 95 | autoKey: 'autoKey', |
| 96 | mouseDown: 'mouseDown', |
| 97 | mouseUp: 'mouseUp', |
| 98 | updateEvt: 'updateEvt', |
| 99 | diskEvt: 'diskEvt', |
| 100 | activateEvt: 'activateEvt', |
| 101 | osEvt: 'osEvt', |
| 102 | } |
| 103 | |
| 104 | def eventname(what): |
| 105 | if _eventnames.has_key(what): return _eventnames[what] |
| 106 | else: return `what` |
| 107 | |
| 108 | def code(x): |
| 109 | "Convert a long int to the 4-character code it really is" |
| 110 | s = '' |
| 111 | for i in range(4): |
| 112 | x, c = divmod(x, 256) |
| 113 | s = chr(c) + s |
| 114 | return s |
| 115 | |
| 116 | |
| 117 | if __name__ == '__main__': |
| 118 | main() |
| 119 | else: main() |