NSTextView中的Shell命令输出

时间:2013-02-21 20:29:03

标签: cocoa nstextview nstask

感谢您的帮助。执行此命令的结果显示在我的Xcode控制台中。在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];

1 个答案:

答案 0 :(得分:0)

添加如下内容:

…
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];