I'm working on a macOS application that uses NSTask
to launch a Python script to perform an operation. The Python implementation displays a progress bar while the operation is running.
The Python script is using the Click package: Click - Showing Progress Bars
NSTask
is not reporting changes to this progress bar through the usual output readers. It is correctly notifying me of output after the progress bar has reached 100% and a final line has been written. I assume there's some sort of flushing that is not happening to inform NSTask
that data is available.
NSTask *task = [[NSTask alloc] init];
task.arguments = ...;
task.environment = ...;
task.executableURL = ...;
NSPipe *pipe = [[NSPipe alloc] init];
task.standardOutput = pipe;
NSFileHandle *outputHandle = pipe.fileHandleForReading;
outputHandle.readabilityHandler = ^(NSFileHandle *handle) {
NSString *output = [[NSString alloc] initWithData:handle.availableData encoding:NSUTF8StringEncoding];
NSLog(@"%@", output);
};
[task launch];
[task waitUntilExit];
NSLog(@"Task should have completed...");
Terminal Output:
[##### ] 50%
Command Finished.
NSTask Output:
Command Finished.
Task should have completed...
While running the command from a Terminal window, the progress bar will update and then finish with the last line. When running from an NSTask
the only output I receive is the last line. No updates are received for the progress bar. Is there a way to receive those progress bar updates?
Same question from 12 years ago: read ASCII progress bar of NSStask