AppleScript 101: Variables and Other Basic Techinques

In the previous post, you learned some of the basics of AppleScript. Alex showed you a couple simple commands that you can run in Finder and demonstrated some of the central principles of AppleScript. You should remember how to:

    • Tell which application should perform the action
    • Set up a tell block
    • Run your script
    • Save your script

If you don’t remember how to do any of that, look back at the previous post and refresh yourself. Now let’s more on to the new stuff!

Right at the end of his post, Alex mentioned how to declare a variable. Here it is again:

set variableName to "variable value"

He also showed you how to display a dialog box. Here’s the code:

display dialog "dialog content"

A Simple Script: Hello Evan!

Once you declare a variable, you can use it however you like! So let’s try a simple script:

set myName to "Evan"
tell application "Finder"
display dialog "Hello, " & myName & ". How are you today?"
end tell

There are quite a few important things you should know in that script, so let’s go line by line.
In line 1, we set the variable myName to contain the value Evan, just like I showed you above.
In line 2, we started a tell block by telling that Finder should perform the following action.
In line 3, we started with our command: display dialog. We then wrote the content for the dialog box. Since Hello is not a variable, we surrounded it in quotes. Then, in order to put the variable after it, we had to concatenate the Hello and the name. Concatenating is just joining two strings together. In AppleScript, you use & to do this. Since myName is a variable, we didn’t need quotes. We then concatenated the variable with the rest of our expression, which again, needed quotes because it was not a variable.
In line 4, we ended the tell block.

Tip: Want to put comments in your script? For single-line comments, either preface your line with -- or #. For multi-line comments, enclose them in (* and *).

A Simple Script v2: Hello yourName!

Let’s make another quick script – this one will be an improvement on the one we just made. And I’ll use inline comments to explain what I’m doing!

# This line makes a dialog box which prompts you for your name. The answer that's already filled in is My name is... By putting the default answer in there, we're telling AppleScript that we want the user to be able to input text in the dialog box.
display dialog "What's your name?" default answer "My name is..."
# This line defines the variable theName as whatever the user entered in the dialog box
set theName to (text returned of result)
# This tell block is the same as the old script. It just uses the new theName variable that we defined.
tell application "Finder"
display dialog "Hello, " & theName & ". How are you today?"
end tell

There you have it! A script that lets you enter your name in, and then it greets you. Pretty cool for just starting out, isn’t it?


It really helps if you type this all out manually into your script editor, instead of just copy and pasting or reading. The act of typing it out makes it much more ingrained in your brain!

In the next post we will improve on this script even more and continue the conversation, but that’s for another day. Stay tuned!

1 Comment

  1. Rayne



    Could you type:
    tell application “Finder”
    Before you typed:
    display dialog “What’s your name?” default answer “My name is . . .”

    Or is it only done the way you showed it above, where you typed:
    tell application “Finder”
    At the end?

Leave a Reply