开发者

exec osascript(AppleScript) from within NodeJS

开发者 https://www.devze.com 2023-03-10 18:07 出处:网络
I know I am probably missing this hugely, but anyone knows why this keeps returning an error? $ nod开发者_运维问答e -v && node

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"`
0

精彩评论

暂无评论...
验证码 换一张
取 消