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: help!

NSTask: help!
Thread Tools
Fresh-Faced Recruit
Join Date: Mar 2001
Location: baltimore, md 21202
Status: Offline
Reply With Quote
Apr 3, 2001, 04:38 PM
 
Hi,

I need to use NSTask to run some command-line scripts. I've seen the code for ApacheControl. I almost understand it. Could someone help me learn NSTask by writing a SIMPLE program?

For example, make a GUI with a button, and a textField. When the button is pressed, the text field returns the output from and "ls."

Thanks so much...

David
dstys
     
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Apr 3, 2001, 05:43 PM
 
I don't think there's any way to redirect NSTask output without using pipes.

What you're looking for is an NSPipe.

There's some sample code on Apple's NSTask and NSPipe pages

[This message has been edited by parallax (edited 04-03-2001).]
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
Senior User
Join Date: Nov 2000
Status: Offline
Reply With Quote
Apr 3, 2001, 08:00 PM
 
Code:
- (void)doAnLS id)sender { NSTask *aTask = [[NSTask alloc] init]; NSArray *args = [NSArray arrayWithObjects:@"-F", nil]; NSPipe *input = [NSPipe pipe]; NSFileHandle *readHandle = [input fileHandleForReading]; NSData *inData = nil; // just hope this works. If not you may have to fiddle a bit. [aTask setLaunchPath: @"/bin/ls"]; [aTask setArguments: args]; [aTask setStandardInput: input]; [aTask launch]; while ((inData = [readHandle availableData]) && [inData length]) NSLog([NSString stringWithData:inData encoding:nil ]); // if nil doesn't work here try something else [aTask terminate]; //[aTask release]; //? }
I'm sure there's something wrong with it cause it was off the top of my head. I'm still learning so I wasn't sure about the release at the end...all this is making my head spin.

..anyway, that's about as simple as it gets, I think.
     
dstys  (op)
Fresh-Faced Recruit
Join Date: Mar 2001
Location: baltimore, md 21202
Status: Offline
Reply With Quote
Apr 4, 2001, 08:19 AM
 
HI,

Thanks for the code! I'll give it a try today!!

Many Thanks!

David
dstys
     
dstys  (op)
Fresh-Faced Recruit
Join Date: Mar 2001
Location: baltimore, md 21202
Status: Offline
Reply With Quote
Apr 4, 2001, 08:36 AM
 
Yeah!
It works great!! I added a few things that I saw in other code. I added things like: [aTask waitUntilExit] and at the very end I use
[aTask release]. I think I should release the pipes too.

Anyway, thanks again!!

David
dstys
     
Dedicated MacNNer
Join Date: Feb 2001
Status: Offline
Reply With Quote
Apr 4, 2001, 06:29 PM
 
You should not release the NSPipe or NSFileHandle, because they were not created with an alloc method and so were already autoreleased by the methods which returned them. You should only release objects that you explicitly allocate or retain, like the NSTask in the example.
     
Dedicated MacNNer
Join Date: Jun 2000
Location: Dundas, Ontario, Canada
Status: Offline
Reply With Quote
Apr 28, 2001, 01:12 AM
 
I have been trying to do exactly this for some time now and I am very frustrated that I can't even get ls to work. Can someone post some working code for this here because whatever I am missing I am not seeing. For some reason, that "Run" tab is receiving the piped data and causes some weird error while doing it. Can someone help me out here?

Jeff.
Spectral Class
"Shedding Light on Innovation"
     
Fresh-Faced Recruit
Join Date: Jul 2000
Location: Nottingham, UK
Status: Offline
Reply With Quote
Apr 28, 2001, 03:51 PM
 
see www.cocoadevcentral.com for a great example.
     
Dedicated MacNNer
Join Date: Jun 2000
Location: Dundas, Ontario, Canada
Status: Offline
Reply With Quote
Apr 28, 2001, 04:14 PM
 
That is where I got the information of executing NSTask instructions but they have no instructions on how to handle standard I/O which is what I need.
Spectral Class
"Shedding Light on Innovation"
     
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Apr 28, 2001, 06:03 PM
 
Show us your code, Apoc?

Code:
- (void)applicationDidFinishLaunching NSNotification *)not { NSTask *pipeTask = [[NSTask alloc] init]; NSPipe *newPipe = [NSPipe pipe]; NSFileHandle *readHandle = [newPipe fileHandleForReading]; NSData *inData = nil; NSMutableArray *args = [NSMutableArray array]; [args addObject:@"-alF"]; [pipeTask setStandardOutput:newPipe]; [pipeTask setLaunchPath:@"/bin/ls"]; [pipeTask setArguments:args]; [pipeTask launch]; while ((inData = [readHandle availableData]) && [inData length]) { NSLog(@"%@", [NSString stringWithCString char *)[inData bytes]]); } }
(This works.)

The problem is that pipes are uni-directional, so you won't have much luck with interactive programs.
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
Dedicated MacNNer
Join Date: Jun 2000
Location: Dundas, Ontario, Canada
Status: Offline
Reply With Quote
Apr 28, 2001, 06:49 PM
 
Works perfectly, I had made a point of view error with standard I/O (one I had actually introduced while trying to solve a string problem earlier). Thanks for the snippet, Parallax. It helped me figure out how to do the string conversion I was having trouble with. Now I will just get the directory settings correct and then I will have the perfect example of how to do this.

Thanks everyone,
Jeff.
Spectral Class
"Shedding Light on Innovation"
     
   
Thread Tools
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
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 02:53 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2