Jump to content
xilopaint

AppleScript vs. JXA

Recommended Posts

I am thinking about to learn JXA so rewriting my Things workflow, originally written in AppleScript, feels a good opportunity.

 

How do you guys see JXA in comparison to AS? Should I expect some gain in performance?

 

Is there any workflow library for JXA? Maybe rewriting my workflow would not worth the effort if there isn't any workflow library for Alfred feedback generation in XML or JSON.

Edited by xilopaint

Share this post


Link to post

JavaScript is a much more pleasant language to use than AppleScript (what isn't?), but the actual API for talking to applications is even more demented (it is at least "native" in AppleScript, not a nasty hack),  almost never documented (you spend a lot of time trying to guess what methods and attributes are called), and often simply doesn't work with a given application.

 

You certainly don't need a library to generate feedback in JavaScript. JSON basically is JavaScript. You only need to call JSON.stringify() on a JS object.

 

Personally, I'd say it's probably not worth rewriting a workflow that already works except as a learning exercise. It's quite possible you'll run into something you need to do API-wise that simply doesn't work in JXA.

 

AppleScript and JavaScript are both fundamentally crappy languages. My approach is almost always to use them solely to extract data from/add data to applications and use a more sensible language for the rest of the workflow.

Share this post


Link to post
25 minutes ago, deanishe said:

JavaScript is a much more pleasant language to use than AppleScript (what isn't?), but the actual API for talking to applications is even more demented (it is at least "native" in AppleScript, not a nasty hack),  almost never documented (you spend a lot of time trying to guess what methods and attributes are called), and often simply doesn't work with a given application.

 

What do you mean with "almost never documented"? I thought JXA had a dictionary similarly to AppleScript.

Share this post


Link to post
10 minutes ago, deanishe said:

I'd say it's probably not worth rewriting a workflow that already works except as a learning exercise.

 

I agree with @deanishe’s whole post, but wanted to reiterate this point.

 

JXA can be more capable than AppleScript. My get_title_and_url script is a good example — read the comments, where I explain why the JXA version is superior. But if the AppleScript version didn’t have the limitation it has, spending time on the JXA version would have been almost1 pointless.


In terms of resources, I recommend both the JXA Cookbook (most web searched will lead here, anyway) and the Mac Automation Scripting Guide which is an official resource and shows how to do the same things in both languages.



1. It’s not pointless in the sense that if you want to integrate the code into your own script, instead of calling it stand-alone, it’s (naturally) preferred that it’s in the language you’re writing.
 

Share this post


Link to post
25 minutes ago, xilopaint said:

What do you mean with "almost never documented"? I thought JXA had a dictionary similarly to AppleScript.

 

But that only tells your how to interact with a particular app (and often poorly), not how to use the language itself. When you want to do something even slightly complex, you’re relegated to a bunch of StackOverflow answers and obscure forums, where you’ll have to mix and match answers to do what you want.


Frequently I see code of the form

tell some_app
  some_action
end tell


When tell some_app to some_action does the same more clearly and with less code. But the people doing the multi-line version aren’t even aware the single-line version exists (in all cases I’ve found). You eventually pick that up in someone else’s code and realise you could have benn doing the same yourself.

Share this post


Link to post
39 minutes ago, vitor said:

 

But that only tells your how to interact with a particular app (and often poorly), not how to use the language itself.

 

But deanishe talked about "the actual API for talking to applications".

Edited by xilopaint

Share this post


Link to post
57 minutes ago, xilopaint said:

What do you mean with "almost never documented"? I thought JXA had a dictionary similarly to AppleScript.

 

Yeah, there's a dictionary, but it's not generally a great help. Apple-style docs are typically great if you already know how everything works and just forgot the name of a parameter, but they're useless if you've no idea what's going on.

 

Like, how do you do a whose clause in JXA?

 

At least with AppleScript, Google will usually turn something up.

Share this post


Link to post
20 minutes ago, deanishe said:

What does “better” mean?

 

If you could pick only one language to interact with macOS applications what would it be and why?

Edited by xilopaint

Share this post


Link to post

Using a bridge, for example. 

 

Do you have anything concrete in mind?

 

There usually isn't such a thing as a "better language" in isolation from an actual problem you're trying to solve.

Edited by deanishe

Share this post


Link to post

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
×