applescript

Getting Started with AppleScript

Using AppleScript, you can create a script that automates many tasks on your mac, and create simple applets. This guide will go into some of the basics of writing AppleScript and some potential uses for it.

AppleScript is a scripting language included with all new Apple computers. If your Mac is running OS 9 or later (which it hopefully is) you’ll already have AppleScript installed.

If you want to find out some more advanced uses of applescript you can check out Apple’s site, which has more advanced tutorials.

What you need

    • Applescript Editor (also known as Script Editor in earlier versions of OS X)
    • A Mac
    • Ingenuity

A Background on AppleScript

AppleScript was an “easy to use” scripting language created by Apple that can access applications running on a Mac. Because it was designed to be easy to use and learn, it contains many syntactical formations that are similar to English; It’s likely that you’ll find it is a lot like writing psuedocode.

Using AppleScript, you can send commands to many different applications on your Mac, by targeting each one and sending a command. You can have a script that opens a new Safari window, pr make a script that creates 50 new folders on your desktop; it all depends on what you want your script to do.

A Very Basic Script

For the purposes of this tutorial, we are going to create a script that closes all open Finder windows and then opens up your home directory. Not a terribly useful script, but a good way to demonstrate some basic concepts.

1. Open AppleScript Editor

The first step towards writing AppleScripts is to open applescript editor. This is where you will be writing and testing all of your scripts. You can find applescript editor in /Applications/Utilities/AppleScript Editor.app

In earlier versions of OS X “AppleScript Editor” was known as “Script Editor”. They both are the same application and perform the same functions.

Once you have AppleScript Editor open, take a moment to look at the buttons on the top of the window. The “record” button allows you to record your actions and turn them into a basic AppleScript, the “stop” button stops executing a running AppleScript, the “run” button runs the written AppleScript, and the “compile” button compiles your AppleScript into an applet.

2. The Tell

Every command in Applescript should start out with some sort of ‘tell’. The tell is a command that identifies the application that will be asked to execute the next line of code. For example, if you wanted Finder to run a command you would start the line of code with

tell application "Finder"

Some commands won’t need to have a tell but we won’t get into that yet.

3. The First Command

So the next step towards creating our neat little script is giving Finder a command. If we look at our scripts objective (to close every window and then open up the home folder) the first thing we need to tell finder to do is to close every window. The syntax for this is easy. We would type:

tell application "Finder" to close every window

It’s that simple, which is one of AppleScripts best features: the similarity to written English.

4. The Second Command

Now that we have had Finder close all of it’s windows, we want it to open up our home folder. To begin a new command we need to insert a line break (hit the enter key). AppleScript is not like many other scripting languages where commands end with semicolons and are enclosed by millions of braces, brackets and parentheses. Instead, AppleScript uses whitespace to break up our script.

Once we are on a new line we need to tell Finder to open up the home folder. The command for this is nearly as simple as the previous one:

tell application "Finder" to open home

Notice that this command is structed very similarly to the last command we they both begin with:

tell application "Finder" to

This is that tell again, which will be seen in most commands we make.

5. Putting it all together

Now that we have both of our commands entered into applescript editor our script should look something along the lines of:

tell application "Finder" to close every window
tell application "Finder" to open home

This is a functional script that performs its purpose. but we could streamline it a little. Because both commands are targeted towards Finder we don’t need to use a tell each time. Instead we can use what is called a tell block. Implementing this would look like this:

tell application "finder"
close every window
open home
end tell

While in a program this short, a tell block doesn’t make it all that more efficient, but when you have a very long script telling one application to do many things, you’ll appreciate not having to type in the tell each time.

6. Saving Your Script

To save your script as an application (or applet) you need to navigate to File – Save As. Then when the “Save” box pops up you need to select “Application” in the drop down menu. You can now assign your application an icon and when you double click on your saved script, it will execute your script without ever needing to open the AppleScript Editor.

Some More Commands

That was a very basic script which barely scratched the surface of what is possible with AppleScript, serving as an introduction to key concepts such as the tell, and the basic structure of an AppleScript command. However, to become truly competent with AppleScript, you’ll need to go out and practice writing scripts.

Here’s a few more examples to help you out:

Get the name of the active window of an application:get the name of front window

Get the index (position in stack) of a window: get the index of window “Macintosh HD

Set a variable (this does not require a tell): set variable to “this variable”

Display a dialog box (if you don’t include a tell it will activate in the current application): display dialog “This is the dialog”

Change the volume (doesn’t need a tell. And values range from 0 to 10 with 10 being the loudest): set volume 10

Check out our next installment in the AppleScript series: Variables and Other Basic Techinques

2 Comments

  1. Rogelio

    05.21.2012

    Reply

    I’m made a simple applescript that would display my name when my MacBook boots up. (display dialog “Hello Rogelio”) but I want to have the dialog box to disappear after 2 seconds how would I do this?

    Thanks.

  2. Hey Rogelio

    10.23.2012

    Reply

    Hey Rogelio use display dialog “Hello Rogelio” giving up after 2

Leave a Reply