NSTask launch path not accessible
Asked Answered
T

5

10

I am using the following code in my Cocoa project to call a script I made. The script is in the same folder as the project and even shows up under the "Resources" folder in Xcode. The proper path is found, but it still says that the path is not accessible. Help 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

Taranto answered 11/7, 2010 at 0:54 Comment(0)
P
4

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

Paludal answered 11/7, 2010 at 0:57 Comment(4)
Thanks! I am new to OSX, so could you please tell me where the bash interpreter would be located?Taranto
/bin always unless the user has modified their base install, which seems unlikely in this regard.Paludal
Oh thanks! /bin/bash seemed to work but now the program seems to stop moving forward after the task was launched. Any clue about this?Taranto
what would you do if the script itself takes arguments?Blown
B
6

add this line before [task launch]:

[task setLaunchPath:@"/bin/sh"];
Blown answered 8/7, 2011 at 3:6 Comment(0)
P
4

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

Paludal answered 11/7, 2010 at 0:57 Comment(4)
Thanks! I am new to OSX, so could you please tell me where the bash interpreter would be located?Taranto
/bin always unless the user has modified their base install, which seems unlikely in this regard.Paludal
Oh thanks! /bin/bash seemed to work but now the program seems to stop moving forward after the task was launched. Any clue about this?Taranto
what would you do if the script itself takes arguments?Blown
A
2

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

chmod +x script.sh

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

Abuse answered 20/7, 2016 at 8:23 Comment(0)
T
2

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

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

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

Troposphere answered 13/6, 2017 at 16:30 Comment(0)
T
-5

because of [task waitUntilExit];

Tablet answered 27/1, 2011 at 20:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.