Welcome to Online Training for AppleScript!
The following beginner's tutorial is excerpted from the Apple Training Series book AppleScript 1-2-3. These interactive materials are meant to be a “Hands-On” experience, taking about 30-40 minutes to complete. A video walk-through is provided, should you want to follow along.
So grab your laptop, make yourself comfortable, and begin the process of changing your computer life forever.
The First Step
Consider this chapter a “book within a book.” If you don't read any other chapter, read this one. It has been designed to give you a quick but thorough introduction to AppleScript through a short, fun, hands-on tutorial. In 25 minutes you’ll learn what AppleScript is, how it works, and how to write basic, functional AppleScript scripts with confidence.
To accomplish this goal, you’ll learn the fundamentals of AppleScript, and then put them to use as we step through the creation of a Finder Toolbar script, that you can use to instantly reset a cluttered desktop window display to a predefined window set.
What is AppleScript?
AppleScript is a language used to automate the actions of the Macintosh Operating System and many of its applications.
Whether a task is as simple as copying a file or as complex as building a real estate catalog, AppleScript can perform the requisite actions for you with “intelligence,” controlling applications and making decisions based on its observations or from information provided by its interaction with the person running the script.
Every day, businesses and individuals alike use AppleScript to create newspapers and books, manage networks, build DVDs, process images, generate web pages, backup files and folders, make videos, and much more. AppleScript is the most powerful, easy-to-use, automation tool available on any platform. And best of all, this technology is free and is built into every copy of the Mac OS!
How it works...
To automate the actions of applications, the computer reads the sequential instructions contained in AppleScript “scripts,” and then communicates those instructions to targeted applications. The targeted applications interpret the communicated instructions, and then perform the actions listed in the scripts.
AppleScript scripts are composed, by motivated users like yourself, in AppleScript, an English-like language containing many of the verbs, nouns, adjectives, articles and other English language elements we use every day. The AppleScript language is designed to be easily understandable, and has a syntax or grammatical structure similar to the one we use to create normal sentences.
Scripts are written in the Script Editor application, a free utility included with every copy of the Mac OS. Scripts can be “run” from within the Script Editor application or saved as either script files or script applications, available for use at any time, and from within any application.
Script files are launched from an application’s script menu or from the Mac OS system-wide Script Menu utility. Script applications, or “applets,” are launched like other applications by double-clicking their icons in the Finder or by clicking their Dock or Finder Toolbar and Sidebar icons.
The Big Picture
AppleScript is based on the concept of scriptable objects belonging to or being contained in other scriptable objects, such as a file being in a folder that belongs to the hard disk, or a word being part of a paragraph belonging to a story. Understanding the relationship between scriptable objects is essential to learning how to write scripts, and can be summarized in the following statements:
On the Macintosh computer everything is an object.
The computer, the desktop, the disks it displays, the folders on the disks and the files in those folders — all of these items are objects. So are applications, their documents, and the data in those documents.
Everything belongs to, is related to, is contained in, or is part of something else.
The file is in the folder that is in the disk that is on the desktop that is on the computer. This same relationship can apply to text as well. The character is in the word that is in the line that is part of the paragraph that is contained in the story.
This relationship between objects is referred to as being hierarchical or, in military terms, follows a “Chain of Command.” AppleScript uses this hierarchical containment structure to identify specific scriptable objects. All the scripts you write will contain hierarchical references.
Just remember this:
In scripts, objects are described in terms of their position in their hierarchical structure, or where they are in their “Chain of Command.”
NOTE: Don’t be put off by the term hierarchical. We often use hierarchical references to identify objects in our lives. For example, you refer to your home as being on a specific street, in a specific city, in a specific county, in a specific state, in a specific country. Or your advanced pottery class as being in room 128, on the third floor, of the west wing, of the Creative Arts building. Object references in AppleScript work the same way.