cocoa - external application argument

polaris

Registered
Hi,
I'm building cocoa application which launch external command line application.
The command line requires file as an input and output as a file as well.
In the command line will be: nfbtrans <inputFile.txt >outputFile.txt which means take input from inputFile and store the result in outputFile. It works fine.
In cocoa, part of my program is as follows:

NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/local/bin/nfbtrans"];

NSArray *arg;
NSString *inFileArgument = @"</inputFile.txt";
NSString *outFileArgument = @">/out.txt";
arg = [NSArray arrayWithObjects:inFileArgument,outFileArgument, nil];
[task setArguments:arg];
[task launch];

The command line application launched but it said that the inputFile.txt doesn't exist. However, when I check in the directory, the file does exist. I suspect that it has problem with "<" character. Is that any special way to handle special characters arguments?

Thank you in advance.
 
Hello!

I'm new in this forum, and I will try to answer your question although it was posted a long time ago ;-)

The ">" and "<" arguments are used by the shell application (ie the Terminal), but have no meaning in interapplication communication. You have to use NSPipes to get the output :

NSString *toolPath = [[NSBundle mainBundle] pathForResource:mad:"your_tool" ofType:mad:""];
NSArray *arguments = [[NSArray alloc] init];
NSMutableDictionary *environnement = [[NSMutableDictionary alloc] initWithDictionary:[[NSProcessInfo processInfo] environment]];

my_tool = [[NSTask alloc] init];
[my_tool setLaunchPath:toolPath];
[my_tool setArguments:arguments];
[my_tool setObject:mad:"YES" forKey:mad:"NSUnbufferedIO"];
[my_tool setEnvironment:environnement];

stdoutPipe = [[NSPipe pipe] retain];
stdoutFile = [stdoutPipe fileHandleForReading];
[my_tool setStandardOutput:stdoutPipe];

stderrPipe = [NSPipe pipe];
stderrFile = [stderrPipe fileHandleForReading];
[my_tool setStandardError:stderrPipe];

stdinPipe = [NSPipe pipe];
stdinFile = [stdinPipe fileHandleForWriting];
[my_tool setStandardInput:stdinPipe];

[my_tool launch];

... and now, you send the .txt file through the "stdin" pipe :
[stdinFile writeData:[[NSString stringWithString:mad:"my_text_file"] dataUsingEncoding:[NSString defaultCStringEncoding]]];

... and you get the output in the "stdout" file :
NSData *theResult = [stdoutFile availableData]

(and in a similar way, the errors in "stderrFile")

Hope this will help ! Do not hesitate to ask for further information.

Thifu
 
Back
Top