2 An application that responds to three types of apple event:
3 'pyth'/'EXEC': execute direct parameter as Python
5 'aevt', 'odoc': perform python scripts
7 Copyright © 1996, Just van Rossum, Letterror
22 from Events
import charCodeMask
, cmdKey
28 modulefilename
= dummyfunc
.func_code
.co_filename
30 def Interact(timeout
= 50000000): # timeout after 10 days...
31 AE
.AEInteractWithUser(timeout
)
34 class PythonSlave(FrameWork
.Application
):
36 FrameWork
.Application
.__init
__(self
)
37 AE
.AEInstallEventHandler('pyth', 'EXEC', ExecHandler
)
38 AE
.AEInstallEventHandler('aevt', 'quit', QuitHandler
)
39 AE
.AEInstallEventHandler('aevt', 'odoc', OpenDocumentHandler
)
41 def makeusermenus(self
):
42 self
.filemenu
= m
= FrameWork
.Menu(self
.menubar
, "File")
43 self
._quititem
= FrameWork
.MenuItem(m
, "Quit", "Q", self
._quit
)
45 def do_kHighLevelEvent(self
, event
):
46 (what
, message
, when
, where
, modifiers
) = event
48 AE
.AEProcessAppleEvent(event
)
49 except AE
.Error
, detail
:
50 print "Apple Event was not handled, error:", detail
52 def do_key(self
, event
):
53 (what
, message
, when
, where
, modifiers
) = event
54 c
= chr(message
& charCodeMask
)
55 if modifiers
& cmdKey
and c
== '.':
57 FrameWork
.Application
.do_key(self
, event
)
59 def idle(self
, event
):
62 def quit(self
, *args
):
65 def getabouttext(self
):
66 return "About PythonSlaveŠ"
68 def do_about(self
, id, item
, window
, event
):
69 EasyDialogs
.Message("PythonSlave " + __version__
+ "\rCopyright © 1996, Letterror, JvR")
72 def ExecHandler(theAppleEvent
, theReply
):
73 parameters
, args
= aetools
.unpackevent(theAppleEvent
)
74 if parameters
.has_key('----'):
75 if parameters
.has_key('NAME'):
76 print '--- executing "' + parameters
['NAME'] + '" ---'
78 print '--- executing "<unknown>" ---'
79 stuff
= parameters
['----']
80 MyExec(stuff
+ "\n") # execute input
85 stuff
= string
.splitfields(stuff
, '\r') # convert return chars
86 stuff
= string
.joinfields(stuff
, '\n') # to newline chars
88 saveyield
= MacOS
.EnableAppswitch(1)
92 MacOS
.EnableAppswitch(saveyield
)
94 MacOS
.EnableAppswitch(saveyield
)
96 def OpenDocumentHandler(theAppleEvent
, theReply
):
97 parameters
, args
= aetools
.unpackevent(theAppleEvent
)
98 docs
= parameters
['----']
99 if type(docs
) <> ListType
:
102 fss
, a
= doc
.Resolve()
103 path
= fss
.as_pathname()
104 if path
<> modulefilename
:
108 def MyExecFile(path
):
109 saveyield
= MacOS
.EnableAppswitch(1)
111 os
.chdir(os
.path
.split(path
)[0])
112 print '--- Executing file "' + os
.path
.split(path
)[1] + '"'
114 execfile(path
, {"__name__": "__main__"})
116 traceback
.print_exc()
117 MacOS
.EnableAppswitch(saveyield
)
118 MacOS
.EnableAppswitch(saveyield
)
122 def QuitHandler(theAppleEvent
, theReply
):
127 slave
= PythonSlave()
128 print "PythonSlave", __version__
, "ready."