One Quickie


Launching a task (NSTask->General)
Here are the basics to launch "ls -l -a -t" in the current directory, and then read the result into an NSString:
    NSTask *task;
    task = [[NSTask alloc] init];
    [task setLaunchPath: @"/bin/ls"];

    NSArray *arguments;
    arguments = [NSArray arrayWithObjects: @"-l", @"-a", @"-t", nil];
    [task setArguments: arguments];

    NSPipe *pipe;
    pipe = [NSPipe pipe];
    [task setStandardOutput: pipe];

    NSFileHandle *file;
    file = [pipe fileHandleForReading];

    [task launch];

    NSData *data;
    data = [file readDataToEndOfFile];

    NSString *string;
    string = [[NSString alloc] initWithData: data
                               encoding: NSUTF8StringEncoding];
    NSLog (@"woop!  got\n%@", string);
Of course, you can use different NSFileHandle methods for different styles of reading, and you can make a pipe for standard input so you can feed data to the task.



borkware home | products | miniblog | rants | quickies | cocoaheads
Advanced Mac OS X Programming book

webmonster@borkware.com