| 1 | """argvemulator - create sys.argv from OSA events. Used by applets that
|
|---|
| 2 | want unix-style arguments.
|
|---|
| 3 | """
|
|---|
| 4 |
|
|---|
| 5 | import sys
|
|---|
| 6 | import traceback
|
|---|
| 7 | from Carbon import AE
|
|---|
| 8 | from Carbon.AppleEvents import *
|
|---|
| 9 | from Carbon import Evt
|
|---|
| 10 | from Carbon import File
|
|---|
| 11 | from Carbon.Events import *
|
|---|
| 12 | import aetools
|
|---|
| 13 |
|
|---|
| 14 | class ArgvCollector:
|
|---|
| 15 |
|
|---|
| 16 | """A minimal FrameWork.Application-like class"""
|
|---|
| 17 |
|
|---|
| 18 | def __init__(self):
|
|---|
| 19 | self.quitting = 0
|
|---|
| 20 | # Remove the funny -psn_xxx_xxx argument
|
|---|
| 21 | if len(sys.argv) > 1 and sys.argv[1][:4] == '-psn':
|
|---|
| 22 | del sys.argv[1]
|
|---|
| 23 |
|
|---|
| 24 | AE.AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, self.__runapp)
|
|---|
| 25 | AE.AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, self.__openfiles)
|
|---|
| 26 |
|
|---|
| 27 | def close(self):
|
|---|
| 28 | AE.AERemoveEventHandler(kCoreEventClass, kAEOpenApplication)
|
|---|
| 29 | AE.AERemoveEventHandler(kCoreEventClass, kAEOpenDocuments)
|
|---|
| 30 |
|
|---|
| 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 even
|
|---|
| 33 | # "recent" versions of MacOS9. This is however code that has proven
|
|---|
| 34 | # itself.
|
|---|
| 35 | stoptime = Evt.TickCount() + timeout
|
|---|
| 36 | while not self.quitting and Evt.TickCount() < stoptime:
|
|---|
| 37 | self._dooneevent(mask, timeout)
|
|---|
| 38 |
|
|---|
| 39 | if not self.quitting:
|
|---|
| 40 | print "argvemulator: timeout waiting for arguments"
|
|---|
| 41 |
|
|---|
| 42 | self.close()
|
|---|
| 43 |
|
|---|
| 44 | def _dooneevent(self, mask = highLevelEventMask, timeout = 1*60):
|
|---|
| 45 | got, event = Evt.WaitNextEvent(mask, timeout)
|
|---|
| 46 | if got:
|
|---|
| 47 | self._lowlevelhandler(event)
|
|---|
| 48 |
|
|---|
| 49 | def _lowlevelhandler(self, event):
|
|---|
| 50 | what, message, when, where, modifiers = event
|
|---|
| 51 | h, v = where
|
|---|
| 52 | if what == kHighLevelEvent:
|
|---|
| 53 | try:
|
|---|
| 54 | AE.AEProcessAppleEvent(event)
|
|---|
| 55 | except AE.Error, err:
|
|---|
| 56 | msg = "High Level Event: %r %r" % (hex(message), hex(h | (v<<16)))
|
|---|
| 57 | print 'AE error: ', err
|
|---|
| 58 | print 'in', msg
|
|---|
| 59 | traceback.print_exc()
|
|---|
| 60 | return
|
|---|
| 61 | else:
|
|---|
| 62 | print "Unhandled event:", event
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 | def _quit(self):
|
|---|
| 66 | self.quitting = 1
|
|---|
| 67 |
|
|---|
| 68 | def __runapp(self, requestevent, replyevent):
|
|---|
| 69 | self._quit()
|
|---|
| 70 |
|
|---|
| 71 | def __openfiles(self, requestevent, replyevent):
|
|---|
| 72 | try:
|
|---|
| 73 | listdesc = requestevent.AEGetParamDesc(keyDirectObject, typeAEList)
|
|---|
| 74 | for i in range(listdesc.AECountItems()):
|
|---|
| 75 | aliasdesc = listdesc.AEGetNthDesc(i+1, typeAlias)[1]
|
|---|
| 76 | alias = File.Alias(rawdata=aliasdesc.data)
|
|---|
| 77 | fsref = alias.FSResolveAlias(None)[0]
|
|---|
| 78 | pathname = fsref.as_pathname()
|
|---|
| 79 | sys.argv.append(pathname)
|
|---|
| 80 | except Exception, e:
|
|---|
| 81 | print "argvemulator.py warning: can't unpack an open document event"
|
|---|
| 82 | import traceback
|
|---|
| 83 | traceback.print_exc()
|
|---|
| 84 |
|
|---|
| 85 | self._quit()
|
|---|
| 86 |
|
|---|
| 87 | if __name__ == '__main__':
|
|---|
| 88 | ArgvCollector().mainloop()
|
|---|
| 89 | print "sys.argv=", sys.argv
|
|---|