Last active
May 21, 2018 06:58
-
-
Save mashhoodr/4996940 to your computer and use it in GitHub Desktop.
NSTask code which outputs directly to a file as well.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 dopath = ~/Desktop/output.txt; [task setArguments: @[ @"-c", @"echo Hello | tee -a path" ]];