Re: Using NSTask and NSPipe to perform a shell script
Re: Using NSTask and NSPipe to perform a shell script
- Subject: Re: Using NSTask and NSPipe to perform a shell script
- From: Jeff C <email@hidden>
- Date: Tue, 5 Sep 2006 18:44:49 -0400
Be careful if the the task creates large amounts of output. You could
get a deadlock if you do waitUntilExit and the pipe's buffers fill.
The task will wait until data is read from the pipe to free up some
buffer space. Reading won't occur because you're waiting for the task
to finish, so both processes deadlock.
Note that the loop below does not suffer from this problem.
On Sep 5, 2006, at 6:26 PM, Spencer Salazar wrote:
for the "cat readme.markdown |" part you could probably just do
this some time before [task launch]:
[task setStandardInput:[NSFileHandle
fileHandleForReadingAtPath:@"readme.markdown"]];
spencer
On Sep 5, 2006, at 6:14 PM, Bruce Johnson wrote:
Here is what I do:
NSData *data;
NSTask *task = [[NSTask alloc] init]; //create a task and pipe
NSPipe *pipe = [[NSPipe alloc] init];
NSString *appPath = @"/usr/local/bin/sqls" //any shell script or
CLI
executable
NSString *dirPath = [someTextField stringValue];
[task setLaunchPath: appPath]; //set the attributes of the task
//the arguments...
[task setArguments: [NSArray arrayWithObjects: @"-f", @"-z",
dirPath, nil]];
[task setStandardOutput: pipe];
[task setStandardError: pipe];
[task launch]; //launch the task
data = [[pipe fileHandleForReading] readDataToEndOfFile];
NSString *string = [[NSString alloc] initWithData: data encoding:
NSASCIIStringEncoding];
[someOtherTextField setStringValue: string];
[string release];
[task release];
[pipe release];
That is a simple as I've gotten it.
On 9/5/06, Keith Blount <email@hidden> wrote:
Hello,
I have a rather basic RTFM question, but unfortunately
reading the docs and browsing several online tutorials
on this one (for instance at CocoaDevCentral and
borkware) has only left me confused. I have several
shell scripts that I want to run through a Cocoa app.
My program provides support for exporting to several
text formats, and a user has sent me some shell
scripts that will expand the range of possible
formats. Unfortunately, despite being comfortable with
Cocoa, my knowledge of using shell-scripts is rather
poor, so transferring this into a Cocoa app is proving
problematic. I know I need to use NSTask and NSPipe,
but am having difficulties finding the correct way of
doing this for my own script. This is how I would run
my script in terminal:
cat readme.markdown | md2latex.sh > readme.tex
(This just uses a script to convert "readme.markdown"
to a LaTeX file - it uses MultiMarkDown to do so.)
If anyone could give me some pointers on how to get
this working in an NSTask wrapper, I would be _very_
grateful. I have found some basic stuff on getting
using the pipes with NSPipe, but using NSTask along
with "cat" and the ">" output has me baffled.
Sorry if this seems incredibly obvious to many.
Many thanks in advance,
Keith
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40gmail.com
This email sent to email@hidden
--
----
Bruce Johnson
email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40gmail.com
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40earthlink.net
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden