2

I know I am probably missing this hugely,

but anyone knows why this keeps returning an error?

$ node -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?

2 Answers 2

4

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\"';
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot @sakra that worked very well :) also cheers to the tip saying that no applescript was necessary...yes I really needed to open an url on the default browser. Thanks again for your help
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.