I know I am probably missing this hugely,
but anyone knows why this keeps returning an error?
$ nod开发者_运维问答e -v && node
v0.4.6
> var cmd = 'osascript -e "open location \"http://google.com\""';
> require('child_process').exec(cmd, function (error, stdout, stderr) { console.log(error); });
//Error message
> {
stack: [Getter/Setter],
arguments: undefined,
type: undefined,
message: 'Command failed: 15:20: syntax error: A “:” can’t go after this identifier. (-2740)\n',
killed: false,
code: 1,
signal: null
}
Perhaps it has something to do with the double quotes in the cmd
?
Probably just a quoting issue. This one works for me:
$ node -v && node
v0.4.8
> var cmd = 'osascript -e \'open location \"http://google.com\"\'';
> require('child_process').exec(cmd, function (error, stdout, stderr) { console.log(error); });
Btw, if you just want to open a URL, there is no need to go through AppleScript. Just use the open command:
> var cmd = 'open \"http://google.com\"';
This is simplified through backticks in current node version
$ node -v && node
v10.5.0
> let cmd = `osascript -e 'open location "http://google.com"'`
> require('child_process').exec(cmd, function (error, stdout, stderr) { console.log(error) })
and for the open command
var cmd = `open "http://google.com"`
精彩评论