cocoa help please

mfhaque

Registered
i'm making a program that uses the unix command uptime..

- (IBAction)getuptime:(id)sender
{
uptime = [[NSTask alloc] init];
[uptime setLaunchPath:mad:"/usr/bin/uptime"];
foo = [uptime launch];
[textField setObjectValue:foo];
[uptime release];
}

how can i grab the results from uptime and display it in a textField. i declares foo as a NSString but that doesn't work. any ideas please.
 
You need to use NSPipe.

It works something like this (taken from http://developer.apple.com/techpubs...e/Foundation/ObjC_classic/Classes/NSPipe.html):

Code:
NSPipe *taskPipe = [NSPipe pipe];
NSFileHandle *readHandle = [taskPipe fileHandleForReading];
NSData *inData = nil;
NSString *myString, *tempString;

uptime = [[NSTask alloc] init]; 
[uptime setLaunchPath:@"/usr/bin/uptime"]; 
[uptime setStandardOutput:taskPipe];
[uptime launch]; 

while ((inData = [readHandle availableData]) && [inData length]) {
        tempString= [[NSString alloc] initWithData:inData encoding:NSASCIIStringEncoding];
        myString = [myString stringByAppendingString:tempString];
        [tempString release];  // Not sure if you need this.
}

[textField setStringValue myString];

Can anyone see if I missed something glaring?

With 'uptime' it's not so bad, but with long-running apps you have to use some other tricks to gather all the output without locking up the event thread (which is what's calling those IBAction-type functions).

-Rob
 
Back
Top