Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 531
jonasbn Avatar asked Feb 06 '26 19:02

jonasbn


1 Answers

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).

like image 165
syntaxera Avatar answered Feb 08 '26 22:02

syntaxera



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!