Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > NSTask + NSMutableArray = Love

NSTask + NSMutableArray = Love
Thread Tools
3R1C
Mac Enthusiast
Join Date: Oct 2001
Status: Offline
Reply With Quote
May 8, 2002, 03:36 AM
 
Well, I'm going over this problem in my head, and all the ideas I'm coming up with seem awfully convoluted...

What I want to do is this: Take the output of a shell command such as 'who' executed via NSTask and slap it into an NSArray so's I can read it into a table. It SEEMS like this should to be really simple but being a newbie has me stumped. Does anyone know of any examples of parsed NSTask output I could l look at? Like an opensource app that is known to take the output of some shell command and put it into a table?
3R1C
     
CharlesS
Posting Junkie
Join Date: Dec 2000
Status: Offline
Reply With Quote
May 8, 2002, 03:51 PM
 
Use an NSPipe and an NSFileHandle. Get an NSData from these and parse it. You can find details at http://www.cocoadevcentral.com/.

Ticking sound coming from a .pkg package? Don't let the .bom go off! Inspect it first with Pacifist. Macworld - five mice!
     
lindberg
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status: Offline
Reply With Quote
May 8, 2002, 04:47 PM
 
An example from modifying some of my code...

This uses /bin/sh -c to execute the command, which treats the string as if it was typed onto the command line. You may very well find it easier to just set the launchPath and arguments directly to what you want to run.

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
NSArray *LinesByExecutingCommand(NSString *command)
{
NSPipe *pipe = [[NSPipe alloc] init];
NSTask *task = [[NSTask alloc] init];
NSData *output;
NSString *stringOutput;

[task setLaunchPath:<font color = orange>@"/bin/sh"</font>];
[task setArguments:[NSArray arrayWithObjects:<font color = orange>@"-c"</font>, command, nil]];
[task setStandardOutputipe];
[task launch];

output = [[pipe fileHandleForReading] readDataToEndOfFile];
[task waitUntilExit];
[pipe release];
[task release];

<font color = brown>//[NSString stringWithCString:[output bytes] length:[output length]]</font>
<font color = brown>//would also work, but with the following you can put in a different</font>
<font color = brown>//string encoding if you know any better what the output will be</font>
<font color = brown>//(it'll be NSMacOSRomanStringEncoding by default).</font>
stringOutput = [[[NSString alloc] initWithData:output encoding:[NSString defaultCStringEncoding]] autorelease];

return [stringOutput componentsSeparatedByString:<font color = orange>@"\n"</font>];
}
</font>[/code]
     
3R1C  (op)
Mac Enthusiast
Join Date: Oct 2001
Status: Offline
Reply With Quote
May 8, 2002, 05:14 PM
 
Sorry guys, I'm so new that the code above doesn't appear to put the output into an array so it can go into a table. Could someone direct me to this magical line of code. Most of what else is there is what I already have. The part that has me stumped is parsing the output into a table.

God I'm st00pid. I can see now that the code above does put it into an array, line by line. My limited cocoa knowledge leads me to believe that my next step is to 'enumerate' thru the array and put it into the table.

[ 05-08-2002: Message edited by: 3R1C ]
3R1C
     
serversurfer
Fresh-Faced Recruit
Join Date: May 2002
Status: Offline
Reply With Quote
May 8, 2002, 05:51 PM
 
Who are you calling "limited"?
Love,
The Surfer
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 08:36 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,