Attachment 'argvemulator.py'
Download 1 #!/usr/bin/python
2
3 """argvemulator - create sys.argv from OSA events. Used by applets that
4 want unix-style arguments.
5 """
6
7
8 import sys
9 import traceback
10 import os
11
12 from Carbon import AE
13 from Carbon.AppleEvents import *
14 from Carbon import Evt
15 from Carbon import File
16 from Carbon.Events import *
17 import aetools
18
19 class ArgvCollector:
20
21 """A minimal FrameWork.Application-like class"""
22
23 def __init__(self):
24 self.quitting = 0
25 # Remove the funny -psn_xxx_xxx argument
26 if len(sys.argv) > 1 and sys.argv[1][:4] == '-psn':
27 del sys.argv[1]
28
29 AE.AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, self.__runapp)
30 AE.AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, self.__openfiles)
31
32 def close(self):
33 AE.AERemoveEventHandler(kCoreEventClass, kAEOpenApplication)
34 AE.AERemoveEventHandler(kCoreEventClass, kAEOpenDocuments)
35
36 def mainloop(self, mask = highLevelEventMask, timeout = 1*60):
37 # Note: this is not the right way to run an event loop in OSX or even
38 # "recent" versions of MacOS9. This is however code that has proven
39 # itself.
40 stoptime = Evt.TickCount() + timeout
41 while not self.quitting and Evt.TickCount() < stoptime:
42 self._dooneevent(mask, timeout)
43
44 if not self.quitting:
45 print "argvemulator: timeout waiting for arguments"
46
47 self.close()
48
49 def _dooneevent(self, mask = highLevelEventMask, timeout = 1*60):
50 got, event = Evt.WaitNextEvent(mask, timeout)
51 if got:
52 self._lowlevelhandler(event)
53
54 def _lowlevelhandler(self, event):
55 what, message, when, where, modifiers = event
56 h, v = where
57 if what == kHighLevelEvent:
58 try:
59 AE.AEProcessAppleEvent(event)
60 except AE.Error, err:
61 msg = "High Level Event: %r %r" % (hex(message), hex(h | (v<<16)))
62 print 'AE error: ', err
63 print 'in', msg
64 traceback.print_exc()
65 return
66 else:
67 print "Unhandled event:", event
68
69
70 def _quit(self):
71 self.quitting = 1
72
73 def __runapp(self, requestevent, replyevent):
74 self._quit()
75
76 def __openfiles(self, requestevent, replyevent):
77 try:
78 listdesc = requestevent.AEGetParamDesc(keyDirectObject, typeAEList)
79 for i in range(listdesc.AECountItems()):
80 aliasdesc = listdesc.AEGetNthDesc(i+1, typeAlias)[1]
81 alias = File.Alias(rawdata=aliasdesc.data)
82 fsref = alias.FSResolveAlias(None)[0]
83 pathname = fsref.as_pathname()
84 sys.argv.append(pathname)
85 except Exception, e:
86 print "argvemulator.py warning: can't unpack an open document event"
87 import traceback
88 traceback.print_exc()
89
90 self._quit()
91
92 if __name__ == '__main__':
93 ArgvCollector().mainloop()
94 print "sys.argv=", sys.argv
95 appname=os.path.basename(sys.argv[0])
96 appreal=os.path.splitext(sys.argv[0])[0]
97 if len(sys.argv) > 1:
98 dirname=os.path.dirname(sys.argv[1])
99 args=" ".join(sys.argv[1:])
100 a=os.popen2("cd %s;%s %s"%(dirname,appreal,args))
101 else:
102 a=os.popen2("cd $HOME;%s"%(appreal))
103
104
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.- [get | view] (2011-10-26 21:54:23, 0.0 KB) [[attachment:PkgInfo]]
- [get | view] (2011-10-26 21:54:23, 155.5 KB) [[attachment:QHelp.icns]]
- [get | view] (2011-10-26 21:54:23, 0.5 KB) [[attachment:QHelp.plist]]
- [get | view] (2011-10-26 21:54:23, 0.1 KB) [[attachment:applaunch.sh]]
- [get | view] (2011-10-26 21:54:23, 2.9 KB) [[attachment:argvemulator.py]]
- [get | view] (2011-10-26 21:54:23, 3.3 KB) [[attachment:boost.patch]]
- [get | view] (2011-10-26 21:54:23, 157.0 KB) [[attachment:boxer.icns]]
- [get | view] (2011-10-26 21:54:23, 1.3 KB) [[attachment:boxer.plist]]
- [get | view] (2011-10-26 21:54:23, 117.9 KB) [[attachment:checkslice.icns]]
- [get | view] (2011-10-26 21:54:23, 0.5 KB) [[attachment:checkslice.plist]]
- [get | view] (2011-10-26 21:54:23, 67.8 KB) [[attachment:ctfit.icns]]
- [get | view] (2011-10-26 21:54:23, 0.5 KB) [[attachment:ctfit.plist]]
- [get | view] (2011-10-26 21:54:23, 5.7 KB) [[attachment:darwin.jam]]
- [get | view] (2011-10-26 21:54:23, 213.8 KB) [[attachment:eman.icns]]
- [get | view] (2011-10-26 21:54:23, 0.5 KB) [[attachment:eman.plist]]
- [get | view] (2011-10-26 21:54:23, 0.2 KB) [[attachment:emaninit.sh]]
- [get | view] (2011-10-26 21:54:23, 172.3 KB) [[attachment:helixboxer.icns]]
- [get | view] (2011-10-26 21:54:23, 1.2 KB) [[attachment:helixboxer.plist]]
- [get | view] (2011-10-26 21:54:23, 126.1 KB) [[attachment:icon-box.icns]]
- [get | view] (2011-10-26 21:54:23, 134.2 KB) [[attachment:icon-dm3.icns]]
- [get | view] (2011-10-26 21:54:23, 185.8 KB) [[attachment:icon-hed.icns]]
- [get | view] (2011-10-26 21:54:23, 192.4 KB) [[attachment:icon-img.icns]]
- [get | view] (2011-10-26 21:54:23, 150.6 KB) [[attachment:icon-lst.icns]]
- [get | view] (2011-10-26 21:54:23, 230.8 KB) [[attachment:icon-mrc.icns]]
- [get | view] (2011-10-26 21:54:23, 0.3 KB) [[attachment:init.EMAN.csh]]
- [get | view] (2011-10-26 21:54:23, 0.2 KB) [[attachment:init.EMAN.sh]]
- [get | view] (2011-10-26 21:54:23, 58.2 KB) [[attachment:jpeg-v6b-patch.tar.gz]]
- [get | view] (2011-10-26 21:54:23, 0.7 KB) [[attachment:jpegpatch.zip]]
- [get | view] (2011-10-26 21:54:23, 53.6 KB) [[attachment:libtool135update.zip]]
- [get | view] (2011-10-26 21:54:23, 3.0 KB) [[attachment:makedist.sh]]
- [get | view] (2011-10-26 21:54:23, 203.1 KB) [[attachment:qindex.icns]]
- [get | view] (2011-10-26 21:54:23, 1.0 KB) [[attachment:qindex.plist]]
- [get | view] (2011-10-26 21:54:23, 128.2 KB) [[attachment:qplot.icns]]
- [get | view] (2011-10-26 21:54:23, 0.5 KB) [[attachment:qplot.plist]]
- [get | view] (2011-10-26 21:54:23, 182.7 KB) [[attachment:qsegment.icns]]
- [get | view] (2011-10-26 21:54:23, 0.5 KB) [[attachment:qsegment.plist]]
- [get | view] (2011-10-26 21:54:23, 3.8 KB) [[attachment:qt3mac.patch.txt]]
- [get | view] (2011-10-26 21:54:23, 2.6 KB) [[attachment:qt3mac.patch2.txt]]
- [get | view] (2011-10-26 21:54:23, 112.3 KB) [[attachment:triplot.icns]]
- [get | view] (2011-10-26 21:54:23, 0.5 KB) [[attachment:triplot.plist]]
- [get | view] (2011-10-26 21:54:23, 171.9 KB) [[attachment:v2.icns]]
- [get | view] (2011-10-26 21:54:23, 2.3 KB) [[attachment:v2.plist]]
- [get | view] (2011-10-26 21:54:23, 175.1 KB) [[attachment:v4.icns]]
- [get | view] (2011-10-26 21:54:23, 0.8 KB) [[attachment:v4.plist]]
You are not allowed to attach a file to this page.