Skip to content

Instantly share code, notes, and snippets.

@mashhoodr
Last active May 21, 2018 06:58
Show Gist options
  • Select an option

  • Save mashhoodr/4996940 to your computer and use it in GitHub Desktop.

Select an option

Save mashhoodr/4996940 to your computer and use it in GitHub Desktop.
NSTask code which outputs directly to a file as well.
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/bash"];
[task setArguments: @[ @"-c", @"echo Hello | tee -a ~/Desktop/output.txt" ]];
[task setCurrentDirectoryPath:@"~/Development/"];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
[task setStandardError: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
NSData *data;
data = [file readDataToEndOfFile];
NSString *response = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
@Vardaan2048
Copy link
Copy Markdown

Is it possible to add variable to the command in setArguments. For ex.
Instead of [task setArguments: @[ @"-c", @"echo Hello | tee -a ~/Desktop/output.txt" ]]; we do path = ~/Desktop/output.txt; [task setArguments: @[ @"-c", @"echo Hello | tee -a path" ]];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment