You are right Ken, thanks for pointing me in this direction.
This gets me data that is sent to the script/task's standard output:
NSUserUnixTask *task = [[NSUserUnixTask alloc]
initWithURL:userScriptURL error:nil];
NSPipe *outPipe = [NSPipe pipe];
task.standardOutput = outPipe.fileHandleForWriting ;
[task executeWithArguments:affectedFolderPaths
completionHandler:^(NSError *error) {
NSLog(@"compl error: %@", error);
}];
NSData *data = [[outPipe fileHandleForReading] readDataToEndOfFile];
NSString *result = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(@"result: %@", result);
In other words, an NSPipe does work with an NSUserUnixTask.
Thanks again,
Rainer
On Apr 13, 2013, at 17:21, Ken Thomases <[email protected]> wrote:
> 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 protected])
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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]