Tuesday, January 14, 2003

Unfortunately, NSApplicationMain() -- the main entry point into a normal Cocoa app -- is broken in OS X 10.2. Specifically, NSApplicationMain() ignores the argv argument you pass into it and, instead, dips into NSProcessInfo to grab the list of arguments.

This causes hell in PyObjC because the first argument is the bootstrap Python script. Cocoa tries to open that as a document in the app, barfs, and that causes the NSDocument architecture to behave in a non-standard fashion.

Unfortunately, NSProcessInfo does not provide a means of changing the contents of its 'arguments' instance variable.

One evil hack later, problem fixed.

        // argv contains a char ** of the args to be passed to NSApplicationMain()
	  typedef struct {
	  } NSProcessInfoStruct;
	  // everything in this scope is evil and wrong.  It leaks, too.
	  NSMutableArray *args = [[NSMutableArray alloc] init];
	  NSProcessInfo *processInfo = [NSProcessInfo processInfo];
	  char **anArg = argv;
	  while(*anArg) {
	    [args addObject: [NSString stringWithUTF8String: *anArg]];
	  ((NSProcessInfoStruct *)processInfo)->arguments = args;

res = NSApplicationMain(argc, argv);

Needless to say, this kind of code really shouldn't be used unless it is unavoidable. If anyone knows of a way that I can avoid this hacque, let me know....
7:29:29 PM  pontificate    

Peter wrote to mention that cmd-alt-F takes one straight to the google search field in Safari. It also sticks the cursor in the search field in Mail.

Cool! Thanks!
11:15:23 AM  pontificate