NSTask startup path is not available. It works in Xcode. Error indicated in Xcode

Ok There are several questions about stack overflows about this. This question was the only one that comes closest to mines but uses notifications.

The code is very simple. Create a new empty Mac OSX project and simply paste the following code into the applicationDidFinishLaunching: method applicationDidFinishLaunching: It should get the path to any executable file (in this case GIT).

 NSTask *aTask = [[NSTask alloc] init]; NSPipe *outputPipe = [NSPipe pipe]; NSPipe *errorPipe = [NSPipe pipe]; [aTask setStandardOutput: outputPipe]; [aTask setStandardError: errorPipe]; [aTask setArguments:[NSArray arrayWithObject:@"which"]]; [aTask setLaunchPath:@"/usr/bin/git"]; NSFileHandle *outputFileHandler = [outputPipe fileHandleForReading]; NSFileHandle *errorFileHandler = [errorPipe fileHandleForReading]; [aTask launch]; [aTask waitUntilExit]; // Task launched now just read and print the data NSData *data = [outputFileHandler readDataToEndOfFile]; NSString *outPutValue = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; NSData *errorData = [errorFileHandler readDataToEndOfFile]; NSString *errorValue = [[NSString alloc] initWithData:errorData encoding:NSUTF8StringEncoding]; NSLog(@"Error value: %@",errorValue); NSLog(@"Output Value: %@",outPutValue); 

This code sets up two reading channels and executes one command: which git .

If I run this in Xcode, I will get these results:

 Error value: "" Output Value: /usr/bin/git 

If I go to the build / Products / Debug folder and double-click the executable, I will send this message in the console application:

enter image description here

Question So, what is the problem please just don’t make an alternative solution ... I also want to know what the problem is .. thanks.

+7
source share
1 answer

OK, the answer to the stack overflow is obtained, but its distribution on various issues.

Here the question is asked β†’ Commands with NSTask , and here β†’ The path to NSTask is unavailable as well

But their answers on this date are unclear regarding the problem. This is only after reading the question from NSTask does not collect $ PATH from the user environment (the title of the question is misleading) and with these two answers NSTask does not collect $ PATH from the user environment and Find out the location of the executable in Cocoa , which I understood.

It seems that this is about setting up the NS Task or a custom shell (for example, ~ / .bashrc) so that the environment ($ PATH) is visible to NSTask.

Decision:

 [task setLaunchPath:@"/bin/bash"]; NSArray *args = [NSArray arrayWithObjects:@"-l", @"-c", @"which git", //Assuming git is the launch path you want to run nil]; [task setArguments: args]; 

However, this assumes that the user shell is always bash, and this will fail for others. Solve this by defining a shell.

 NSDictionary *environmentDict = [[NSProcessInfo processInfo] environment]; NSString *shellString = [environmentDict objectForKey:@"SHELL"]; 
+13
source

All Articles