I have two applications, one with UI (a Cocoa app). The other being a console app.
My requirement is to take user input in CocoaApp and pass it to process to ConsoleApp and return the value.
I have tried something like this, I am able to send and process the data, but not able to return back. I must not use NSDistibutedNotification. I googled and find NSPipe should work, but I am not able to understand how to achieve and use Pipes, please suggest and help to understand.
My codes are here:
In CocoaApp:
- (IBAction)addClicked:(id)sender {
if (self.firstNumber.stringValue.length == 0 || self.secondNumber.stringValue.length ==0) {
NSLog(@"Enter values in both the fields");
return;
}
NSString *a = self.firstNumber.stringValue;
NSString *b = self.secondNumber.stringValue;
NSTask *unixTask = [[NSTask alloc] init];
[unixTask setArguments:@[a, b]];
[unixTask setLaunchPath:@"/Users/.../ConsoleApp"];//the path
[unixTask launch];
}
In ConsoleApp:
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSArray *args = [[NSProcessInfo processInfo] arguments];
NSLog(@"%@", args);
if(args.count>1){
Adder *adderObject = [Adder new];
adderObject.aInt = [args[1] integerValue];
adderObject.bInt = [args[2] integerValue];
NSInteger sum = [adderObject addAwithB];
NSLog(@"Sum = %ld", sum);
}
}
return 0;
}