Many Mac users have heard of AppleScript. They know that it exists, and maybe they’ve even tried it out. But few people have actually learned AppleScript and seen how it can help them do more with their Mac. That’s why I’m here. I’ve been AppleScripting for a long time, and my goal is to teach you a little something. Over the course of this five-post series, I will be giving you an introduction to AppleScript that I believe is both fast-paced and accessible. If you’ve programmed or scripted before, great; you’ll catch on quick. If not, you’ll still be able to keep up fine and learn a lot from this series; AppleScript is a fun first language. And the best part is, you’ve got a great resource for any questions: Ask Different! Just ask your question on the site and I or another user will help you out. So, without further ado, let’s jump right in!
So, the first thing you’re probably asking is “What the heck is AppleScript?” Wikipedia’s overview is pretty good:
“AppleScript is a scripting language created by Apple Inc. AppleScript is primarily an inter-application processing system, designed to exchange data between and control other applications in order to automate repetitive tasks. AppleScript has some limited processing abilities of its own – basic calculation abilities, and some more intricate text processing tools.”
So, as you can see, AppleScript focuses on allowing you to interact with other applications and Mac OS X itself. I’ll be covering how that works, but first I’ll introduce you to the language and get you familiar with some of its basic built-in functionality. Ready to get started? I am.
To work with AppleScript, you use the AppleScript Editor app (already on your Mac). This app can be found in /Applications/Utilities. Open that, and you’ll see a window that looks like this one:
I’ve marked the important parts of the window. Here’s what they do:
- Stop: Stop a script while it’s running.
- Run: Compile the script and run it.
- Compile: Compile and check your code without running it.
- Your code: Type your code in here.
- Event log: After you run your code, this shows a summary of what happened.
Okay, time to write our first script To start off with, I’ll show you how to write three simple Hello World programs using AppleScript. You’ll get a taste some of AppleScript’s built in user interaction tools, as well as its ability to interact with other applications. You’ll also get familiar with the AppleScript Editor application and how to use it. First, let’s start with a simple dialog box. Type this into the Your code area of AppleScript Editor:
display dialog “Hello World”It will appear in a purple fixed-width font. Press Compile. You’ll notice that the code changed appearance: it’s now blue and black Verdana. The fact that AppleScript Editor didn’t give you an error message means that the code is valid and you don’t have any syntax errors. Now, let’s see what this code does. Press the Run button. You’ll see a dialog box like this one:
Ta-da! Your first AppleScript. You’ll also notice that the Event log tells you what button you clicked on the dialog. You are now officially an AppleScripter!
Let’s say hello a couple more times. Replace your existing code with this:
say “hello world”This time, let’s take a shortcut: instead of pressing the Compile button, skip straight to the Run button. You code will be checked, compiled, and then run. If your volume was turned up, you’ll hear the default computer voice speak the words “hello world” to you. Pretty cool for one line of code, huh?
Okay, one more time. This script will open TextEdit, make a new document, and write in “hello world”. This script is the most complex, so hold on tight. Replace your existing code with this:
tell application "TextEdit" activate make new document set text of front document to "hello world" end tellSo, this may look a little complicated, but don’t worry; I’ll walk you through it. The first line is the beginning of a
tell applicationblock (which lasts until the
end tell). This is what AppleScript uses to talk to other apps. The
TextEditin quotes is all you need to tell AppleScript which app you want to talk to. Just put in the app’s name. The word
activateis telling the application to launch (if it isn’t already) and come to the front. The
make new documentline, as you’ve guessed, tells TextEdit to make a new document. The next line is the most complicated. The document we just created will be in front. This is the one we want to edit.
set text... tois how you edit a document. So, we’re telling TextEdit to, in the front document (the one we just created), edit the text to say “hello world”. The
end tellline closes the
tell applicationblock; anything after this line will not be sent to TextEdit.
You’ve now said Hello World in three ways using AppleScript. You’ve seen a little code and become familiar with AppleScript Editor. You’re already learning the language and what it can do. Tune in next time to go deeper into AppleScript’s built-in functionality: we’ll learn about variables, data types, and operations.
If you have any questions, feel free to comment on this post and I’ll respond. If you have any technical questions about AppleScript or AppleScript Editor, feel free to post on Ask Different!
Filed under AppleScript