यहां एक यूनिक्स स्क्रिप्ट चलाने वाले एसिंक एनएसटीस्क का एक उदाहरण दिया गया है।
echo "working" >&2
यह सूचना केंद्र द्वारा संसाधित और प्रदर्शित करने के लिए भेज दिया जाता है: यूनिक्स स्क्रिप्ट के भीतर echo
आदेशों कि इस तरह मानक त्रुटि के लिए वर्तमान स्थिति वापस भेज रहे हैं।
एक निर्धारित प्रगति पट्टी को अद्यतन करने के लिए बस "25.0" "26.0" जैसे स्टेटस अपडेट भेजें और फ्लोट में कनवर्ट करें और प्रगति पट्टी पर भेजें।
नोट: मुझे यह प्रयोग करने के बाद और इस साइट और अन्य संदर्भों से कई युक्तियों का उपयोग करके यह काम मिल गया। तो मुझे आशा है कि यह आपके लिए सहायक होगा।
NSTask *unixTask;
NSPipe *unixStandardOutputPipe;
NSPipe *unixStandardErrorPipe;
NSPipe *unixStandardInputPipe;
NSFileHandle *fhOutput;
NSFileHandle *fhError;
NSData *standardOutputData;
NSData *standardErrorData;
यहां मुख्य कार्यक्रम मॉड्यूल हैं:
यहाँ घोषणाओं हैं
- (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
धन्यवाद यह काम किया। मुझे यह पसंद है कि आप घोषणा कैसे करते हैं, कुछ लोग नहीं करते हैं और यह वास्तव में मुझे खराब करता है क्योंकि मुझे नहीं पता कि घोषणाओं के रूप में क्या रखा जाए। IBAction की शुरुआत में – drewsdunne
फिर से धन्यवाद, उन चीजों के लिए घोषणाएं क्या हैं जिनके तार – drewsdunne
सेट किए जा रहे हैं और एनएसएमयूटेबलएरे के साथ हिस्सा आवश्यक है? क्या मैं सिर्फ एक .sh फ़ाइल को इंगित नहीं कर सकता/उपयोग/usr/bin/sh – drewsdunne