Washington Apple Pi

A Community of Apple iPad, iPhone and Mac Users

 AppleScript for real people

© 2008 Lawrence I. Charters

Washington Apple Pi Journal, reprint information

Few Mac users say that they wish to know how to program. Many use their Macs for writing E-mail, sending photos to relatives and similar tasks; who needs to know how to program? They think they understand their E-mail package, they are comfortable using a Web browser, and are comfortable in basic maintenance tasks. Beyond that, they claim to have no particular expertise.

These are precisely the people who should learn about AppleScript. You don’t need to know anything about programming. Life can be so much better if you simply use what Apple has thoughtfully already provided.

What is AppleScript?

AppleScript is Apple’s scripting language; the name alone seems to make this self-evident. But if you aren’t a Hollywood scriptwriter, what does that mean? AppleScript was designed to allow applications to talk to other applications using a precisely defined and limited vocabulary. Apple provides dozens of pre-written scripts that can do everything from print iTunes album covers to import names into Address Book. This article will concentrate on one set of scripts to do a common task: renaming files.

Directory of digital photos

Scripting1.jpg: An entire directory full of digital photos with odd names and no indication of what the subject might be.
Say, for example, you regularly work with photos and these photos have strange and non-descriptive names. You need to rename these images to something that is more useful and more descriptive, but renaming “IMG_0296.JPG” into “picnic20080515.jpg,” over and over again, can easily turn your mind into mush, and trigger carpal tunnel syndrome. (See Figure 1.)

Fortunately for everyone except your surgeon and psychiatrist, AppleScript handles this with ease. Inside your Applications > AppleScript directory is AppleScript Utility. Launch AppleScript Utility, and check the box that says “Show Script menu in menu bar.” Then quit, and put everything back the way it was before.

What happened? You now have a new icon in your menu bar that looks like a scroll. Select this menu item, and a drop-down list reveals dozens of scripts to do all kinds of things. Since we are interested in eliminating the tedious task of remaining files, we are interested in the fourth item down, Finder Scripts. (See Figure 2.)

Select Finder Scripts from the menu

Scripting2: Select Finder Scripts from the AppleScript menu and you’ll see a selection of very useful scripts for doing routine actions.

For renaming photos, we first open a directory of photos that we want to rename. We then select the script called “Replace Text in Item Names.” Once selected, a dialogue box pops up, asking if we want to search and replace File Names, Folder Names, or Both. Since we are interested in changing photos, which are files, we select File Names. (See Figure 3.)

Files or folders

Scripting3: So what will it be: file names, or folder names, or both?

In our sample, the photos are named “_DSC00something.JPG.” An underscore is a terrible thing to have in a file name as, among other things, it upsets alphabetic sorting. So we want to replace the underscore, replace the “DSC” prefix added by the camera, and replace the superfluous zeros. We do want to keep the numbers at the end, plus the .jpg file extension. So we enter the text we wish to search for as “_DSC0,” as shown in Figure 4.

Enter your text pattern

Scripting4: Enter the pattern you wish to replace.

As for what to enter in its place, you want something more descriptive. In this case, the photographer was named Sanderson, the subject was a Pi general meeting, and it took place in January 2007. Dates sort best if you enter them as year-month, so from this we can construct replacement text: “SandersonGM0701-“ – the hyphen is tossed in to separate the date from the sequence number. (See Figure 5.)

Replace the text

Scripting5: Replace the text with this new, improved, more descriptive text.

The script next asks you to confirm what you are searching for and what you wish to replace it with; this is a critical step. Pay close attention, or you could replicate a typographical error across dozens, or hundreds, of files. Also, confirm you are working in the correct directory; renaming photos of your grandmother’s birthday party to “dormkegger” could raise some interesting questions. (See Figure 6.)

Do you really want to do this?

Scripting6: Do you really want me to replace one string of text with another? If so, press “OK.”

If everything is fine, press “OK” and your Mac will tirelessly, and effortlessly, renumber tens or dozens or hundreds or thousands of files according to your specifications. This not only can save you incredible amounts of time, but also save your sanity, and possibly prevent surgery for repetitive stress syndrome. (See Figure 7.)

Renamed photos

Scripting7: Success! What were once photos indistinguishable from thousands of other photos now have names both useful and distinctive.

And if you find this script a time saver, note that other scripts can change the case of file names, can add a prefix or suffix to file and folder names, can print out a font sample of all your installed fonts, can create a family tree based on entries in your Address Book, can create Mail messages that look like 1930s ransom notes, and many other useful and sometimes not so useful tasks.

Third party developers have also created scripts that will, for example, find iTunes files that aren’t referenced by any playlist, or find “dead” tracks where the entry is still present but the file is lost.

One word of caution specifically concerning photos: rename photos before you import them into iPhoto. Attempting to change iPhoto file names in the Finder is a definite no-no.

If you find the pre-written AppleScripts useful, the next step, of course, is writing your own. Fortunately, you can use the Script Editor, in Applications > AppleScript, to examine Apple’s scripts and use them as a starting point. A number of good books on AppleScript are also available through your local bookstore or through Amazon or other online retailers.