使用NSTask启动启动Node.JS的Shell脚本 [英] Using NSTask to launch shell script that launches Node.JS

查看:157
本文介绍了使用NSTask启动启动Node.JS的Shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可可应用,我想启动启动Node.js的Shell脚本。我想我可以使用NSTask

I have a cocoa app and I want to launch a shell script that launches Node.js. I figured I would do that with NSTask

NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/bin/bash/start.sh"];
[task setArguments:[NSArray arrayWithObjects:@"start.sh", nil]];
[task setStandardOutput:[NSPipe pipe]];
[task setStandardInput:[NSPipe pipe]];

[task launch];

该脚本位于应用程序的根目录中。我在启动路径上尝试了许多变体,但遇到了麻烦。任何帮助将不胜感激!

The script is in the root of my application. I have tried many variations in the launch path and I am stuck. Any help would be greatly appreciated!

编辑:

这是我设置参数的新代码。

Here is my new code where i set the argument. It still will not work for some reason.

NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/bin/bash"];
[task setArguments:[NSArray arrayWithObjects:[[NSBundle mainBundle] pathForResource:@"start" ofType:@"sh"], nil]];
[task launch];


推荐答案

您猜到了,问题出在您的启动路径上。

As you guessed, the problem is your launch path.

启动路径必须是文件的单个路径。文件名不起作用*,并且您不能在同一路径中命名两个单独的文件。

The launch path must be a single path to a file. Filenames won't work*, and you can't name two separate files in the same path.

您的启动路径应该是bash的完整绝对路径。在终端机中使用哪一拳 code来找出哪一个。 (在OS X上安装时应该是/ bin / bash。)

Your launch path should be the complete absolute path to bash. Use which bash in the Terminal to find out which one that'll be. (It should be /bin/bash on a stock OS X install.)

要告诉bash运行脚本,您需要在参数中进行标识。只是说脚本的名字不会减少它。您必须提供脚本的完整绝对路径。没有理由只在任何地方都没有脚本的文件名。

To tell bash to run the script, you need to identify it in the arguments. Just saying the script's name won't cut it; you must give the script's complete absolute path. There's no reason to have the script's filename alone anywhere.

我认为脚本是应用程序包中的资源。要获取其绝对路径,请询问您的主捆绑包,用于该资源的URL ,然后获取该URL的路径

I assume the script is a resource in your application bundle. To get its absolute path, ask your main bundle for the URL to that resource, and then get that URL's path.

(您可以要求资源路径,但是使用URL是值得养成的习惯。如果需要具体的好处,基于URL的方法可以正确地将其第二个参数称为文件扩展名,而不是类型; 类型是指现代用法有所不同。)

(You can ask for a path for a resource directly, but working with URLs is a habit worth developing. If you need a concrete advantage, the URL-based method correctly calls its second argument a filename extension, not a "type"; "type" means something different in modern usage.)

*文件名被视为任何其他相对路径。相对路径可以工作,但是需要解析为相对于当前工作目录存在的路径。默认情况下,该目录为/(根目录),因此任何可以使用的相对路径在功能上均等同于绝对路径。您应该在任何地方都使用绝对路径。

*Filenames are treated as any other relative paths. Relative paths can work, but need to resolve to a path that exists relative to the current working directory. By default, that's / (the root directory), so any relative path that would work is functionally equivalent to an absolute path. You should just use absolute paths everywhere.

这篇关于使用NSTask启动启动Node.JS的Shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆