Execute shell script from cocoa (obj.c) with response

前端 未结 4 1171
粉色の甜心
粉色の甜心 2021-01-06 04:57

I have something like:

- (NSString *)unixSinglePathCommandWithReturn:(NSString *)command
{
NSPipe *newPipe = [NSPipe pipe];
NSFileHandle *readHandle = [newPi         


        
相关标签:
4条回答
  • 2021-01-06 05:16

    Hey, Kukosk. There’s a comment on CocoaDev about NSLog() issues when running an NSTask. The fix is to set a pipe for stdin before launching the task:

    [task setStandardInput:[NSPipe pipe]];
    

    If you’re relying on NSLog() only to check whether the task has run, this might fix your problem. Alternatively, you could try to present output in your GUI instead of using NSLog().

    0 讨论(0)
  • 2021-01-06 05:19

    Ah, there's a very important line in the docs you seem to have missed, one of those irritations that NextStep seems to like: "An NSTask object can only be run once. Subsequent attempts to run the task raise an error."

    So, bag the wait, and add [unixTask release] before the return. When you want to run it again, remake the task.

    NSTimer is like this.

    0 讨论(0)
  • 2021-01-06 05:23

    For different approaches to run shell scripts in Cocoa have a look at AMShellWrapper, PseudoTTY.app or OpenFileKiller!

    http://www.cocoadev.com/index.pl?NSTask

    0 讨论(0)
  • 2021-01-06 05:34

    The problem is that you aren't emptying the output buffers of the task. You can't simply launch a task and waitUntilDone unless the task also emits an extremely small amount of data.

    waitUntilDone will obviously not work at all with a task that never exits.

    For a task that emits any quantity of output, you need to set it up such that the output is read as it is generated. Typically, you use readInBackgroundAndNotify or a variant therein.

    In any case, the top of the class description for NSTask has both links to the conceptual guide and to a series of examples that cover this.

    0 讨论(0)
提交回复
热议问题