You'll get your Mac news here from now on...

Help TMO Grow

Subscriber Login

Advertising Info


The Mac Observer Express Daily Newsletter


More Info

Site Navigation

Home
News
Tips
Columns & Editorials
Reviews
Reports
Archives
Search
Forums
Links
Mac Links
Software
Reports
Contact



by Stephen Swift


Introduction to AppleScript
June 18th, 2001

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

AppleScript FormattingWhenever 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" (*Sends a message to the Finder.*)
activate (*Makes the Finder the front most application.*)
select startup disk (*Select is a command that the Finder understands. Startup disk is the reference to select.*)
open selection (*Open is another Finder command. We've stored the startup disk as a selection, an object.*)
end tell (*Ends the message.*)

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.

  1. An application will run the script when you open it.
  2. A compiled script is a file that you can edit or run in the Script Editor.
  3. 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:
  1. What AppleScript Is.
  2. How to use Script Editor.
  3. How to record a simple script and save it.
New Topics To Be Covered Next Time:
  1. How to hand code a simple script.
  2. How to use the AppleScript dictionaries.
  3. More about AppleScript's syntax.


Comments or Questions? Is this column going to slow or fast for you? Do you want to script something, but don't know how? Do you need something explained or have a question about a script?  My E-mail address, stephen@macobserver.com, is open 24 hours a day, 7 days a week.


Most Recent AppleScript Columns

Break Your Script Into Pieces with Subroutines
June 26th

Repeat Yourself With AppleScript
June 12th

AppleScript Lists
June 5th

AppleScript Archives

Back to The Mac Observer For More Mac News!



Today's Mac Headlines

[Podcast]Podcast - Apple Weekly Report #135: Apple Lawsuits, Banned iPhone Ad, Green MacBook Ad

We also offer Today's News On One Page!

Yesterday's News

 

[Podcast]Podcast - Mac Geek Gab #178: Batch Permission Changes, Encrypting Follow-up, Re-Enabling AirPort, and GigE speeds

We also offer Yesterday's News On One Page!

Mac Products Guide
New Arrivals
New and updated products added to the Guide.

Hot Deals
Great prices on hot selling Mac products from your favorite Macintosh resellers.

Special Offers
Promotions and offers direct from Macintosh developers and magazines.

Software
Browse the software section for over 17,000 Macintosh applications and software titles.

Hardware
Over 4,000 peripherals and accessories such as cameras, printers, scanners, keyboards, mice and more.

© All information presented on this site is copyrighted by The Mac Observer except where otherwise noted. No portion of this site may be copied without express written consent. Other sites are invited to link to any aspect of this site provided that all content is presented in its original form and is not placed within another .