我所拥有的是NSTask运行一个长的预制外壳脚本,我希望NSProgressIndicator检查完成了多少。我已经尝试了很多东西,但似乎无法使其正常工作。如果进度条不确定,我知道如何使用它,但是我希望它随着任务的进行而加载。

这是我运行脚本的方式: 通用标签

我需要在其中放置一个进度条,以检查任务的进度,并进行相应的更新。

有帮助吗?

解决方案

Here is an example of an async NSTask running a unix script. Within the Unix script there are echo commands that send back the current status to standard error like this:

echo "working" >&2

This is processed by notification center and sent to the display.

To update a determinate progress bar just send status updates like "25.0" "26.0" and convert to float and send to the progress bar.

note: I got this working after alot of experimenting and by using many tips from this site and other references. so I hope it is helpful to you.

Here are the declarations:

NSTask *unixTask;
NSPipe *unixStandardOutputPipe;
NSPipe *unixStandardErrorPipe;
NSPipe *unixStandardInputPipe;
NSFileHandle *fhOutput;
NSFileHandle *fhError;
NSData *standardOutputData;
NSData *standardErrorData;

Here are the main program modules:

    - (IBAction)buttonLaunchProgram:(id)sender {

    [_unixTaskStdOutput setString:@"" ];
    [_unixProgressUpdate setStringValue:@""];
    [_unixProgressBar startAnimation:sender];

    [self runCommand];
}
- (void)runCommand {

    //setup system pipes and filehandles to process output data
    unixStandardOutputPipe = [[NSPipe alloc] init];
    unixStandardErrorPipe =  [[NSPipe alloc] init];

    fhOutput = [unixStandardOutputPipe fileHandleForReading];
    fhError =  [unixStandardErrorPipe fileHandleForReading];

    //setup notification alerts
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

    [nc addObserver:self selector:@selector(notifiedForStdOutput:) name:NSFileHandleReadCompletionNotification object:fhOutput];
    [nc addObserver:self selector:@selector(notifiedForStdError:)  name:NSFileHandleReadCompletionNotification object:fhError];
    [nc addObserver:self selector:@selector(notifiedForComplete:)  name:NSTaskDidTerminateNotification object:unixTask];

    NSMutableArray *commandLine = [NSMutableArray new];
    [commandLine addObject:@"-c"];
    [commandLine addObject:@"/usr/bin/kpu -ca"]; //put your script here

    unixTask = [[NSTask alloc] init];
    [unixTask setLaunchPath:@"/bin/bash"];
    [unixTask setArguments:commandLine];
    [unixTask setStandardOutput:unixStandardOutputPipe];
    [unixTask setStandardError:unixStandardErrorPipe];
    [unixTask setStandardInput:[NSPipe pipe]];
    [unixTask launch];

    //note we are calling the file handle not the pipe
    [fhOutput readInBackgroundAndNotify];
    [fhError readInBackgroundAndNotify];
}
-(void) notifiedForStdOutput: (NSNotification *)notified
{

    NSData * data = [[notified userInfo] valueForKey:NSFileHandleNotificationDataItem];
    NSLog(@"standard data ready %ld bytes",data.length);

    if ([data length]){

        NSString * outputString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];  
        NSTextStorage *ts = [_unixTaskStdOutput textStorage];
        [ts replaceCharactersInRange:NSMakeRange([ts length], 0)
                          withString:outputString];
    }

    if (unixTask != nil) {

        [fhOutput readInBackgroundAndNotify];
    }

}
-(void) notifiedForStdError: (NSNotification *)notified
{

    NSData * data = [[notified userInfo] valueForKey:NSFileHandleNotificationDataItem];
    NSLog(@"standard error ready %ld bytes",data.length);

    if ([data length]) {

        NSString * outputString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];  
        [_unixProgressUpdate setStringValue:outputString];
    }

    if (unixTask != nil) {

        [fhError readInBackgroundAndNotify];
    }

}
-(void) notifiedForComplete:(NSNotification *)anotification {

    NSLog(@"task completed or was stopped with exit code %d",[unixTask terminationStatus]);
    unixTask = nil;

    [_unixProgressBar stopAnimation:self];
    [_unixProgressBar viewDidHide];

    if ([unixTask terminationStatus] == 0) {
        [_unixProgressUpdate setStringValue:@"Success"]; 
    }
    else {
        [_unixProgressUpdate setStringValue:@"Terminated with non-zero exit code"];
    }
}
@end

其他提示

You have to have some way to call back or interrupt the progress of a task in oder to tell how much progress you have made. If you are talking about a shell script you could break 1 script up into multiple scripts and upon the completion of a section of the script update the progress indicator. Other apps have done things like this, iirc Sparkle did some custom logic in its decompression code to uncompress in chunks so it could update a progress indicator. If you want to achieve the same effect you are going to have to do something similar.

Get the PID(process ID) for the command which u ran,using it in conjunction with PS(Process state) command you can get the state of your process use that value in your code to show it in the progress bar

a simple way to have your script (nstask) communicate with your obj c controller is by using standard error as a communication channel. Not saying this is perfect but works quite well. You have to setup your task as an asynchronous nstask and use notifications to read from standard input and standard error separately. I can post it later if you need it.

Wrap your script like this:

echo "now starting" >&2
for i in $(ls /tmp)
do 
echo $i 2>&1
done
echo "now ending" >&2

Process Your standard error through a pipe and filehandle and wire it to an outlet for textual status updates or convert it to a float for progress displays. I.e

echo "25.0" >&2. Can be captured and converted.

If you need to capture real std error then trap it and merge it to std out like my example. This is not a perfect approach but I use it frequently and it works well.

On my iPad and don't have code. Let me know if you need a better example.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top