如何使用exec打开谷歌浏览器

时间:2015-09-09 17:12:43

标签: node.js

在节点控制台中:

> var a = require('child_process')
> a.execSync('open /Applications/Google Chrome.app')
The files /Applications/Google and /Users/***/Chrome.app do not exist.
Error: Command failed: open /Applications/Google Chrome.app
The files /Applications/Google and /Users/***/Chrome.app do not exist.

    at checkExecSyncError (child_process.js:1339:13)
    at Object.execSync (child_process.js:1379:13)
    at repl:1:3
    at REPLServer.defaultEval (repl.js:132:27)
    at bound (domain.js:254:14)
    at REPLServer.runBound [as eval] (domain.js:267:12)
    at REPLServer.<anonymous> (repl.js:279:12)
    at REPLServer.emit (events.js:107:17)
    at REPLServer.Interface._onLine (readline.js:214:10)
    at REPLServer.Interface._line (readline.js:553:8)
> 

Google和Chrome.app之间有一个空格。我应该怎么做才能使这项工作?

谢谢,

2 个答案:

答案 0 :(得分:0)

尝试在“”字符之间添加路径:

var a = require('child_process')
a.execSync('open "/Applications/Google Chrome.app"')

或''字符之间:

var a = require('child_process')
a.execSync("open '/Applications/Google Chrome.app'")

使用反斜杠(\)转义空间无法正常工作并导致“没有此类文件或目录”错误

答案 1 :(得分:0)

您也可以在OSX上使用它:

var cp = require('child_process');
cp.exec("open -a 'google chrome'");

注意:这仅适用于OSX。