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?

zanona
  • 12,345
  • 25
  • 86
  • 141

2 Answers2

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\"';
sakra
  • 62,199
  • 16
  • 168
  • 151
  • 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 – zanona Jun 07 '11 at 12:25
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"`
ammonvictor
  • 75
  • 2
  • 8