Node webkit:如何使用MAC中的系统默认浏览器打开外部链接

Jry*_*972 0 javascript macos node.js node-webkit

在Windows中我使用这些线条

gui = require('nw.gui');

//To open a website externally
gui.Shell.openExternal(URL);

//To execute command line
gui.Shell.openItem(commandString);
Run Code Online (Sandbox Code Playgroud)

它工作正常.同一条代码在MAC中不起作用.我在这里失踪了什么?我不想在其中创建任何文件和编写命令(批处理文件,通常称为shell脚本).有没有办法不创建批处理文件并在MAC中运行这些命令?

小智 5

你能提供你正在测试的完整代码部分吗?

以下代码片段在OSX上运行正常(已测试nw.js 0.12.0):

var gui = require('nw.gui');
gui.Shell.openExternal('http://www.google.com');
Run Code Online (Sandbox Code Playgroud)

此外,该gui.Shell.openItem命令不用于执行命令(请参阅Shell文档).

您应该使用child_processnodejs附带的模块:

var exec = require('child_process').exec;
exec(commandString, function (error, stdout, stderr)
{
    console.log('stdout:' + stdout);
    console.log('stderr:' + stderr);
});
Run Code Online (Sandbox Code Playgroud)