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 Carbon
.AppleEvents
import *
15 from Carbon
import Evt
16 from Carbon
.Events
import *
17 from Carbon
import Menu
18 from Carbon
import Dlg
19 from Carbon
import Win
20 from Carbon
.Windows
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
.InsertMenu(0)
68 for suite
in self
.suites
:
69 AE
.AERemoveEventHandler(suite
, typeWildCard
)
71 def mainloop(self
, mask
= everyEvent
, timeout
= 60*60):
73 self
.dooneevent(mask
, timeout
)
75 def dooneevent(self
, mask
= everyEvent
, timeout
= 60*60):
76 got
, event
= Evt
.WaitNextEvent(mask
, timeout
)
78 self
.lowlevelhandler(event
)
80 def lowlevelhandler(self
, event
):
81 what
, message
, when
, where
, modifiers
= event
83 if what
== kHighLevelEvent
:
84 msg
= "High Level Event: %s %s" % \
85 (`
code(message
)`
, `
code(h |
(v
<<16))`
)
87 AE
.AEProcessAppleEvent(event
)
89 mymessage(msg
+ "\015AEProcessAppleEvent error: %s" % str(err
))
92 mymessage(msg
+ "\015OK!")
94 c
= chr(message
& charCodeMask
)
95 if c
== '.' and modifiers
& cmdKey
:
96 raise KeyboardInterrupt, "Command-period"
97 MacOS
.HandleEvent(event
)
98 elif what
== mouseDown
:
99 partcode
, window
= Win
.FindWindow(where
)
100 if partcode
== inMenuBar
:
101 result
= Menu
.MenuSelect(where
)
102 id = (result
>>16) & 0xffff # Hi word
103 item
= result
& 0xffff # Lo word
104 if id == self
.appleid
:
106 mymessage("Echo -- echo AppleEvents")
107 elif what
<> autoKey
:
108 print "Event:", (eventname(what
), message
, when
, (h
, v
), modifiers
)
109 ## MacOS.HandleEvent(event)
111 def aehandler(self
, request
, reply
):
113 parameters
, attributes
= aetools
.unpackevent(request
)
114 print "class =", `attributes
['evcl'].type`
,
115 print "id =", `attributes
['evid'].type`
117 keys
= parameters
.keys()
120 print "%s: %.150s" % (`key`
, `parameters
[key
]`
)
121 print " :", str(parameters
[key
])
123 keys
= attributes
.keys()
126 print "%s: %.150s" % (`key`
, `attributes
[key
]`
)
127 aetools
.packevent(reply
, parameters
)
133 mouseDown
: 'mouseDown',
135 updateEvt
: 'updateEvt',
137 activateEvt
: 'activateEvt',
142 if _eventnames
.has_key(what
): return _eventnames
[what
]
146 "Convert a long int to the 4-character code it really is"
149 x
, c
= divmod(x
, 256)
154 if __name__
== '__main__':