0

Thanks for the help. The results of this executed command is displayed in my Xcode Console. What's the best way to get the results of the command to be displayed in an NSTextView?

NSString *commandToRun = @"~/Library/webREF/ffmpeg -nostats -i ~/Desktop/input.wav -  filter_complex ebur128 -f null -";



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

    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];
Paul
  • 189
  • 10

1 Answers1

0

Add something like:

…
NSFileHandle *file;
file = [pipe fileHandleForReading];
NSMutableData *data = [[NSMutableData alloc] init];
NSData *inData = nil;
[task setStandardOutput:pipe];
[task launch];
[task waitUntilExit];

while ((inData = [file availableData]) && [inData length]) {
    [data appendData:inData];
}

[file closeFile];
[task release];
[pipe release];

NSString *result = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
[data release];

// somewhere we have an NSTextView textView
[textView setString: result];
Smilin Brian
  • 980
  • 8
  • 18
  • Thanks but can't get it to work. Also the project is set up to use ARC. – Paul Feb 21 '13 at 22:05
  • I don't have `ffmpg` so can't test your exact command, but other commands that output to stdOut work for me. – Smilin Brian Feb 21 '13 at 22:22
  • Thanks again. The command worx as expected. The Console output confirms this. I'm not getting any output in the textView ... -Paul. – Paul Feb 21 '13 at 22:26
  • I've been answering this without any actual code, but finally made a little project to test it. I thought I was having the same problem, but I had accidentally broken the "wiring" to my `textView`. Once wired up correctly, this does actually work for me (with a different shell command). – Smilin Brian Feb 21 '13 at 22:56
  • Hi @Paul, I think your edits to my answer were rejected. Maybe try updating your question? In any case, I think if you can test whether or not the `result` string actually contains the desired result, you can narrow the problem down to some kind of wiring or setup issue with your text view. – Smilin Brian Feb 22 '13 at 18:02
  • Thanks again. As noted I'm getting exactly what I need in the Xcode Console. Since the project uses ARC, I edited your code for compliance (removed task/pipe release, etc). Pretty straight forward. The textView object is wired up correctly as is IBAction. If you have time would you pull down the project and test it if I uploaded to my server? Again, thanks. – Paul Feb 22 '13 at 19:06