JS is available across platforms. AppleScript is Mac-only. That, at least, is not a matter of taste
That’s right, and here’s another objective difference:
If we need a group of (key, value) pairs we can (in AppleScript or JavaScript) give a variable name to a value like:
{Alpha:70, Beta:60, Gamma:50, Epsilon:30}
AppleScript calls that a record
, JavaScript calls it an Object
.
When we need a list of the keys
in our record/object.
In JavaScript:
let keyValues = {Alpha:70, Beta:60, Gamma:50, Epsilon:30}
Object.keys(keyValues)
// --> ["Alpha", "Beta", "Gamma", "Epsilon"]
in AppleScript, it quickly gets difficult, we have to import a library, and learn a verbose and exotic syntax:
use framework "Foundation"
set keyValues to {Alpha:70, Beta:60, Gamma:50, Epsilon:30}
(current application's NSDictionary's dictionaryWithDictionary:keyValues)'s allKeys() as list
And if we want to check the value of a key like 'Delta'
, which the record/object may or may not have (we’re not sure, and need to check):
In JavaScript:
keyValues.Delta
// -> undefined
keyValues.Gamma
// -> 50
But in AppleScript, the script just blows up with an error if we even try a key which the record doesn’t have:
To avoid tripping errors in AppleScript, we would have to use a ‘Foreign Function Interface’ to Objective C
functions.
use framework "Foundation"
set keyValues to {Alpha:70, Beta:60, Gamma:50, Epsilon:30}
(current application's NSDictionary's dictionaryWithDictionary:keyValues)'s objectForKey:("Delta")
-- > missing value
OK, missing value is better … but if we use that same interface, when the record does have the key we tried, what we get back is a raw C pointer
(don’t ask) and the poor beginner is now expected to handle its conversion to an AppleScript value:
use framework "Foundation"
set keyValues to {Alpha:70, Beta:60, Gamma:50, Epsilon:30}
(current application's NSDictionary's dictionaryWithDictionary:keyValues)'s objectForKey:("Beta")
--> «class ocid» id «data optr00000000D9FEBB59DFB6CA9F»
Checking the keys in a record
, testing a key we’re not sure about – these are simple basics …
For example:
- What is the full list of student names ?
- Do I have an entry for this student ?
Very simple in nearly all languages. AppleScript is very unusual in making such basic things hard.
I have had great fun with AppleScript for over 20 years, and invested a huge amount of time in it. But I’m not going to pretend, just because I’m heavily invested in it, that it’s “easy”.
To be honest, it just ain’t.
When TaskPaper became JavaScript only, I experimented with learning a little JavaScript.
I soon noticed that it makes the basics quite a lot easier.
- Not just easier records (see above),
- but also much easier work with Strings and Lists (far more built-in operations for free),
- and much richer libraries of things like:
- regular expressions for pattern matching
- url encoding and decoding
- basic Math functions
Try to do some simple trigonometry for diagrams in OmniGraffle for example. Reach for AppleScript and you will be expected to write or bring your own sin and cos
Reach for JS, and you will find them built in.
AppleScript “easy” ?
Fine if you’ve used it for years. Not a good place start. Especially now, in the context of iOS, and with AppleScript in sunset mode, and no longer actively developed.