# cocoa help please



## mfhaque (Jun 29, 2001)

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

- (IBAction)getuptimeid)sender
{
   uptime = [[NSTask alloc] init];
   [uptime setLaunchPath"/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.


----------



## rharder (Jul 2, 2001)

You need to use NSPipe.

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


```
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


----------



## mfhaque (Jul 2, 2001)

wow, i was starting to feel like i wasn't gonna get a reply, thanks. i'm going to try your method.


----------



## MikeySquid (Jul 6, 2001)

hey, that's pretty darn cool. Thanks, rharder.


----------



## endian (Jul 6, 2001)

> ```
> [tempString release];  // Not sure if you need this.
> ```



Yes you do need it, because you alloc/inited the string above. If you use one of the 'convenience constructors' like 
	
	



```
[NSString stringWithDate]
```
 that usually begin with string, array  or whatever it's returning, the returned object is autoreleased, so releasing it manually isnt necessary. See http://www.stepwise.com/Articles/Technical/2001-03-11.01.html


----------

