| 
									
										
										
										
											2002-08-02 11:12:15 +00:00
										 |  |  | """argvemulator - create sys.argv from OSA events. Used by applets that
 | 
					
						
							|  |  |  | want unix-style arguments. | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-12 21:42:13 +00:00
										 |  |  | from warnings import warnpy3k | 
					
						
							|  |  |  | warnpy3k("In 3.x, the argvemulator module is removed.") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-08-02 11:12:15 +00:00
										 |  |  | import sys | 
					
						
							|  |  |  | import traceback | 
					
						
							|  |  |  | from Carbon import AE | 
					
						
							|  |  |  | from Carbon.AppleEvents import * | 
					
						
							|  |  |  | from Carbon import Evt | 
					
						
							| 
									
										
										
										
											2006-06-07 20:18:44 +00:00
										 |  |  | from Carbon import File | 
					
						
							| 
									
										
										
										
											2002-08-02 11:12:15 +00:00
										 |  |  | from Carbon.Events import * | 
					
						
							|  |  |  | import aetools | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ArgvCollector: | 
					
						
							| 
									
										
										
										
											2003-04-06 09:01:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  |     """A minimal FrameWork.Application-like class""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __init__(self): | 
					
						
							|  |  |  |         self.quitting = 0 | 
					
						
							|  |  |  |         # Remove the funny -psn_xxx_xxx argument | 
					
						
							|  |  |  |         if len(sys.argv) > 1 and sys.argv[1][:4] == '-psn': | 
					
						
							|  |  |  |             del sys.argv[1] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-07 20:18:44 +00:00
										 |  |  |         AE.AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, self.__runapp) | 
					
						
							|  |  |  |         AE.AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, self.__openfiles) | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def close(self): | 
					
						
							| 
									
										
										
										
											2006-06-07 20:18:44 +00:00
										 |  |  |         AE.AERemoveEventHandler(kCoreEventClass, kAEOpenApplication) | 
					
						
							|  |  |  |         AE.AERemoveEventHandler(kCoreEventClass, kAEOpenDocuments) | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def mainloop(self, mask = highLevelEventMask, timeout = 1*60): | 
					
						
							| 
									
										
										
										
											2006-06-07 20:18:44 +00:00
										 |  |  |         # Note: this is not the right way to run an event loop in OSX or even | 
					
						
							|  |  |  |         # "recent" versions of MacOS9. This is however code that has proven | 
					
						
							|  |  |  |         # itself. | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  |         stoptime = Evt.TickCount() + timeout | 
					
						
							|  |  |  |         while not self.quitting and Evt.TickCount() < stoptime: | 
					
						
							| 
									
										
										
										
											2006-06-07 20:18:44 +00:00
										 |  |  |             self._dooneevent(mask, timeout) | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-07 20:18:44 +00:00
										 |  |  |         if not self.quitting: | 
					
						
							|  |  |  |             print "argvemulator: timeout waiting for arguments" | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-07 20:18:44 +00:00
										 |  |  |         self.close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _dooneevent(self, mask = highLevelEventMask, timeout = 1*60): | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  |         got, event = Evt.WaitNextEvent(mask, timeout) | 
					
						
							|  |  |  |         if got: | 
					
						
							| 
									
										
										
										
											2006-06-07 20:18:44 +00:00
										 |  |  |             self._lowlevelhandler(event) | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-07 20:18:44 +00:00
										 |  |  |     def _lowlevelhandler(self, event): | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  |         what, message, when, where, modifiers = event | 
					
						
							|  |  |  |         h, v = where | 
					
						
							|  |  |  |         if what == kHighLevelEvent: | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 AE.AEProcessAppleEvent(event) | 
					
						
							|  |  |  |             except AE.Error, err: | 
					
						
							| 
									
										
										
										
											2004-02-12 17:35:32 +00:00
										 |  |  |                 msg = "High Level Event: %r %r" % (hex(message), hex(h | (v<<16))) | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  |                 print 'AE error: ', err | 
					
						
							|  |  |  |                 print 'in', msg | 
					
						
							|  |  |  |                 traceback.print_exc() | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             print "Unhandled event:", event | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-07 20:18:44 +00:00
										 |  |  |     def _quit(self): | 
					
						
							|  |  |  |         self.quitting = 1 | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-07 20:18:44 +00:00
										 |  |  |     def __runapp(self, requestevent, replyevent): | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  |         self._quit() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-07 20:18:44 +00:00
										 |  |  |     def __openfiles(self, requestevent, replyevent): | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             listdesc = requestevent.AEGetParamDesc(keyDirectObject, typeAEList) | 
					
						
							|  |  |  |             for i in range(listdesc.AECountItems()): | 
					
						
							|  |  |  |                 aliasdesc = listdesc.AEGetNthDesc(i+1, typeAlias)[1] | 
					
						
							|  |  |  |                 alias = File.Alias(rawdata=aliasdesc.data) | 
					
						
							|  |  |  |                 fsref = alias.FSResolveAlias(None)[0] | 
					
						
							|  |  |  |                 pathname = fsref.as_pathname() | 
					
						
							|  |  |  |                 sys.argv.append(pathname) | 
					
						
							|  |  |  |         except  Exception, e: | 
					
						
							|  |  |  |             print "argvemulator.py warning: can't unpack an open document event" | 
					
						
							|  |  |  |             import traceback | 
					
						
							|  |  |  |             traceback.print_exc() | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-07 20:18:44 +00:00
										 |  |  |         self._quit() | 
					
						
							| 
									
										
										
										
											2002-08-02 11:12:15 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							| 
									
										
										
										
											2003-06-21 14:41:32 +00:00
										 |  |  |     ArgvCollector().mainloop() | 
					
						
							|  |  |  |     print "sys.argv=", sys.argv |