How to Write Your First AppleScript
Matt Cone March 7, 2007 Tutorials Mac Developer
Apple describes AppleScript as “an English-like language used to write script files that automate the actions of the computer and the applications that run on it.” I’d add that AppleScript is the easiest scripting language to learn, because it’s so similar to English and it’s very easy to understand.
Script Editor - The Scripting Application
To write AppleScripts, you need Script Editor, an application included with Mac OS X (located in /Applications/AppleScript/). Open Script Editor and create a new file (File > New or ⌘N). You should see something like this:
This is the window where you will be writing scripts. There are several buttons on the top and the bottom of the window. We will be using only two of them for now: Compile and Run. When you have written something in the middle section of the window and click Compile, Script Editor checks the syntax of your script and prepares to run it. (Note that if you click Run without compiling your script, it should automatically compile and run if the syntax is correct). Ignore the other buttons for now.
Tweaking the Preferences
Before we go any further, you will have to make sure Script Editor’s Formatting preferences are set to default:
- Open Script Editor’s Preferences by selecting Script Editor > Preferences in the menu bar (or pressing ⌘,)
- Click the Formatting icon
- Click the Use Defaults button, then click Apply
You have just changed the formatting of the text you’ll be writing in Script Editor. This means that when you compile your script, each element will be colored, so you can recognize it easily.
Writing the Script
What we’ll study today is called User Interaction. These are standalone scripts that don’t affect specific applications. First, we will write the simplest AppleScript in the world. It’s just one word: beep
. This will make your computer beep using the sound set in System Preferences. To change this sound effect, follow these instructions:
- From the Apple menu, select System Preferences
- In the Hardware section, click Sound
- Click the Sound Effects tab, and choose your desired sound effect
Back in Script Editor, type beep
in the main text field. You will see that the word appears in purple Courier font (the default set in the Formatting preferences). This means the text is uncompiled, and your script is not ready to run. To prepare it to run, click Compile (or press ⌘K or the Enter key). Now that you’ve compiled it, the font and color of the text will have changed. This means Script Editor has recognized the word you typed as an AppleScript command and is ready to run the script. Now your window should look like this:
Running the Script
Now that your script is ready, you can run it. Turn your Mac’s sound on (to hear the beep) and click Run (or press ⌘R).
Editing the Script
Let’s say you don’t want one beep, but three. You’ll have to edit your script. To do this, you will have to write beep 3
(or any number you want, as long as you write it as a number). Simply type the 3
after the beep. You’ll notice that the 3
doesn’t appear in the same color as the beep
. This is because beep is a keyword and 3 is a value. Now run the script again. Cool, you have three beeps! If a script has multiple commands, each command must be on a separate line for Script Editor to understand it correctly.
Dude, my Computer is Talking!
(Delete the text you’ve typed in Script Editor before continuing.) That’s cool. You made your computer beep. Now, we can go further. Let’s say you want your computer to talk to you. This is possible using the say
command. You can have your computer say anything you want as long as you specify it correctly. For example, to have it say “Hello world,” you will have to write say "Hello world"
. Note that your computer will speak in the System Voice you set in System Preferences. To change the voice:
- Open System Preferences
- In the System section, click Speech
- Click the “Text to Speech” tab
- Choose a voice from the System Voice popup menu
You can also change the voice within the script, but it won’t be saved in the System Preferences. For example, you can write say "Hello world" using "Bruce"
to use the “Bruce” voice to speak your text. Replace "Bruce"
with the name of any voice you see in the System Voice menu. Now, try to have your computer say something with the voice you like the most. Remember: the text to be spoken must be specified between quotes.
Wrapping Up
Today, you have learned two commands:
- beep
- say
These are certainly not the most useful AppleScript commands, but they’ll give a taste of what AppleScript can do.
Your Turn to Script
Now that you have learned two commands, you can create your own script. Try to write your own which beeps two times, says “Hi, I’m a Computer” and beeps three times.
AppleScript Resources: Bone up on the Basics
Here are a few AppleScript-related resources:
- Apple’s main AppleScript page
- More on AppleScript on Apple’s site
- AppleScript Tutorials and Resources
- An infinity of AppleScripts for iTunes
- An excellent book for those looking to master AppleScript - AppleScript 1-2-3
Subscribe to our email newsletter
Sign up and get Macinstruct's tutorials delivered to your inbox. No spam, promise!