Hi, For those of you familiar with handling command line arguments with an NSApplication, I am requesting a reality-check whether I am understanding the following problem correctly, and whether the proposed solution makes sense. Please reply directly to me off-line unless you believe the answer to be of public value, thanks!
I have recently converted an application which processes command line arguments to call NSApplicationMain when it starts up, and defer command line processing until later in a separate thread. The deferred command line processing still works; however, I am seeing a new behavior where, in some circumstances, NSApplication is passing certain arguments to openFiles, which is undesirable. For example, there may be a command line argument '-logfile /foo/bar', where '/foo/ bar' is also being passed to openFiles. My proposed solution is to detect when an argument is being passed to openFiles during startup (i.e., when the arguments are being processed), and filter it out. I do this by using a boolean flag which is initialized to false and set to true when applicationDidFinishLaunching is called on my delegate. If openFiles is called when sIsRunning is false, I perform this extra check if it's on the command line and ignore it if it is. namespace { bool sIsRunning = false; }; bool isFileNameCommandLineArgument(NSString *fileName) { bool found = false; // If we are called while starting up, we need to filter out any fileNames which match a // command line argument. This is because -display and -logfile parameters will cause NSApplication // to call handleOpenDocument, which is not the desired behavior. // Once started up, we don't attempt to filter. if (!sIsRunning) { const char* fileNameCStr = [fileName cStringUsingEncoding: NSASCIIStringEncoding]; char **argv = *_NSGetArgv(); for (int i = 0; argv[i] != NULL; ++i) { // Only check non-zero length non-leading-dash arguments if (argv[i][0] > 0 && argv[i][0] != '-' && strcmp(fileNameCStr, argv[i]) == 0) { found = true; } } } return found; } In my application delegate, implement applicationDidFinishLaunching and filter file names in openFiles: - (void) applicationDidFinishLaunching:(NSNotification *)aNotification { sIsRunning = true; } - (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames { NSUInteger i; for( i = 0; i < [filenames count]; i++) { NSString *fileName = [filenames objectAtIndex: i]; if (!isFileNameCommandLineArgument(fileName)) { handleOpenDocument(fileName); } } } This appears to resolve the problem. Did I understand the problem correctly, and is this the best way to keep openFiles from opening files which happen to be passed as command line arguments? Are there other avenues/methods I should explore? - Brian Mac Developer The MathWorks, Inc. _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com