Using Tiger: Learning AppleScript

Page 2 of 5

Ihnatko Tiger book cover
This article is excerpted from The Mac OS X Tiger Book from Wiley Publishing (0-7645-7956-6); the book can be ordered from and Barnes & Noble. Copyright ©2005 by Wiley Publishing, Inc. This material is used by permission of John Wiley & Sons, Inc.

  • Create a new script file in Script Editor by pressing Command-N.
    Alternatively, you can click File: New.
  • Click the Record button.
    The Stop button activates.
  • Click over to the Finder.
    Create those four windows, click in them until they’re displaying the folders I want to examine, and change their views to the styles I want (one set to Columns and the rest set to Lists).
  • Click back into Script Editor when the windows are just the way I like them.
    Notice that the script window is now jam-freakin’-packed with script.
  • Click the Stop button.
    The final result is what you see below.
  • AppleScript recording

    Woo-hoo! Just imagine having to type all that in yourself! Recording scripts rules!!!

    Not so fast, Skeezix. Why don’t you try something even simpler, like recording all the steps of using the Finder to connect to an FTP server? Go ahead. I’ll wait here.

    Uh-huh. You wound up with something like the screenshot below, didn’t you?

    Unsuccessful applescript recording

    The only thing it actually recorded was that thing at the very end, when you finished logging in to the FTP server and you changed the window’s view from Icon to List. See what I mean? Spotty and unpredictable. Recording scripts isn’t totally useless, but once you pick up some scripting skills, you practically never use it.

    Well, the Finder window thing went well at any rate. I might want to actually use that script later. Which dovetails us nicely into…

    Saving scripts

    Saving a script has a couple of quirks, compared to saving document files in other applications. No big surprise…in a sense, you’re building software here, so you have to decide how this new software is going to be deployed, you know? The screenshot below shows Script Editor’s standard Save dialog.

    Script Editor’s Save options

    The file format options are as follows:

  • Application. The most useful form for your finished script. It’ll run whether or not Script Editor is present, and it can run as a drag-and-drop utility if you’ve scripted it properly.
  • Script. If you’re still working on your script—or if you’re co-authoring it with another scripter — you might want to save it as a Script file instead. It’s slower and not quite as versatile as an application, but it’s a little easier for a scripter to work with.
  • Script and Application Bundles. You don’t want to know.*
  • Text. It’s a file containing nothing but words. No formatting, no other data at all. Useful for publishing purposes and when you need to read your script on an OS that doesn’t support AppleScript (like a PDA or a Windows notebook).
  • * No, really… I want to know about Bundles : I’ll give you the simplest possible explanation of what a Bundles is—it’s a scheme that the Mac OS uses to ensure that a price of executable software and its resources are always lumped together and treated like a single entity. Imagine that I’ve written an AppleScript that takes a generic, prefabbed sales agreement (which exists as a TextEdit document) and, after asking the user some questions, prints out a binding contract that even Judge Judy would approve of. It’s so useful that I want to give it away to people. If I save the script as a bundle, I can stick the template file right in the same package as the script. My script will always be able to find it, and there’s no chance that someone will receive this script without this Really Important File. Bottom line is that Bundles are things you’ll deal with as you become an advanced scripter. Don’t worry about ’em for now. These aren’t the droids you’re looking for. Move along.

    You also have three options available to you:

  • Run Only. Normally, a saved script, even one that winds up as an Application, can be opened in Script Editor and modified. If you want to protect your code from tampering or theft, click this option.
  • Startup Screen. Sure, you know what this script does and know how to use it. But will everybody else? Clicking this option takes the text you wrote in the Description tab of the script window and packages it as a startup screen that appears whenever the script is run.
  • Stay Open. Scripts normally run once and then quit. Checking this box causes the script to stay open and active. There’s a special kind of AppleScript code called an idle handler that takes advantage of this. If it’s incredibly important that iTunes is always up and running (it has to be available to serve music to all the other Macs in your house, let’s say), you can write a script that checks every 10 minutes to relaunch it if it doesn’t appear to be in the list of running apps.
  • Give the script a name, click Save, and you’re golden.

    | 1 2 3 4 5 Page 2
    Shop Tech Products at Amazon