1

I'm using NSTask in xcode to try and find the version of node js the computer is using. Here's my code:

NSString *runCommand(NSString *commandToRun)
{
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/bash"];

NSArray *arguments = [NSArray arrayWithObjects:
                      @"-c" ,
                      [NSString stringWithFormat:@"%@", commandToRun],
                      nil];
NSLog(@"run command: %@",commandToRun);
[task setArguments: arguments];

NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];

NSFileHandle *file;
file = [pipe fileHandleForReading];

[task launch];

NSData *data;
data = [file readDataToEndOfFile];

NSString *output;
output = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"output %@",output);
return output;
}
- (IBAction)start_sapbot:(id)sender {
  NSString *node = @"node --version";
  NSString *git = @"git --version";

NSString *nodeCheck = runCommand(node);



}

So I already have node on my computer (and I even went in bin/bash and tried node --version and it showed a version).

But when I run this on xcode, I get /bin/bash: node: command not found

Just wondering how I fix this

1 Answers1

0

Add folder who contains node in PATH environment of the NSTask obj.

Something like this if you install node using macport :

NSDictionary * envDict = [NSProcessInfo processInfo].environment;
NSString * currentPath = [envDict objectForKey:@"PATH"];//Need nil check
NSString * newPath = [NSString stringWithFormat:@"/opt/local/bin:%@", currentPath];
NSMutableDictionary * myEnv = [NSMutableDictionary dictionaryWithDictionary:envDict];
[myEnv setObject:editValPath forKey:@"PATH"];

task.environment = myEnv;
MartinV
  • 1
  • 1