Section 14.2. AppleScript

14.2. AppleScript

AppleScript may be hard for a Windows switcher to grasp right away, because there's simply nothing like it in Windows . It's a programming language that's both very simple and very powerful, because it lets Mac programs send instructions or data to each other . A simple AppleScript program might perform some simple daily task for you: backing up your Documents folder, for example. A more complex script can be pages long. In professional printing and publishing, where AppleScript enjoys its greatest popularity, a script might connect to a photographer's hard drive elsewhere on the Internet, download a photo from a predetermined folder, color -correct it in Photoshop, import it into a specified page-layout document, print a proof copy, and send a notification email to the editorautomatically.

14.2.1. Ready-made AppleScripts

Mac OS X comes with several dozen prewritten scripts that are genuinely usefuland all you have to do is choose their names from a menu. "Playing back" an AppleScript in this way requires about as much technical skill as pressing an elevator button.

To sample some of these cool starter scripts, you must first bring the Script menu to your menu bar. To do so, open your Applications AppleScript folder. Inside, double-click the icon called AppleScript Utility. Turn on "Show Script Menu in menu bar."

Now open the newly installed Script menu, whose icon looks like a scroll, to see the list of prewritten scripts (Figure 14-1).

Some of the scripts in this menu operate on familiar components of the Mac OS, like the Finder; others show off applications or features that are new in Mac OS X. Here are a few of the best:

  • Basics Open Script Editor launches Script Editor, a program that you can use to edit and write your own AppleScript programs.

  • Finder Script Add to File Names, Finder Add to Folder Names . These scripts tack on a prefix or suffix to the name of every file or folder in the frontmost Finder window (or, if no windows are open, on the desktop). Now youre starting to see the power of AppleScript: You could use this script to add the word draft or final or old to all of the files in a certain folder.

  • Finder Scripts Replace Text in Item Names lets you do a search-and-replace of text bits inside file names, folder names, or both. When one publisher rejects your 45-chapter book proposal, you can use this script to change all 45 chapter files from, for example, "A History of Mouse PadsA Proposal for Random House, Chapter 1 to "A History of Mouse PadsA Proposal for Simon & Schuster, Chapter 1."

  • Info Scripts Font Sampler is designed to show you what all your fonts look like (see Figure 14-2).

    Figure 14-1. Mac OS X comes with an assortment of useful scripts to try. The Script menu (which you install from your Applications AppleScript folder) lets you launch AppleScripts, just by choosing their names, from within any application.


    Figure 14-2. The Font Sampler script launches TextEdit, opens a new document, and fills it with dozens of copies of the classic "What does this font look like?" test sentence : The quick brown fox jumped over the lazy dog. Then, as you watch, it formats each line with a different fonta good page to print out and keep as a reference.


  • Mail Scripts Crazy Message Text is Apple at its wackiest. When you run it, a dialog box asks you what message you want to send ("Happy Birthday," for example). Mail then creates a colorful , zany, outgoing formatted message in which each letter has a random typeface, style, color, and size . Its ideal for making people think you spent a long time with your Format menu for their entertainment.

  • URLs CNN and the other commands in the URLs submenu simply open your browser, connect to the Internet if necessary, and then open the specified Web page.

    Another script here, Download Weather Map, is much cooler . In a flash, it downloads the current U.S. weather-map image and then opens the file in the Preview program for viewing.

14.2.2. Writing Your Own AppleScripts

As programming languages go, AppleScript is easy to understand. It takes only a few weeks, not years , to become comfortable with AppleScript. And the power AppleScript places in your hands is well worth the effort you'll expend learning it.

For example, here's a fragment of actual AppleScript code:

 open folder "AppleScript" of folder "Applications" of startup disk 

You probably don't need a manual to tell you what this line from an AppleScript program does. It opens the Applications AppleScript folder on your hard drive. (Thats the folder that contains Script Editor, the Mac OS X program that lets you write your own AppleScripts.)


Tip: AppleScript is too big of a topic to cover in just a few pages. If you're serious about learning to script on the Macto control Photoshop, Word, and iChat automatically, for examplea book like AppleScript: The Missing Manual is a near-necessity.


Switching to the Mac[c] The Missing Manual
Switching to the Mac[c] The Missing Manual
ISBN: 1449398537
EAN: N/A
Year: 2006
Pages: 371

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