AppleScript for the Programming-Shy

Today's Best Tech Deals

Picked by Macworld's Editors

Top Deals On Great Products

Picked by Techconnect's Editors

When AppleScript was introduced in the early days of System 7, it sounded like a dream come true: here, at last, was a scripting language for normal people–a language with a syntax built around plain English, one that would let average users automate their Macs without requiring the skills of a software engineer.

Then I got my first taste of AppleScript. I found myself staring at sample scripts with lines of code like this:

	(exists (file PrefFileName
	folder "Documents" of preferences folder)))
	display dialog "Continue to
	watch" & ((FolderToWatch)
	buttons {"OK", "New", "Cancel"} default
	button 1
	to the
	of the
	end if

To my nonprogrammer's brain, this looked less like English than like a nightmarish cross between C++ and a bad e. e. cummings poem.

I quickly discovered, however, that you don't have to write code to create scripts. You can record AppleScripts using Script Editor, the little script-making program that comes with every Macintosh (it's in the AppleScript folder inside the Apple Extras folder). If you click on Script Editor's Record button and then perform tasks in any application that supports AppleScript recordability (such as the Finder in Mac OS 8.0 and later), Script Editor will record your actions, translating them into lines of AppleScript code on the fly. This capability lets you build useful AppleScripts that require little or no tweaking. Consider a few examples that take advantage of some of the Mac's easily recordable system components.

The Finder's built-in Clean Up command tidies things up by snapping your icons (on the desktop or in a folder) into position along an invisible grid, but you can't control where it places those icons. With AppleScript, you can create a smart Clean Up command.

To start, launch Script Editor and click on the Record button in the Script Editor window. With Script Editor still running (you can collapse the window, drag it to a corner, or hide it in the background), clean your desktop–making it look precisely the way you want it to. One by one, drag each desktop icon to the exact place you want it to appear. Even if an icon is already where you want it, drag it slightly out of place and then back into position.

Cleanup Code Here's an entire AppleScript created without typing a word of code. All you have to do to produce this script is drag icons around on the desktop with Script Editor's Record function activated. Playing back the script automatically moves the icons into the recorded positions.

The Script Editor window records every action you perform in the Finder, capturing the final screen coordinates for each item you've selected and moved (see "Cleanup Code"). When you're done, click on the Stop button in the Script Editor and choose Save from the File menu. In the Save dialog box, change the Kind pop-up menu to Application, check the Never Show Startup Screen check box, and then save the script.

You now have a genuine AppleScript that will reposition all your desktop icons exactly where you want them. Test the script by dragging the icons out of place and then launching the script to neaten them instantly. (You can set up similar scripts to clean up particular folders instead of the desktop, but beware: this simple script will always look for the exact icons you've specified, so use it only with items that will continue to be present.)

Suppose you're fond of the Bondi Extra Dark desktop pattern, but your spouse prefers to work against a background photo of a Cancun beach. You can record an AppleScript that enables you to switch automatically between the two backgrounds.

Again, start with a fresh script window in Script Editor, as outlined above. Click on the Record button, and then follow the steps for changing the desktop pattern: open the Appearance control panel, choose the pattern or picture you want, click on Set Desktop, and close the control panel. Then stop recording.

This script requires just a tiny tweak, due to an ugly recording bug. Notice that when you selected a pattern from the list, Script Editor recorded a line of code that says something like

	background pattern

Replace the garbage characters between the quotation marks with the actual name of the pattern you chose, such as Bondi Extra Dark, so the line reads

	background pattern
	"Bondi Extra Dark"

You must type the name of the pattern accurately, including the correct case, for the script to work. Save the script as an application, as described in "Ultimate Desktop Cleanup" above.

You can create a script for several favorite backgrounds and place them in the Apple menu for easy access. Once they're in place, you'll be able to switch backgrounds by simply choosing a different script from the Apple menu.

Do you find yourself opening the same folders day after day to get your work done? Why not create a single AppleScript that opens all the folders you need simultaneously and sets the sizes, positions, and view options of the resulting windows all at the same time?

It's easy with Script Editor's recording feature. Once you've started a new script and clicked on Record, open the folders you want. Set the view (such as icon or list) in each window, as well as the size and position of each window. Finally, close unwanted windows, so that only the items you want revealed are on screen. Then click on Stop and save the script. The finished script will fetch multiple folders in rapid succession, opening the windows just where you want them.

If these examples inspire you to create scripts for your favorite applications, first check to see how AppleScript-aware they are–some actions may not be recordable in every program.

Of course, if you take the time to actually learn AppleScript, there's much more you can do to automate your Mac. But even if you're not ready to tackle the syntax of a scripting language, experimenting with AppleScript's record-and-play approach to automation can make at least some of your work a little easier.

Also see ("The Voices of AppleScript")
Macworld Mac Secrets

November 1999 page: 118

AppleScript can do more than streamline your business and automate your workflow. It can also talk. AppleScript provides access to all those lovely voices used in the Mac's text-to-speech functions–the same ones you can choose from the Sound menu in SimpleText or from the Voice pop-up menu in the Speech control panel.

Adding speech to a script involves typing code, but it's pretty painless. Just type the word say , followed by the phrase you want to have spoken, enclosed by quotation marks, like this:

say "boy, this script is sure going to save you a lot of time!"

If you want a voice other than the Speech control panel's default voice to speak the phrase, add the word using to the end of the phrase, followed by the name of the voice, again in quotation marks. For example:

say "your files have been copied" using "Kathy"

say "have a nice day" using "Zarvox"

You can place the voices anywhere in a script. You can even create a script that contains nothing but voices speaking to each other (see "Shakespeare in Code"). For information about controlling the pace and pitch of voices used within AppleScript, choose AppleScript Help from the Help menu in Script Editor and do a search on the word speech .

Shakespeare in Code By following a simple syntax, you can have multiple Mac voices talk to each other within any AppleScript. In this example, Zarvox and Bubbles perform a pivotal scene from Othello .

1 2 Page 1
Page 1 of 2
Shop Tech Products at Amazon