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.

You are not allowed to attach a file to this page.