osascriptjavascript-automation

How do you start an application in JavaScript via 'osascript'?


How do you start an application in JavaScript via osascript?

I have been playing around with some of the examples, which can be found on the Internet and I can get them to work, but if an application targeted for automation is not already started I get the following error:

2015-04-02 10:43:34.749 js.sh[3434:57612] warning: failed to get scripting definition from /Applications/Safari.app; it may not be scriptable.

If Safari is already open, the following example works like a charm.

#!/usr/bin/osascript -l JavaScript

var Safari = new Application("/Applications/Safari.app");

Safari.activate;

window = Safari.windows[0];

tab = Safari.Tab({url:"http://www.dr.dk"});
window.tabs.push(tab);
window.currentTab = tab;

I thought the activate method was the one, but it seems I have overlooked something.


Solution

  • Your script worked just fine on my machine, and Safari launched. To bring the application to the front, you need to change this line:

    Safari.activate;
    

    to this:

    Safari.activate();
    

    You must call the activate command as a function (using parentheses).