HamishClaxton
HamishClaxton

Reputation: 11

Output text from NSTask to NSTextField OS X - XCode Objective -C

I was wondering how I would output text from NSTask and send it to a NSTextField on OSX.

Upvotes: 0

Views: 649

Answers (1)

Richard Williamson
Richard Williamson

Reputation: 154

I googled NSTask and got this...

but here is the code to do what you want.

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];

 [myTextField setStringValue: string];

Upvotes: 1

Related Questions