Basic AppleScript information and syntax
AppleScript is a scripting language that can be used as a kind of "glue" between the system and the Mac applications.
Apple describes it as a tool for repetitive tasks, in fact it a lot more than this: AppleScript is an object oriented language that interacts with all of the objects (and methods) that the system and the applications provide.

Here you will find some basic AppleScript concepts.
This is nor an official guide neither a reference: AppleScript is too complex and too widely usable for this. Here are only some basic concepts.


In order to start with AppleSricpt we need to use the Script Editor. You can launch it on Mac OS X from /Applications/ApleScript/Script Editor


"on" - the events (and the comments)

Everything happens in AppleScript is in response to some event.
The most common event is the script run, that will look like this:

on run
-- some code

The handle "run" is invoked when the script starts.
When this handler is onvoked all of the code inside it is executed.

The double hyphen marks a one-line comment, while multi-line comments are marked with (* ... *)

on run

There are several built-in events, and in addition to those events the user can definre his own events, and pass them parameters:

on run

on myEvent(someText)
display dialog (someText)

built-in basic events

Some of the buil-in events used in AppleScript:

on run
-- invoked when the script is launched

on open listVariable
invoked when the script is opened using Drag&Drop.
The name of variable "listVariable" is user defined,
and will be a list of references to dragged objects

on quit
-- invoked before the script quits.
-- In order to quit effectively, needs the command:
continue quit

on idle
If the script is saved with "No exit", this
handle is invoked once "run" or "open" exit.
-- If you want to sleep before next idle use:
return 10
-- where "10" is the number of seconds

Type of data, "set" and "copy"

AppleScript provides some basic type of data built-in in the code:

-- integer (integer numbers)
set x to 5

-- real (real numbers)
set x to 3.57

-- characters (single characters)
set x to "f"

-- strings
set x to "The Hitch-hicker's Guide To The Galaxy"

-- lists (array)
set x to {"foo",2,3.57}

This introduces the "set" command, that can be used to set the value of some variable to something.
Another way to set the value of a variable is "copy":

copy "Hello world" to x

The main diffence between the two is that "set" creates a reference, while "copy" copies the value of the expression into the given variable.
In case of simple values (like the ones listed above) the effect is exactly the same, while for more complex objects there is a big difference.


(to be continued)