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 > Piping to NSTask, or something.

Piping to NSTask, or something.
Thread Tools
Xeo
Moderator Emeritus
Join Date: Mar 2001
Location: Austin, MN, USA
Status: Offline
Reply With Quote
May 9, 2001, 04:44 AM
 
OK, I am making this app which will be a GUI wrapped command. I started out using the Tutorial at CocoaDevCentral for wgetWrapper.

I got through that OK, but now I'm modifying it to make it work like I want. I stripped the UI down to just a text box and a button. I only have the wgetWrapper.m and wgetWrapper.h files for code (that I've touched, anyway).

Here is the code that I'm at right now. This is from my wgetWrapper.m file.
Code:
#import "wgetWrapper.h" @implementation wgetWrapper - (IBAction)download :(id)sender { [downloadButton setTitle:@"Running..."]; [downloadButton setEnabled:NO]; wget = [[NSTask alloc] init]; [wget setLaunchPath:@"/usr/bin/wget"]; [wget setArguments:[NSArray arrayWithObjects:@"-q",@"-O",@"/dev/stdout",[url stringValue],nil]]; [wget launch]; } - (id)init { self = [super init]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(finishedDownload :) name:NSTaskDidTerminateNotification object:nil]; wget = nil; // This is a good time to initialize the pointer return self; } - (void)finishedDownload :(NSNotification *)aNotification { [downloadButton setTitle:@"Download"]; [downloadButton setEnabled:YES]; [wget release]; // Don't forget to clean up memory wget=nil; // Just in case... } @end
As it is, this code works. It returns the HTML of a web page typed into the text field. Right now, it only returns it into the "Run" tab, but it at least works. This next code is just an excerpt from the one above. I'm guessing this is where I need to focus my energy.

Code:
wget = [[NSTask alloc] init]; [wget setLaunchPath:@"/usr/bin/wget"]; [wget setArguments:[NSArray arrayWithObjects:@"-q",@"-O",@"/dev/stdout",[url stringValue],nil]]; [wget launch];
And just for good measure, here is my wgetWrapper.h code.

Code:
#import <Cocoa/Cocoa.h> @interface wgetWrapper : NSObject { IBOutlet id downloadButton; IBOutlet id url; IBOutlet id window; } - (IBAction)download :(id)sender; NSTask *wget; @end
OK, firstly, thanks for reading all that. Secondly, here's my problem.

I want to pipe the result to grep so I can filter it down. From there I plan to pipe it again, but if I learn to do it for the first one, the second should be fairly easy.

I can't figure out how to use NSPipe. What can I do to get started? I've tried everything I can think of (using code from Apple's site and code from these forums that I searched out) and I haven't come up with a working way.

On a slightly different note, I want to make it return that HTML to a text box (or some static text, whatever) in the UI. I'm not sure how to do that either.

I apologize for my ignorance. TIA for any and all help.

[edit: smiles should be off by default in [code] text.];
------------------


[This message has been edited by Xeo (edited 05-09-2001).]
     
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
May 9, 2001, 08:21 AM
 
The code from Apple's NSPipe page will work well.

Just don't copy the task code, and insert your own task instead. Start with a simple Pipe<->Task example first (do an "ls" and have it NSLog() the results), then work it larger.
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
Xeo  (op)
Moderator Emeritus
Join Date: Mar 2001
Location: Austin, MN, USA
Status: Offline
Reply With Quote
May 9, 2001, 04:02 PM
 
I guess I don't understand the Apple page much then. I don't know how to work with the NSFileHandle or the NSData.

Here is the code from Apple's NSPipe page.
Code:
- (void)readTaskData :(id)sender { NSTask *pipeTask = [[NSTask alloc] init]; NSPipe *newPipe = [NSPipe pipe]; NSFileHandle *readHandle = [newPipe fileHandleForReading]; NSData *inData = nil; [pipeTask setStandardOutput:newPipe]; // write handle is closed to this process [pipeTask setLaunchPath:[NSHomeDirectory() stringByAppendingPathComponent:@"PipeTask.app/PipeTask"]]; [pipeTask launch]; while ((inData = [readHandle availableData]) && [inData length]) { [inData processData]; } }
I don't get how to make this work with my code. All my code is in the action for the Button and this code is more self contained. Where can I use this? Where does the output go?

I'm a cocoa and obj-c newbie so I don't know much, but I prefer to learn by doing. I've done a few tutorials to get started, but I haven't come across anything like this.

Can you recommend a book or something that will explain to me all these commands (NSFileHandle, etc) that I don't understand?

[edit: forgot about the smilie thing again]
------------------


[This message has been edited by Xeo (edited 05-09-2001).]
     
   
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 10:01 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