NSTask 启动路径不可访问
我在我的 Cocoa 项目中使用以下代码来调用我制作的脚本。该脚本与项目位于同一文件夹中,甚至显示在 Xcode 中的“Resources”文件夹下。找到了正确的路径,但仍然提示该路径不可访问。请帮忙。
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];
我在调试器中得到的输出是:
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] 启动路径无法访问
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在
[任务启动]
之前添加此行:add this line before
[task launch]
:您需要做的是获取 shell 二进制文件,并将脚本作为参数传递。因此,如果 shell 脚本是针对 bash 编写的,请获取 bash 解释器,并向其传递一个带有一个参数的参数列表:script.sh 的路径。
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.
当脚本本身未标记为可执行时,会发生此错误。打开终端窗口并转到脚本在项目目录中的位置,然后输入:
Clean and build,您可以直接使用脚本 - 这也意味着您可以向其传递参数。
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:
Clean and build and you can use the script directly - this also means you can pass arguments to it.
当我将脚本的名称(存储在应用程序包中)从
foo.sh
更改为foo.command
时,我避免了此错误。.command
扩展名用于 RayNSTask
/Process
的 Wenderlich 教程。我似乎在任何地方都找不到这个记录(我还确保该脚本可以通过 chmod +x foo.sh 执行)。When I change the name of my script (stored in the App Bundle) from
foo.sh
tofoo.command
I avoid this error.The
.command
extension is what is used in the Ray Wenderlich tutorial forNSTask
/Process
. I can't seem to find this documented anywhere (I also make sure the script is executable viachmod +x foo.sh
).因为[任务等待退出];
because of [task waitUntilExit];