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).
10 addpack
.addpack('Demo')
11 addpack
.addpack('bgen')
13 addpack
.addpack('evt')
14 #addpack.addpack('menu')
15 addpack
.addpack('win')
18 sys
.stdout
= sys
.stderr
22 from AppleEvents
import *
34 kHighLevelEvent
= 23 # Not defined anywhere for Python yet?
39 yield = MacOS
.EnableAppswitch(-1) # Disable Python's own "event handling"
41 echo
.mainloop(everyEvent
, 0)
43 MacOS
.EnableAppswitch(yield) # Let Python have a go at events
49 suites
= ['aevt', 'core']
53 for suite
in self
.suites
:
54 AE
.AEInstallEventHandler(suite
, typeWildCard
, self
.aehandler
)
58 self
.applemenu
= applemenu
= Menu
.NewMenu(self
.appleid
, "\024")
59 applemenu
.AppendMenu("All about echo...;(-")
60 applemenu
.AddResMenu('DRVR')
61 applemenu
.InsertMenu(0)
70 for suite
in self
.suites
:
71 AE
.AERemoveEventHandler(suite
, typeWildCard
)
73 def mainloop(self
, mask
= everyEvent
, timeout
= 60*60):
75 self
.dooneevent(mask
, timeout
)
77 def dooneevent(self
, mask
= everyEvent
, timeout
= 60*60):
78 got
, event
= Evt
.WaitNextEvent(mask
, timeout
)
80 self
.lowlevelhandler(event
)
82 def lowlevelhandler(self
, event
):
83 what
, message
, when
, where
, modifiers
= event
85 if what
== kHighLevelEvent
:
86 msg
= "High Level Event: %s %s" % \
87 (`
code(message
)`
, `
code(h |
(v
<<16))`
)
89 AE
.AEProcessAppleEvent(event
)
91 EasyDialogs
.Message(msg
+ "\015AEProcessAppleEvent error: %s" % str(err
))
94 EasyDialogs
.Message(msg
+ "\015OK!")
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
:
108 EasyDialogs
.Message("Echo -- echo AppleEvents")
110 name
= self
.applemenu
.GetItem(item
)
112 elif what
<> autoKey
:
113 print "Event:", (eventname(what
), message
, when
, (h
, v
), modifiers
)
114 ## MacOS.HandleEvent(event)
116 def aehandler(self
, request
, reply
):
118 parameters
, attributes
= aetools
.unpackevent(request
)
119 print "class =", `attributes
['evcl'].type`
,
120 print "id =", `attributes
['evid'].type`
122 keys
= parameters
.keys()
125 print "%s: %.150s" % (`key`
, `parameters
[key
]`
)
126 print " :", str(parameters
[key
])
128 keys
= attributes
.keys()
131 print "%s: %.150s" % (`key`
, `attributes
[key
]`
)
132 aetools
.packevent(reply
, parameters
)
138 mouseDown
: 'mouseDown',
140 updateEvt
: 'updateEvt',
142 activateEvt
: 'activateEvt',
147 if _eventnames
.has_key(what
): return _eventnames
[what
]
151 "Convert a long int to the 4-character code it really is"
154 x
, c
= divmod(x
, 256)
159 if __name__
== '__main__':