How to use a determinate NSProgressIndicator to check on the progress of NSTask? - Cocoa
Asked Answered
N

4

6

What I have is NSTask running a long premade shell script and I want the NSProgressIndicator to check on how much is done. I've tried many things but just can't seem to get it to work. I know how to use it if the progress bar is indeterminate but i want it to load as the task goes on.

Here is how I am running the script:

- (IBAction)pressButton:(id)sender {
    NSTask *task = [[NSTask alloc] init];
    [task setLaunchPath:@"/bin/sh"];
    [task setArguments:[NSArray arrayWithObjects:[[NSBundle mainBundle] pathForResource:@"script" ofType:@"sh"], nil]];
    [task launch];
}

I need to put a progress bar in that checks the progress of that task while it happens and update accordingly.

Newport answered 17/1, 2012 at 2:35 Comment(0)
S
7

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
Shortwinded answered 4/2, 2012 at 21:49 Comment(10)
thanks this worked. I like how u put the declarations, some people dont and it really bugs me because i dont know what to put as declarations. Thank u againNewport
at the beginning in the IBAction, what are the declarations for the things whose strings are being setNewport
and is the part with the NSMutableArray necessary? Couldn't I just point to an .sh file and use /usr/bin/shNewport
I've done a bit of mixing user interface actions with the task code. The commands in the IBAction, just clear out the labels on the form when the button is pushed. Then the task runs, get's data from std in or std err and updates those labels during the task. I also turn on the progress animation and then turn it off when the task is complete. You can use a standard NSArray instead of a NSMutableArray if you want, but you have to provide one or the other. Here is the method definition from NSTask: - (void)setArguments:(NSArray *)argumentsShortwinded
Ok this works but the task bar doesnt update at all, its blank the whole way and blank at the end. I've linked it in IB to the _unixProgressBar and it doesnt do anythingNewport
and i still don't know what to link the _unixTaskStdOutput and _unixProgressUpdate toNewport
Ok nvm about the IB thing. Its just the progress bar doesn't move up as in numbers how am I supposed to do thatNewport
I'm not 100% sure how you are implementing this. my example just updated a textfield with status messages (from std error), turned on then off a progress bar, then at the end updated a textview with the output of the command. make sure you have these properties correct:@property (weak) IBOutlet NSTextField *unixProgressUpdate; @property (unsafe_unretained) IBOutlet NSTextView *unixTaskStdOutput; also are you getting the NSLog data messages in your console during the run?Shortwinded
I setLaunchPath: with other command and notifiedForStdOutput is never called. Only available for "bash"?Cleptomania
Hi, try to make sure that your script is actually being called and that it is not causing a unix error that you can't see. I didn't test the code above with other shells, but in theory you can use any shell. I'd leave the bash -c command to fire off the command because that works fine on your mac, but put a explicit #!/bin/yourshell command on the first line of your script if you want it to run a different shell. as long as it doesn't do any odd to standard output or standard error it should be okShortwinded
C
1

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.

Camelot answered 17/1, 2012 at 2:43 Comment(4)
this doesnt work because you can't run multiple NSTasks in one actionNewport
Why would you say that? You can launch a NSTask, get its results, launch a different task and so on, i've written apps that launch multiple tasks for various things before. Only difference is I use a class that encapsulates NSTask, but other than that you should be able to do the same github.com/Machx/Zangetsu/blob/master/Source/CWTask.mCamelot
I mean like if i copy and pasted my code in that same IBAction and rename the variable to task2 and changed the bundle and arguments and told it to waitUntilExit for the first task, it says it executes but nothing happens. Sorry I should have been more specific about what wasn't workingNewport
then it sounds like you have another issue going on, perhaps something for another so question. I can't say much without seeing your code, but if you properly abstract things so your not just pasting in code you should be able to launch multiple tasks easily. Perhaps create a method to launch the task & get the results just by itself at a minimum.Camelot
R
0

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

Renter answered 17/1, 2012 at 7:42 Comment(2)
ps - command to get the status of any process.If u run this command with pid (process id,unique to the process) in the terminal it displays the status of the process having that as pid.say for e.g: ps -l 185Renter
but how would i call this back into my application if its run in terminalNewport
S
0

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.

Shortwinded answered 4/2, 2012 at 5:28 Comment(2)
This sounds like it could work but I don't think I know how to do this so a better example would be greatly appreciatedNewport
Ok, here is the context. Run NSTask in async mode. split out standard output and standard error. Use notification center to respond to data on these channels. use the standard error to create a running text status. Basically, in your program while its running, send status updates to standard error with an echo command like this: echo "status report" >&2. It will show up on the standard error pipe and your program can then display it on the screen. Or instead of a status text, send numbers like 25.0 and convert them to float and then update your determinate progress bar.Shortwinded

© 2022 - 2024 — McMap. All rights reserved.