 |
 |
NSTask: help!
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Mar 2001
Location: baltimore, md 21202
Status:
Offline
|
|
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
|
|
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
|
|
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. 
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Mar 2001
Location: baltimore, md 21202
Status:
Offline
|
|
HI,
Thanks for the code! I'll give it a try today!!
Many Thanks!
David
|
|
dstys
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Mar 2001
Location: baltimore, md 21202
Status:
Offline
|
|
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
|
|
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
|
|
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.
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jul 2000
Location: Nottingham, UK
Status:
Offline
|
|
|
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Jun 2000
Location: Dundas, Ontario, Canada
Status:
Offline
|
|
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.
|
|
|
| |
|
|
|
 |
|
 |
|
Admin Emeritus 
Join Date: Oct 2000
Location: Boston, MA
Status:
Offline
|
|
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
|
|
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.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|