Section 2.2. Script Formats


2.2. Script Formats

When you double-click one of the scripts in, say, your Library Scripts Finder Scripts folder,Script Editor opens a new window containing the AppleScript code for that script. On the other hand, if you double-click one of the scripts in your Library Scripts ColorSync folder, the script itself runs rather than opening up in Script Editor.

That might seem like a minor difference, but it highlights an important fact of AppleScript life: scripts come in different formats. If you hope to make it through the jungle of AppleScript files available on the Web (Section C.1)and, perhaps, to post your own scripts thereyou first have to understand the different formats AppleScript supports.

You can open any AppleScript-supported file by dragging it onto Script Editor's icon in the Finder or Dock. To save a script in a different format, you use the File Format pop-up menu in the Save dialog box (Figure 2-5).

There are three main formats:

  • Script files, which get a .scpt file extension.

  • Applications, which get a .app file extension (just like regular Mac OS X programs).

  • Text documents, which get the lengthy .applescript file extension.

Two other minor formatscalled bundlesare also available. Page Sidebar 2.5 explains how and when to use these.

The following sections give you a better idea of when you should save a script as a particular script type.

Figure 2-5. The File Format pop-up is available whenever you choose Save or Save As from Script Editor's File menu. The other options in this dialog box are available only for certain formats; you can set the Line Endings, for instance, only if you're saving the file as Text.


2.2.1. Script Files

So far, the scripts you've been using for most of this bookthe ones located in your Library Scripts Basics folder, for exampleare stored as .scpt files (Figure 2-6). When you double-click a .scpt file, the script automatically opens in Script Editor.

If you plan to use your scripts in the Script Menu (Section 1.1.16.1), use this format. To save a file in .scpt format, simply choose Script from the File Format pop-up menu (Figure 2-5).

Figure 2-6. You can distinguish a .scpt file from any other by its distinct icon (and, of course, by its file extension). This format is the universal standard for scripts; if you find a script online, chances are it'll be a .scpt file.


Power Users' Clinic
The Navigation Bar

If you open long, involved scripts with Script Editor (say, the Stock Quote script included with Mac OS X), you might get annoyed at the time-consuming process of scrolling up and down just to find a certain section of code. Luckily, Script Editor includes an easier way to get where you need to go: the Navigation Bar.

To display the Navigation Bar, simply choose View Show Navigation Bar. You'll see a thin new strip just below the toolbar, containing two pop-up menus.

The left menu lets you choose which scripting language to use for the script. Unless you've installed third-party languages (Sidebar 3.2), the only choice you'll have in this menu is AppleScript.

The menu on the right, though, is where the action is. Here, you can choose from the different properties (Sidebar 3.4) and subroutines (Section 5.2.5) in your script, which are the building blocks of advanced AppleScript code. Just select a particular handler from the menu, for example, to jump right to that line of code in the script.

It turns out the Navigation Bar in Script Editor is actually a close approximation of the Navigation Bar in Xcode (Section 14.5). If you ever tire of the overwhelming power, simply choose View Hide Navigation Bar.


2.2.2. Applications

An application, is a double-clickable program that gets saved with a .app file extension. (Of course, when you view an AppleScript application in the Finder, you'll likely notice that its extension is hidden, just like all the other applications in your Applications folder.)

With Script Editor, you can create your own applications using AppleScript code. For example, you could take the Current Temperature by Zipcode script (Section 1.1.7), save it as an application, and then run it just by double-clicking its Finder icon.

Figure 2-7. Top: Choose Application from the File Format pop-up menu, and select any options you'd like from the bottom. Turning on Run Only will keep other people from reading your code, while turning on Stay Open will keep your program running even after it's done with its commands.Middle left: When you click Save, you get a double-clickable icon.Middle right: If you turned on the Startup Screen option, the application will present a dialog box before it runs its commands. Bottom: When you click Run, your application finally runs its code.


AppleScript applications have all the other benefits of normal programs, too: you can put them in your Dock; they'll show up in the Apple menu's Recent Items; and you can launch them when you log in by dragging them into your System Preferences Accounts Startup Items pane.

You can even make AppleScript applications that support drag-and-dropped files. These mini-applications are known as droplets, and Section 7.3.3 shows how to write them.

2.2.3. Plain Text Documents

If you choose Text from the File Format pop-up menu, you'll get a file that ends in .applescript. This extension signifies that the script is nothing more than a plain old text file. Even though you can double-click the file's icon to open it in Script Editor, you can also drag the file into TextEdit, BBEdit, Microsoft Word, or any other text-processing program.

"But why would I save a script as a text file when I can just save it as an application?" you ask. The biggest benefit is that it creates a teeny, tiny file, as shown in Table 2-1. If you want to send a big script as an email attachment, a plain text file is much quicker to download than an application. And once your friend has downloaded the .applescript file, she can open it in Script Editor and re-save it as either a script file or an AppleScript application.

There's one more benefit to using a plain text file: Windows and Mac OS 9 users can open it, too. Just choose Windows Line Endings in the Save dialog box, and send the script to all your Mac- and PC-using friends. (Of course, Windows people can't run the AppleScript file, but at least they can see the beauty of your code.)

When you save a file as plain text, it loses all the syntax coloring that Script Editor applied. If formatting is important to you, choose File Print Save As PDF instead. You'll get a convenient PDF filerather than a text filewith all the color and formatting intact.

Power users' Clinic Bundling Files

As mentioned earlier, there are two other formats that AppleScript supports: Script Bundles and Application Bundles. The point of using a bundle is that all the auxiliary files that your commands use (movies, images, sounds, and so on) can be hidden from view, stored inside the script or application. Then, if you choose to distribute your script over the Internet, you can be sure that all the auxiliary files get distributed as well.

Once you save a file as a bundle, you can crack it open in the Finder by Control-clicking its icon and choosing Show

Package Contents from the shortcut menu. Then navigate to the Contents Resources folder, and drop any files there. Now, when you launch the script, it'll have direct access to those files (using the path to me command, described on Section 8.3.4).

The best part of this arrangement is that you can move your bundle aroundon your hard drive or over the Internetand keep all the files intact. You can even rename the bundle itself (but not the files inside), and the script or application will still work properly.


Table 2-1. Script format comparison

Script type

What happens when you double-click it?

Can Windows open it?

Typical file size

.scpt file or script bundle

Launches Script Editor

No

12 KB

Application or application bundle

Runs itself

No

60 KB

Plain text file

Launches Script Editor

Yes

4 KB




AppleScript. The Missing Manual
AppleScript: The Missing Manual
ISBN: 0596008503
EAN: 2147483647
Year: 2003
Pages: 150

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net