Help - Search - Members - Calendar
Full Version: Running a Shell Script from Objective-C
InsanelyMac Forum > Apple World > Mac Programming and Development
macprogrammer
Hello:

I have been playing around with xcode a little bit recently, and have not been able to figure out how to run a bash shell script right from an implementation file. I looked into NSTask, but couldn't understand how to use it.

Is there a simple code block with a place to insert a shell script into it? If I could have some assistance with this it would be greatly appreciated. Thanks!
Airr
Look into either of the following:

Moriarty

AMShellWrapper and other stuff

They both support redirecting the output to a Cocoa app....might come in handy!

AIR.
stroke
Here's an example of running a shell script. The actual program being called, sh, is the program that runs the script. Assume the script was called sscript.sh and was in your resources project folder…
CODE
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/bin/sh"];
[task setArguments:[NSArray arrayWithObjects:[[NSBundle mainBundle] pathForResource:@"sscript" ofType:@"sh"], nil]];
[task launch];


setArguments: uses an NSArray to supply arguments to the task — in this case, the location of your script.
macprogrammer
Thanks for the replies; is there any way to insert a direct shell script into the code?

Let's say:
CODE
touch /Users/myusername/hello.text hellowhatever
etc.....
stroke
You could use system() but it's use is proscribed.

NSTask would be the proper API supported method of doing it.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.