Re: Using NSTask and NSPipe to perform a shell script (SOLVED)
Re: Using NSTask and NSPipe to perform a shell script (SOLVED)
- Subject: Re: Using NSTask and NSPipe to perform a shell script (SOLVED)
- From: Keith Blount <email@hidden>
- Date: Wed, 6 Sep 2006 14:11:12 -0700 (PDT)
A very big thank you to all of those who took the time
to answer this. With the help you all gave me, I was
able to get this working. The main thing was setting
-setEnvironment: as Spencer pointed out.
The following code did the trick:
- (void)test
{
NSBundle *bundle = [NSBundle mainBundle];
NSString *latexPath = [bundle
pathForAuxiliaryExecutable:@"md2latex.sh"];
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:latexPath];
[task setArguments:[NSArray
arrayWithObject:[latexPath
stringByDeletingLastPathComponent]]];
char* path = getenv("PATH");
NSString *newpath = [[NSString alloc]
initWithCString: path];
newpath = [newpath stringByAppendingString:[@":"
stringByAppendingPathComponent:[latexPath
stringByDeletingLastPathComponent]]];
[task setEnvironment:[NSDictionary
dictionaryWithObject:newpath
forKey:@"PATH"]];
NSPipe *readPipe = [NSPipe pipe];
NSFileHandle *readHandle = [readPipe
fileHandleForReading];
NSPipe *writePipe = [NSPipe pipe];
NSFileHandle *writeHandle = [writePipe
fileHandleForWriting];
[task setStandardInput: writePipe];
[task setStandardOutput: readPipe];
[task launch];
[writeHandle writeData:[NSData
dataWithContentsOfFile:@"/users/keithblount/Markdown/readme.markdown"]];
[writeHandle closeFile];
NSMutableData *data = [[NSMutableData alloc]
init];
NSData *readData;
while ((readData = [readHandle availableData])
&& [readData length]) {
[data appendData: readData];
}
NSString *outputString;
outputString = [[NSString alloc]
initWithData: data
encoding: NSASCIIStringEncoding];
//[data
writeToFile:@"/users/keithblount/Markdown/CocoaTest.tex"
atomically:YES];
[task release];
[data release];
[outputString autorelease];
[textView setString:outputString]; // Write the
string to the text view for testing purposes
}
For the sake of the archives, this was the original
shell script (named "md2latex.sh"):
cd "%SCR_BUNDLE_PATH"
MultiMarkdown.pl|SmartyPants.pl|xsltproc -novalid
-nonet "$SCR_BUNDLE_PATH/xhtml2memoir.xslt" -
This just uses two freely available perl scripts to
convert a given text file written using the
MultiMarkDown syntax to LaTeX .tex format. Hopefully
this will come in helpful as an example to someone
else browsing Cocoabuilder in the future.
Again, many thanks for all the help - I would have
spent days running around in circles trying to figure
out this, so I am very grateful.
All the best,
Keith
--- Spencer Salazar <email@hidden> wrote:
> On Sep 5, 2006, at 6:59 PM, Keith Blount wrote:
>
> > // [task setCurrentDirectoryPath:[latexPath
> > stringByDeletingLastPathComponent]];
>
> if you are getting those kinds of errors, you
> probably dont want to
> comment this out.
>
> not knowing much about your shell script, id suggest
> appending ":"
> and then [latexPath
> stringByDeletingLastPathComponent] to the value
> for the environment variable "PATH". That way you
> can guarantee that
> it will find the executables in its own directory.
> You can do this
> using NSTask's environment and setEnvironment.
>
> spencer
>
> > NSPipe *readPipe = [NSPipe pipe];
> > NSFileHandle *readHandle = [readPipe
> > fileHandleForReading];
> >
> > NSPipe *writePipe = [NSPipe pipe];
> > NSFileHandle *writeHandle = [writePipe
> > fileHandleForWriting];
> >
> > [task setStandardInput: writePipe];
> > [task setStandardOutput: readPipe];
> >
> > [task launch];
> >
> > [writeHandle writeData:[NSData
> >
>
dataWithContentsOfFile:@"/users/keithblount/Markdown/
>
> > readme.markdown"]];
> > [writeHandle closeFile];
> >
> > NSMutableData *data = [[NSMutableData alloc]
> > init];
> > NSData *readData;
> >
> > while ((readData = [readHandle availableData])
> > && [readData length]) {
> > [data appendData: readData];
> > }
> >
> > NSString *outputString;
> > outputString = [[NSString alloc]
> > initWithData: data
> > encoding: NSASCIIStringEncoding];
> >
> > [task release];
> > [data release];
> > [outputString autorelease];
> >
> > [textView setString:outputString]; // Write the
> > string to the text view for testing purposes
> > }
> >
> > However, when I do this, I get the following
> error:
> >
> >
>
/Users/keithblount/Programming/NSTaskShellTest/build/Debug/
>
> > NSTaskShellTest.app/Contents/MacOS/md2latex.sh:
> > line 4: MultiMarkdown.pl: command not found
> >
>
/Users/keithblount/Programming/NSTaskShellTest/build/Debug/
>
> > NSTaskShellTest.app/Contents/MacOS/md2latex.sh:
> > line 4: SmartyPants.pl: command not found
> > warning: failed to load external entity
> > "xhtml2memoir.xslt"
> > cannot parse xhtml2memoir.xslt
> >
> > Note that all of the files that are named as not
> being
> > found are contained in the same directory as
> > md2latex.sh - they are all copied into the
> Executables
> > directory in a build phase. I wonder if I need to
> do
> > something else to tell the NSTask where to find
> all
> > other files that the shell script refers to?
> Hmm...
> >
> > Any help or advice much appreciated.
> >
> > Many thanks in advance,
> > Keith
> >
> >
> > --- ORIGINAL MESSAGE ---
> >
> > 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
>
>
__________________________________________________
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:
This email sent to email@hidden