1 def _get_argvemulator():
2 """argvemulator - create sys.argv from OSA events. Used by applets that
3 want unix-style arguments.
9 from Carbon
.AppleEvents
import kCoreEventClass
, kAEOpenApplication
, \
10 kAEOpenDocuments
, keyDirectObject
, typeAEList
, typeAlias
11 from Carbon
import Evt
12 from Carbon
import File
13 from Carbon
.Events
import highLevelEventMask
, kHighLevelEvent
17 """A minimal FrameWork.Application-like class"""
22 AE
.AEInstallEventHandler(kCoreEventClass
, kAEOpenApplication
,
24 AE
.AEInstallEventHandler(kCoreEventClass
, kAEOpenDocuments
,
28 AE
.AERemoveEventHandler(kCoreEventClass
, kAEOpenApplication
)
29 AE
.AERemoveEventHandler(kCoreEventClass
, kAEOpenDocuments
)
31 def mainloop(self
, mask
= highLevelEventMask
, timeout
= 1*60):
32 # Note: this is not the right way to run an event loop in OSX or
33 # even "recent" versions of MacOS9. This is however code that has
36 # Remove the funny -psn_xxx_xxx argument
37 if len(sys
.argv
) > 1 and sys
.argv
[1][:4] == '-psn':
40 stoptime
= Evt
.TickCount() + timeout
41 while not self
.quitting
and Evt
.TickCount() < stoptime
:
42 self
._dooneevent
(mask
, timeout
)
45 print "argvemulator: timeout waiting for arguments"
49 def _dooneevent(self
, mask
= highLevelEventMask
, timeout
= 1*60):
50 got
, event
= Evt
.WaitNextEvent(mask
, timeout
)
52 self
._lowlevelhandler
(event
)
54 def _lowlevelhandler(self
, event
):
55 what
, message
, when
, where
, modifiers
= event
57 if what
== kHighLevelEvent
:
59 AE
.AEProcessAppleEvent(event
)
61 msg
= "High Level Event: %r %r" % (hex(message
),
63 print 'AE error: ', err
68 print "Unhandled event:", event
74 def __runapp(self
, requestevent
, replyevent
):
77 def __openfiles(self
, requestevent
, replyevent
):
79 listdesc
= requestevent
.AEGetParamDesc(keyDirectObject
,
81 for i
in range(listdesc
.AECountItems()):
82 aliasdesc
= listdesc
.AEGetNthDesc(i
+1, typeAlias
)[1]
83 alias
= File
.Alias(rawdata
=aliasdesc
.data
)
84 fsref
= alias
.FSResolveAlias(None)[0]
85 pathname
= fsref
.as_pathname()
86 sys
.argv
.append(pathname
)
88 print "argvemulator.py warning: can't unpack an open document event"
94 return ArgvCollector()
96 def _argv_emulation():
98 # only use if started by LaunchServices
99 for arg
in sys
.argv
[1:]:
100 if arg
.startswith('-psn'):
101 _get_argvemulator().mainloop()
107 def _argv_inject(argv
):
109 # only use if started by LaunchServices
110 if len(sys
.argv
) > 1 and sys
.argv
[1].startswith('-psn'):
112 elif len(sys
.argv
) == 2:
116 _argv_inject(['--repo'])
119 def _disable_linecache():
121 def fake_getline(*args
, **kwargs
):
123 linecache
.orig_getline
= linecache
.getline
124 linecache
.getline
= fake_getline
131 sys
.frozen
= 'macosx_app'
132 base
= os
.environ
['RESOURCEPATH']
133 site
.addsitedir(base
)
134 site
.addsitedir(os
.path
.join(base
, 'Python', 'site-packages'))
137 for script
in scripts
:
138 path
= os
.path
.join(base
, script
)
139 sys
.argv
[0] = __file__
= path
140 execfile(path
, globals(), globals())