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 sys
.stdout
= sys
.stderr
14 from AppleEvents
import *
26 kHighLevelEvent
= 23 # Not defined anywhere for Python yet?
29 err
= AE
.AEInteractWithUser(kAEDefaultTimeout
)
32 EasyDialogs
.Message(str)
36 saveparams
= MacOS
.SchedParams(0, 0) # Disable Python's own "event handling"
38 echo
.mainloop(everyEvent
, 0)
40 apply(MacOS
.SchedParams
, saveparams
) # Let Python have a go at events
46 #suites = ['aevt', 'core', 'reqd']
51 for suite
in self
.suites
:
52 AE
.AEInstallEventHandler(suite
, typeWildCard
, self
.aehandler
)
53 print (suite
, typeWildCard
, self
.aehandler
)
57 self
.applemenu
= applemenu
= Menu
.NewMenu(self
.appleid
, "\024")
58 applemenu
.AppendMenu("All about echo...;(-")
59 applemenu
.AppendResMenu('DRVR')
60 applemenu
.InsertMenu(0)
69 for suite
in self
.suites
:
70 AE
.AERemoveEventHandler(suite
, typeWildCard
)
72 def mainloop(self
, mask
= everyEvent
, timeout
= 60*60):
74 self
.dooneevent(mask
, timeout
)
76 def dooneevent(self
, mask
= everyEvent
, timeout
= 60*60):
77 got
, event
= Evt
.WaitNextEvent(mask
, timeout
)
79 self
.lowlevelhandler(event
)
81 def lowlevelhandler(self
, event
):
82 what
, message
, when
, where
, modifiers
= event
84 if what
== kHighLevelEvent
:
85 msg
= "High Level Event: %s %s" % \
86 (`
code(message
)`
, `
code(h |
(v
<<16))`
)
88 AE
.AEProcessAppleEvent(event
)
90 mymessage(msg
+ "\015AEProcessAppleEvent error: %s" % str(err
))
93 mymessage(msg
+ "\015OK!")
95 c
= chr(message
& charCodeMask
)
96 if c
== '.' and modifiers
& cmdKey
:
97 raise KeyboardInterrupt, "Command-period"
98 MacOS
.HandleEvent(event
)
99 elif what
== mouseDown
:
100 partcode
, window
= Win
.FindWindow(where
)
101 if partcode
== inMenuBar
:
102 result
= Menu
.MenuSelect(where
)
103 id = (result
>>16) & 0xffff # Hi word
104 item
= result
& 0xffff # Lo word
105 if id == self
.appleid
:
107 mymessage("Echo -- echo AppleEvents")
109 name
= self
.applemenu
.GetItem(item
)
111 elif what
<> autoKey
:
112 print "Event:", (eventname(what
), message
, when
, (h
, v
), modifiers
)
113 ## MacOS.HandleEvent(event)
115 def aehandler(self
, request
, reply
):
117 parameters
, attributes
= aetools
.unpackevent(request
)
118 print "class =", `attributes
['evcl'].type`
,
119 print "id =", `attributes
['evid'].type`
121 keys
= parameters
.keys()
124 print "%s: %.150s" % (`key`
, `parameters
[key
]`
)
125 print " :", str(parameters
[key
])
127 keys
= attributes
.keys()
130 print "%s: %.150s" % (`key`
, `attributes
[key
]`
)
131 aetools
.packevent(reply
, parameters
)
137 mouseDown
: 'mouseDown',
139 updateEvt
: 'updateEvt',
141 activateEvt
: 'activateEvt',
146 if _eventnames
.has_key(what
): return _eventnames
[what
]
150 "Convert a long int to the 4-character code it really is"
153 x
, c
= divmod(x
, 256)
158 if __name__
== '__main__':