Ever since System 7.5, Apple has included a programming technology with every computer they sold. Professionals use it with Photoshop, QuarkXPress, FileMaker, and many other programs. It can be used to automate repeated tasks, create a simple program, and all this time it has been sitting in your Apple Extras folder. Many are intimidated by it, and few know its full capabilities. However, if you aren't using it, you are working too hard. What is this programming technology? It is fondly known as AppleScript.
Welcome to the first article of my new AppleScript column at The Mac Observer. This column is targeted to people who have dabbled in AppleScript, or haven't opened the Script Editor once. The first article will introduce you to AppleScript and cover the basics. If you know the AppleScript basics already, wait until this column catches up to your ability, then you can start learning. Each new article will slowly cover a new subject about AppleScript's syntax and functionality so no one becomes overwhelmed.
How It Works
88Behind every action you perform, is an AppleEvent. An AppleEvent is a message containing information, commands, and requests by applications, networks, and the Mac OS. Without AppleEvents telling your computer what to do, it couldn't open a folder, transfer a file, or display pixels a certain way.
AppleScript allows you to control AppleEvents one more way. Instead of opening a folder by double-clicking it, you can write the command in AppleScript. You can store the information as a script, which is just an application or a file. This ability of AppleScript's allows you to automate tasks. Repeated tasks can be recorded into a script. When you need to run the task, you can double-click the script instead of completing the chore manually. When you open the script, the operating system converts the script's instructions into AppleEvent messages, and carries out the commands. In this way, AppleScript acts as a translator between the English dialect and the computer language.
Script Editor - Activate
In the Apple Extras folder of your hard disk, there should be a folder called AppleScript. In that folder you will find an application called Script Editor, and we'll be using this to create our scripts for now. It's a simple AppleScript editor with many limitations. For one thing, it can't create scripts larger than 32k. It also does not have a find and replace command. However, we are only going to create simple scripts for now. When you need to upgrade, I'll suggest some commercial programs. I do recommend downloading Smile, a free AppleScript program that is more advanced than Script Editor, but a little more confusing in my opinion. Script Editor does a very good job labeling the components of the script and has a line-by-line result checking.
Customization Is Key
Whenever I get a new program, the first thing I do is mess around with the preferences. The key to writing a good script is a good working environment. First, the script window is way too small. With monitors ranging in size from 15-inches and up, you can resize the script window to give yourself more room. Then, select 'Set Default Window Size' from the file menu. The next problem is how the scripts look. You can change this by selecting 'AppleScript Formatting' from the Edit menu. Follow the instructions in the window, and you'll be able to change the formatting settings. Why would you want to do this? For me, Geneva 10 is too small. I bumped the size up to 12pt for easier reading. Also, scripts are made up of different parts. AppleScript can italicize, colorize, or underline the categories to set them apart from each other. By doing this, you can learn about and see examples of the parts. This seems like a Catch-22 doesn't it? How are you going to change the formatting, if you haven't learned about the categories yet? The beauty of this is that you don't have to. Take a look at the picture for example styles that I've selected for each category. For now, just watch how AppleScript stylizes words in your script differently. Of course, you'll learn all about the different categories later on.
Your First Script
We're going to write our first script - without touching the keyboard. We'll use the recordable functionality of the Finder. If an application is recordable, AppleScript can watch AppleEvents (Messages sent from one Mac application to another) generated by your actions and translate them into AppleScript commands. Think of it as recording notes onto an audiotape. Press the record button in the Script Editor window. Now open your hard drive. Select the Script Editor and press the stop button. This script will appear in the window:
tell application "Finder" (**)
select startup disk (**)
open selection (**)
end tell (**)
Note that the text in the (* *) won't appear. Those are just comments explaining the scripting lines that I added.
If you open the 'Event Log' and 'Result' windows (From the Control menu) and run the script you can see what AppleScript is doing. As each event is processed it is recorded down in the log. If the computer returns something, it is recorded down after the next line. The final result is displayed in the 'Result' window. In this case it is a reference, the startup disk.
You can write down what your script does in the description box. Now save and quit. You can save your script as an application, compiled script, or text.
- An application will run the script when you open it.
- A compiled script is a file that you can edit or run in the Script Editor.
- A text file can be opened up in any application, and doesn't have to be a script. It could be an idea to write a script.
For now save it you script as an application. Make sure to check 'Never Show Startup Screen' if it isn't checked already so your description of the script doesn't get displayed each time you run it.
These Topics Were Covered This Time:
- What AppleScript Is.
- How to use Script Editor.
- How to record a simple script and save it.
New Topics To Be Covered Next Time:
- How to hand code a simple script.
- How to use the AppleScript dictionaries.
- More about AppleScript's syntax.