NSTask Launch Path Unavailable

I am using the following code in my Cocoa project to invoke the script I made. The script is located in the same folder as the project, and is even displayed in the Resources folder in Xcode. The correct path has been found, but it still says that the path is inaccessible. Help me please.

NSBundle *mainBundle=[NSBundle mainBundle]; NSString *path=[mainBundle pathForResource:@"script" ofType:@"sh"]; NSTask *task = [[NSTask alloc] init]; [task setLaunchPath: path]; NSLog (path); NSPipe *pipe = [NSPipe pipe]; [task setStandardOutput: pipe]; [task setStandardError: pipe]; NSFileHandle *file = [pipe fileHandleForReading]; [task launch]; [task waitUntilExit]; NSData *data = [ddFile readDataToEndOfFile]; NSString *output = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; NSNumberFormatter *f = [[NSNumberFormatter alloc] init]; [f setNumberStyle:NSNumberFormatterDecimalStyle]; HDSpeed = [f output]; [f release]; [task release]; 

The output I get in the debugger is:

2010-07-10 17:53:26.384 tester[5023:a0f] /Users/guest/Library/Developer/Xcode/DerivedData/tester-bhukztmqjwoqrwereagsshvtbfqx/Build/Products/Debug/tester.app/Contents/Resources/script.sh

2010-07-10 17:53:26.386 tester[5023:a0f] launch path not accessible

+7
objective-c path cocoa nsbundle nstask
source share
5 answers

What you need to do is get the shell binary and pass your script as an argument. Therefore, if the shell script is written with bash targeting, get a bash interpreter and pass it a list of arguments with one argument: the path to your script.sh.

+3
source share

add this line before [task launch] :

 [task setLaunchPath:@"/bin/sh"]; 
+4
source share

This error occurs when the script itself is not marked as executable. Open a terminal window and go to where the script is in the project directory, then enter the following:

 chmod +x script.sh 

Clean and create, and you can use the script directly - this also means that you can pass arguments to it.

+2
source share

When I change the name of my script (stored in the Application foo.sh ) from foo.sh to foo.command , I avoid this error.

 let path = Bundle.main.path(forResource: "foo", ofType: "command") 

The .command is what is used in the Ray Wenderlich tutorial for NSTask / Process . I can't seem to find anything in this document (I also make sure the script is executable via chmod +x foo.sh ).

+1
source share

due to [task waitUntilExit];

-one
source share

All Articles