2013-02-21 163 views
0

感謝您的幫助。這個執行命令的結果顯示在我的Xcode控制檯中。在NSTextView中顯示命令的結果的最好方法是什麼?NSTextView的Shell命令輸出

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

回答

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]; 
+0

謝謝,但不能讓它開始工作。此外,該項目設置爲使用ARC。 – Paul 2013-02-21 22:05:11

+0

我沒有'ffmpg',所以不能測試你的確切命令,但輸出到stdOut的其他命令對我來說也是一樣。 – 2013-02-21 22:22:14

+0

再次感謝。命令worx正如所料。控制檯輸出確認了這一點。我沒有在textView中得到任何輸出... - 保羅。 – Paul 2013-02-21 22:26:11