Applescript, the most unnatural natural language

Coming from a computer science background, Applescript is one of the most difficult languages to learn and use. Scripting languages should encapsulate different ways of expressing the same functionality. For example, most languages support conditional statements such as if-then/else, iteration (i.e., for, while, until), and some mechanism for sub-routines. In most popular scripting languages, operators are straightforward: plus and equal signs, brackets or braces, etc.

However Apple has created a language which throws off the chains of cryptic language conventions. Apple created Applescript, a programming language (of sorts) which is based on natural language. Sounds great, right? It will be just like writing a letter to mom. Let’s get started.

Dear Applescript interpreter,

Will you please start at the number 1 and count up to 1000 and do [something useful] each time you count up to the next number?

–your loyal user.

Applescripts response:

Dear Applescript user,

57974, error.

–signed Applescript.

(Read: ha! good luck with that. Your lucky I even gave you this stupid error message, you stupid loser. Learn a real programming language.)

The real problem here is that we as humans are really really good at expressing one idea in a nearly infinite number of ways, Applescript only captures and recognizes about 3 of those. So much for natural.

Next, in contrast to the premise of the language, Applescript documentation is horrible. For example, in the official online Applescript Language Guide, under “The Language At a Glance” we find this unintuitive explanation of the syntax for using handlers:

Handler Syntax
Subroutine definition
(labeled parameters)
( on | to ) subroutineName¬
[ of | in directParameterVariable
[ subroutineParamLabel
paramVariable] … ¬
[ given label:paramVariable[, label:paramVariable
]…]
[ global variable[, variable]…]
[ local variable[, variable]…]
[ statement]…
end [ subroutineName]

For a language whose motivation is doing it the intuitive and natural language way, it’s ironic that the official Applescript documentation is more difficult to understand than that of most other programming languages. It is far behind Java’s simple yet effective API, even Perl’s online documentation is better.

Also, finding answers to Applescript questions online is more difficult and less fruitful than analogous searches for help with many other languages. For my future reference and others, here are a few snippets that I found useful along the way.

Handling errors:

Use a try/end try block, and add an “on error” block to the end (no closing tag). Something like

try
    –code goes here
on error errorStr number errorNum
    display dialog errorStr & “: ” & errorNum
end try

Sub routines:

Here is a template for using a sub routine. Don’t forget the “my in the function call.

tell application “Finder”
    my mySub()
end tell

on mySub
    display dialog “in mySub”
end mySub

If you’d like, leave a comment to share your own tips.