1.1 Introduction
AppleScript is intended to be a natural language. In other words writing programs in AppleScript should be as close to speaking as possible. A very common pattern in AppleScript is: verb, noun, followed by some modifiers.
1.2 Documentation
1.2.1 Apple documentation
The AppleScript language guide can be found here: Introduction to AppleScript Language
For an overivew of Commands read: Commands Overview
For a list of Commands and an explaination to how to use them see: Commands Reference
1.2.2 Third party sites:
1.3 Comments, Variables, Strings, and Numbers
1.3.1 Comments
Single line comments begin with --
-- this is a comment
you can also use the "#" character for comments, like this:
# this is a comment
Here's a multi-line comment
(* Hello world,
this is my first multi-line AppleScript
comment. *)
1.3.2 Variables
To create a variable you use the keywords "set" and "to"
-- set var to set myVariable to 5
Variables cannot have any spaces in them. They cannot start with a number.
1.3.3 Strings
A string is one or more characters enclosed in double quotes.
-- set var to "string" set otherVariable to "This is a string."
Variables may be combined using the ampersand: &
-- combine vars using & set combinedVariable to myVariable & otherVariable
Combining these variables returns a list (an array.)
1.3.4 Numbers and operators
All standard operators work on number variables. However, there are some math symbols you cannot use like the arithmetic "multiplication sign" or "square root"
-- operators "+, -, /, *" work as expected set mathResult1 to 3 + 5 set mathResult2 to 3 - 5 set mathResult3 to 3 / 5 set mathResult4 to 3 * 5 set output to mathResult1 & mathResult2 & mathResult3 & mathResult4
1.3.5 Variable types
AppleScript is loosely typed and tries to make sense of the variable types for you.
However, if you want to explicitly cast a variable as a type use "as string", "as number", etc.
-- Cast a var as a specific type using "as" set combinedVariable to (myVariable as string) & otherVariable
1.3.6 Auto-corrections
ScriptEditor is smart enough to make obvious corrections as you go. Prior ot running a script use the hammer icon to check whether the script compiles properly, this also activates text highlighting after an edit is complete.
Upon attempting to run a script any errors ScriptEditor detects will be flagged up.
N.B. AppleScript remembers all the variables that are set in a script even if they are not used.
1.4 The Dictionary
A dictionary is a place where other apps list the things they will let AppleScript do. Open a dictionary for the program you want to interact with to see what is possible.
Apple's system dictionaries are located in the Library. Go to: Menu > Window > Library
The Dictionary window is split into a column view on the top and a detail window at the bottom.
On the left of the column view is the listing of Suites within the dictionary. You can think of these Suites like chapters in a book, organizing the different classes and commands together into related groupings.
A quick look at some of the components of dictionary listings for the Finder Dictionary:
symbol | meaning |
---|---|
small square orange "S" icon | Suite (a grouping of related classes and their related properties, commands, etc...) |
small blue "C" icon | Command (something you can execute on a class) |
purple "C" icon | Class (a type of thing) |
square purple "P" icon | Property (Classes have properties. Each of these properties has a value that you can read or set.) |
"r/o" signifier icon | A flag to indicate if a proptery is Read-Only. |
square orange "E" icon | Element (things that can be contained within a Container) |
Etc...
1.4.1 Classes
AppleScript is object oriented. The dictionary lists the classes you can interact with.
In Object Oriented Programming, "Inheritance" is a term that means an object has all the properties of something else. In AppleScript all classes inherit from a Super Class called "item".
For example the "Disk" Class has all of the properties of "item".
1.4.2 Commands
-- "tell" and "end tell" frame a command to an application tell application "Atom" activate end tell
In a Dictionary a C in a round blue circle indicates a "Command".
One command is: activate
This is the finder command you use to open an application. You must "tell" the finder what application to use and the what commands to run.
For an overivew of Commands read: Commands Overview
For a list of Commands and an explaination to how to use them see: Commands Reference
The language reference lays out what each of the commands are and what kind of arguments or properties they expect and accept.
Some examples of commands are: activate
, beep
, choose folder
, or set path to folder
.
Commands are grouped into suites. Suites are indicated by square sand colored 'S' icons.
Many of these commands work with different data types like, strings of text, or numbers of files. In AppleScript, these data objects are called classes. You can find them in the Class Reference section of the Language Guide.
1.4.3 Properties
An item
is the archtypal class, all other object classes inherit from item
Items have properties, for example the property "path to" is a property of all items.
-- "path to" is a property of "item" tell application "Finder" set var1 to every item of (path to desktop) end tell
...here "path to desktop" is like an alias to the desktop folder item
All Classes
have properties, some of which are already existing, while others can be set. For example, an object of the file
class will have a name
and a size
. The name
can be set to something different, but you can't modify the size directly.
In dictionary listings the square purple colored 'p' icons indicate Properties of Classes.
-- name is a property of the folder "desktop" tell application "Finder" set var1 to the name of (path to desktop) end tell
Say your interested in the name of a disk, so you look up disk in the dictionary. You see that disk inherits from container which inherits from item. So, in addition to the properties of disk (listed below) you can also interact with properties of container and item.
A dictionary listing for disk reads:
disk n [inh. container > item] : A disk
properties
id (integer, r/o) : the unique id for this disk (unchanged while disk remains connected and Finder remains running)
capacity (double integer, r/o) : the total number of bytes (free or used) on the disk
free space (double integer, r/o) : the number of free bytes left on the disk
ejectable (boolean, r/o) : Can the media be ejected (floppies, CDs, and so on)?
local volume (boolean, r/o) : Is the media a local volume (as opposed to a file server)?
startup (boolean, r/o) : Is this disk the boot disk?
format (Mac OS format/Mac OS Extended format/UFS format/NFS format/audio format/ProDOS format/MSDOS format/NTFS format/ISO 9660 format/High Sierra format/QuickTake format/Apple Photo format/AppleShare format/UDF format/WebDAV format/FTP format/Packet written UDF format/Xsan format/unknown format, r/o) : the filesystem format of this disk
journaling enabled (boolean, r/o) : Does this disk do file system journaling?
ignore privileges (boolean) : Ignore permissions on this disk?
In this script we can make use of any properties of "item" which "disk" inherits. And we can also use properties of "disk" itself.
-- the "name" property is inherited from "item" tell application "Finder" set var1 to the name of (startup disk) end tell -- the "owner" property is inherited from "item" tell application "Finder" set var2 to the owner of (startup disk) end tell "Disk Name = " & var1 & ", Disk Owner = " & var2 -- "format" is a property of "Disk" itself tell application "Finder" set var3 to the format of (startup disk) end tell -- "free space" is a property of "Disk" itself tell application "Finder" set var4 to the free space of (startup disk) end tell "Disk Name = " & var1 & ", Disk Owner = " & var2 & " Disk Format = " & var3 & ", Disk Free Space = " & var4
1.4.4 Elements
Something is an element of an object if it can be contained by the object.
A file is an element of startup disk (something that startup disk can contain)
-- "file" is an element of startup disk tell application "Finder" set var1 to every file of (startup disk) end tell
...in this case an empty array is returned because there are no files on the startup disc, only folders
A folder is an element of startup disk (something that startup disk can contain)
-- "folder" is an element of startup disk tell application "Finder" set var1 to every folder of (startup disk) end tell
…returns an array...
Natural language variations of the statement also work: you can use "the folders" instead of "to every folder".
tell application "Finder" set var1 to the folders of (startup disk) end tell
1.4.5 Conditions
AppleScript also provides control statements, which let you control program flow based on various conditions.
1.4.6 Operators
The control statements often work in conjunction with the operators which evaluate and combine information in various ways. These can test if a value is "greater than", "less than", or "equal" to another value, or is "contained within" a list.