Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7433239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:35:02+00:00 2026-05-29T09:35:02+00:00

What I have is NSTask running a long premade shell script and I want

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:35:03+00:00Added an answer on May 29, 2026 at 9:35 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am planning to convert a rather long shell script I have into an
I currently have a shell script that process many images one after the other,
Have you managed to get Aptana Studio debugging to work? I tried following this,
This is a very simple question. I have a script in the same folder
I have an NSTextView that I'm outputting text from NSTask. Everything works as expected
I'm developing my first Mac App have some issues with shell commands... I'm trying
Hi I have the following code: - (IBAction)runTask:(id)sender { NSTask *proc; NSPipe *output; NSData
I have a Cocoa app that does a number of things, but among them,
I have a app that calls an NSTask, (I have written the NSTask and
I seem to have the same problem as here: NSTask waitUntilExit hanging app on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.