Re: get return value from NSUserUnixTask
Re: get return value from NSUserUnixTask
- Subject: Re: get return value from NSUserUnixTask
- From: Ken Thomases <email@hidden>
- Date: Sat, 13 Apr 2013 19:21:08 -0500
On Apr 13, 2013, at 5:41 PM, Rainer Standke wrote:
> I have a sandboxed Mac app that I want to write user script for (which could run outside of the sandbox). I am trying to write that script as a stand-lone Unix executable. I have that called as a NSUserUnixTask, and I can see that it runs. However my sandboxed app doesn't get a return value.
>
> This is what I do:
>
> NSUserUnixTask *task = [[NSUserUnixTask alloc] initWithURL:userScriptURL error:nil];
>
> NSFileHandle *fileHandle = [NSFileHandle fileHandleWithStandardOutput];
> fileHandle.readabilityHandler = ^(NSFileHandle *fh){
> NSLog(@"readable");
> NSData *resultData = [fh availableData];
> NSLog(@"resultData: %@", resultData);
>
> };
> task.standardOutput = fileHandle;
>
> [task executeWithArguments:affectedFolderPaths completionHandler:^(NSError *error) {
> NSLog(@"compl error: %@", error);
> }];
>
> The error is null, the script/task runs, but the readability handler never gets called.
If error is nil, then there was no error.
You have obtained your app's standard output file handle, which is where your app writes its output, and you've set that as the task's standard output. This means that the tasks output will go to the same place as your app's output, which is basically nowhere when launched from the normal GUI (e.g. Finder, Dock, Launchpad, etc.). If you were to launch your app from a Terminal windows, the output of both your app and the task would go to that window.
The standard output of your app can't be read by your app. The file handle isn't even opened for reading, typically.
What you want is to create an NSPipe for the communication between the task and your app. Set the write end of the pipe as the task's standard output and read from the read end of the pipe in your app. (Actually, it's not entirely clear from that docs that you can set up a pipe between the task and your app. NSTask is clearer about this, but within the sandbox has a different purpose.)
Regards,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden