0

I am developing a macOS app using Objective-C that I want to run some commands same like terminal. Actually I want to run YOLO command from my application. I am using NSTask class for this. When I run the command through code, on task launch, I am getting error "Couldn't open file cfg/coco.data". The same command works fine with terminal but not in my application.

Here is my code:

NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath:@"/bin/bash"];

NSString *commandToRun = @"Desktop/darknet/ && ./darknet detect cfg/yolo.cfg   yolo.weights data/dog.jpg";

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];
idrees
  • 1
  • 1
  • Possible duplicate of [How do I run an terminal command in a swift script? (e.g. xcodebuild)](http://stackoverflow.com/questions/26971240/how-do-i-run-an-terminal-command-in-a-swift-script-e-g-xcodebuild) – shallowThought Jan 26 '17 at 09:42

2 Answers2

0

NSTask is not a shell. It's more low-level than that. You can't run full shell expressions in there, just one command. So your commandToRun makes no sense, it should only be the path to the command to run.

uliwitness
  • 8,532
  • 36
  • 58
0

You can use system c function to run a command but it does not give much control over the details.

Khurram Shehzad
  • 261
  • 3
  • 12