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.
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:
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
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.
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:
Here are the main program modules:
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
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.