On Apr 18, 2005, at 12:15 AM, David Darby wrote:
- (void)launchStudentVersion
{
NSTask* task = [[NSTask alloc] init];
NSMutableArray* args = [NSMutableArray array];
NSBundle* appBundle = [NSBundle mainBundle];
// set arguments
NSString* firstCmd;
NSString* cmd1 = @"/MyApp.app/Contents/MacOS/MyApp";
NSString* cmd2 = @"-student";
NSString* path = [[appBundle bundlePath] stringByDeletingLastPathComponent];
firstCmd = [path stringByAppendingString:cmd1];
[task setLaunchPath:firstCmd];
NSLog(firstCmd);
[args addObject:firstCmd];
[args addObject:cmd2];
[task setArguments:args];
[task launch];
[NSApp terminate:self];
}
This works as it should, ie when the application is opened from the Finder, it launches the application called "MyApp" which resides in the same directory using the command line option of -student which causes the application to run in "student" mode (with some options disabled). After launching MyApp, this small application politely quits.
However, when the launched application runs in Student mode, it strangely cannot access (due to insufficient privileges) any directory in the same folder.
Have you verified that the errors are definitely due to permissions, rather than "file not found"?
I ask because child processes inherit the working directory of the parent, which is *not* the same directory that the executable is found in. Nor is it always the same - when launched from Finder, it's "/", but when launched from Xcode it's the build directory.
There are two potential solutions to that: First, you could use full paths in your child process. That is, "/full/path/to/file.txt" instead of just "file.txt". Or, you could specify the working directory for the child process to use by calling NSTask's -setCurrentDirectoryPath: method before launching the child.
sherm--