Introduction to AppleScript: Hello World

Post to Twitter

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:

  1. Stop: Stop a script while it’s running.
  2. Run: Compile the script and run it.
  3. Compile: Compile and check your code without running it.
  4. Your code: Type your code in here.
  5. 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 tell
So, 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 application block (which lasts until the end tell). This is what AppleScript uses to talk to other apps. The TextEdit in quotes is all you need to tell AppleScript which app you want to talk to. Just put in the app’s name. The word activate is telling the application to launch (if it isn’t already) and come to the front. The make new document line, 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... to is 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 tell line closes the tell application block; 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

6 Comments

Subscribe to comments with RSS.

  • Andrew says:

    One, little pedantic question. Is there any reason why you indented after the line “tell application ‘TextEdit'” in the last AppleScriptor was that done purely for readability?

    • Andrew says:

      Nevermind. Apparently AppleScript does that automatically after you run the code.

      Nothing to see here gentlemen…carry on!

    • Philip Regan says:

      @Andrew: Applescript files are pre-compiled—they aren’t plain text files with a special extension like other scripting languages—and part of the compilation process means that Applescript enforces all sorts of conventions (whether you want them or not).

  • Ian C. says:

    Nice. I’m looking forward to the series!

  • Daviesgeek says:

    Awesome! I love AppleScripting! Lookin’ forward to seeing more.

  • Kerry Dawson says:

    I was just curious, do you know who I could ask how to do something with Applescruipt. I have two scripts that “I found” each that work perfectly. One chooses the mail id I want Apple mail to send from and the other will sign with the signature I choose. I’d like to combine these so they both act on one email and I’ve tried everything but I’m just no scripter. I need help and I’ve googled this every which way I can think of but can’t come up with the answer. The are:

    tell application “Mail” to make new outgoing message with properties {sender:”ksdawson@icloud.com“, visible:true}

    and

    tell application “Mail” activate set theMsg to make new outgoing message with properties {visible:true} set message signature of theMsg to signature “Kerry Dawson” end tell

  • Comments have been closed for this post