Jump to content

What is osascript (JavaScript)?


Recommended Posts

43 minutes ago, AndreProducer said:

From my understanding the osascript is proprietary Apple scripting language


It's the command-line interpreter for Apple's scripting languages. That means AppleScript and JavaScript for Automation (JXA).

Link to comment
16 hours ago, deanishe said:


It's the command-line interpreter for Apple's scripting languages. That means AppleScript and JavaScript for Automation (JXA).

Could you elaborate, please? Do you mean that you write the scripts in JavaScript but the interpreter "converts" the JavaScript into AppleScript? 

Link to comment
6 minutes ago, AndreProducer said:

Do you mean that you write the scripts in JavaScript but the interpreter "converts" the JavaScript into AppleScript?

 

No, it’s just another language with access to (mostly) the same functionality and APIs. You do what you already did in AppleScript but in JavaScript. This isn’t an obscure feature or related to Alfred—if you search online for “JavaScript for Automation” you’ll find specifics, like the Wikipedia link above, the JavaScript for Automation Cookbook, and Apple’s own Mac Automation Scripting Guide which includes examples in both languages.

Link to comment

I apologise for being thick, but does it mean that you can write scripts in JavaScript but only limited to the functionalities that Apple exposes through API?

 

56 minutes ago, vitor said:

 

No, it’s just another language with access to (mostly) the same functionality and APIs. You do what you already did in AppleScript but in JavaScript. This isn’t an obscure feature or related to Alfred—if you search online for “JavaScript for Automation” you’ll find specifics, like the Wikipedia link above, the JavaScript for Automation Cookbook, and Apple’s own Mac Automation Scripting Guide which includes examples in both languages.

 

Link to comment

You can use JavaScript as you know it and have access to some OS functionality and app automation. Look at the last link: Apple shows you how to do the same thing in both languages. Try it. That site is contained, simple, organised, and a good starting point. A rare case of acceptable Apple documentation.


JavaScript for Automation matters if you need to do something only AppleScript can but instead of using an awful, atypical, underpowered, unpopular language (AppleScript) you want to use an awful, familiar, popular language (JavaScript). If you don’t have a use for AppleScript, you probably don’t have a use for JavaScript for Automation.


You seem to have a goal in mind but are asking abstract open questions without context. If you know what you want, ask that and provide what you already know. If you don’t yet know what you want, check the provided resources and return with specific queries.


Otherwise it’s difficult to help you. We’re on the fourth answer and I have no idea what answer you’re looking for. You need to rethink the question.

Link to comment
20 hours ago, AndreProducer said:

Its not easy to ask precise questions when you know very little.

 

That’s why I linked you to comprehensive resources. You’re supposed to check them to get an overview and ask about what isn’t clear. A forum isn’t a good medium for an extensive explanation. We’d only be repeating what’s in those resources.

 

13 hours ago, AndreProducer said:

Alfred supports also shell scripts but you've mentioned only JavaScript and AppleScript. Why?

 

You specifically asked how osascript and JavaScript relate. That’s the entirety of your top post.

Edited by vitor
Link to comment

@vitor Please don't bash me for that question. I've tried to understand the documentation you've gracefully linked and there is still one thing that I fail to comprehend - it seems to me that JavaScript and AppleScript are somehow "elevated" by Apple above other languages (in the context of automation), since Apple bothered to create interpreter for those 2. So are there any advantages of using JavaScript to automate the MacOS over e.g. Bash? I imagine that there would be some additional interface to control MacOS?

Link to comment

Apple created AppleScript; they’re the only ones who support it. Its purpose is to allow the automation of macOS apps, which have to implement a dictionary of what’s possible.


AppleScript is a weird language and dissimilar to most. It tries to be English with the goal of being easy for non-programmers to use. It fails miserably because it’s just as unforgiving of wrong syntax as other programming languages. AppleScript was neither adopted by non-programmers nor is it easy to pick up by programmers. Enter JavaScript, a very popular language, as a way to try to bring more programmers to use these features. It is more pleasant to use but is incomplete and Apple gutted its automation division so either is unlikely to ever be updated. We’re left with two languages with access to the same interfaces, mostly overlapping in features but with a few idiosyncrasies in each.


There is nothing special about these languages. It’s the access they’re given that matters. It’s not “JavaScript”, it’s “JavaScript for Automation” (“JXA” for short). So no, you can’t use Bash to do the same these can but it has nothing to do with the language.


You should avoid them unless you’re trying to do something only they can.

Link to comment
1 hour ago, AndreProducer said:

it seems to me that JavaScript and AppleScript are somehow "elevated" by Apple above other languages (in the context of automation)

 

They are. To automate macOS, you need to be able to call its automation APIs. Apple has added official support for doing that to AppleScript and JXA, while very few non-Apple languages are able to call those APIs.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...