【问题标题】:NSTask launch path not accessibleNSTask 启动路径不可访问
【发布时间】:2011-03-14 09:08:31
【问题描述】:

我在我的 Cocoa 项目中使用以下代码来调用我制作的脚本。该脚本与项目位于同一文件夹中,甚至显示在 Xcode 中的“资源”文件夹下。找到了正确的路径,但它仍然说该路径不可访问。请帮忙。

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] launch path not accessible

【问题讨论】:

    标签: objective-c cocoa path nsbundle nstask


    【解决方案1】:

    [task launch]之前添加这一行:

    [task setLaunchPath:@"/bin/sh"];
    

    【讨论】:

      【解决方案2】:

      您需要做的是获取 shell 二进制文件,并将您的脚本作为参数传递。因此,如果 shell 脚本是针对 bash 编写的,请获取 bash 解释器,然后将一个参数列表传递给它,其中包含一个参数:您的 script.sh 的路径。

      【讨论】:

      • 谢谢!我是 OSX 新手,能否告诉我 bash 解释器的位置?
      • /bin 除非用户修改了他们的基本安装,这在这方面似乎不太可能。
      • 哦,谢谢! /bin/bash 似乎可以工作,但现在程序似乎在任务启动后停止前进。有什么线索吗?
      • 如果脚本本身带有参数,你会怎么做?
      【解决方案3】:

      当脚本本身未标记为可执行时会发生此错误。打开终端窗口并转到脚本在项目目录中的位置,然后输入:

      chmod +x script.sh
      

      清理和构建,您可以直接使用脚本 - 这也意味着您可以将参数传递给它。

      【讨论】:

        【解决方案4】:

        当我将脚本的名称(存储在 App Bundle 中)从 foo.sh 更改为 foo.command 时,我避免了这个错误。

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

        .command 扩展名用于Ray Wenderlich tutorial for NSTask / Process。我似乎在任何地方都找不到这个文档(我还确保脚本可以通过chmod +x foo.sh 执行)。

        【讨论】:

          【解决方案5】:

          因为[任务 waitUntilExit];

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-09-20
            • 2013-06-24
            • 2011-08-11
            • 1970-01-01
            • 2015-08-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多