Development software

3 cool AppleScripts

EDITOR’S NOTE: This article was adapted from AppleScript: The Missing Manual , by Adam Goldstein (2005; reprinted by permission of O’Reilly Media).

AppleScript has been around since the days of System 7. Despite its maturity, snooty Mac programmers often look down on AppleScript for being too simple, too easy to learn, and too much like English. Of course, those are precisely the traits you want in a computer language. Here are three cool things AppleScript can do for you.

1. Slim Images Down in a Jiffy

Photoshop is the embodiment of pro image editing, but it’s a memory and processor pig. If you’d like a very simple graphics program—one that works completely in the background—OS X has your ticket. It’s called Image Events.

For example, if you take pictures on a digital camera or use a scanner, then you’ve probably got a bunch of bloated TIFF files that are far too big to e-mail or post on a Web page. So what should you do? Convert your files using AppleScript.

If you look in Image Events’ dictionary, you’ll find that the

save
command lets you specify exactly what format to save an image in. (To see the dictionary, open Script Editor in your /Applications/ AppleScript folder, select File: Open Dictionary, select Image Events from the list that appears, and click on Open.) Here’s a script you can use to convert any image files to space-saving JPEGs. In Script Editor, type the following:

	--Part 1:
	on run
	display dialog “Please drag image files to this script to turn them into JPEGs”
	end run
	

	--Part 2:
	on open draggeditems
	repeat with currentFile in draggeditems
	tell application “Image Events”
	set openedFile to open (currentFile as alias)
	

	--Part 3:
	set fileLocation to the location of openedFile
	set fileName to the name of openedFile
	

	--Part 4:
	save openedFile as JPEG
	close openedFile
	end tell
	

	--Part 5:
	tell application “Finder”
	set the name of file fileName of fileLocation to (fileName & “->.jpg”)
	end tell
	end repeat
	end open
	

Save this script as an application by selecting File: Save As and choosing Application from the File Format pop-up menu. Give it a memorable name (such as Convert2JPEG.app). Now when you drag a bunch of files onto the script’s Finder icon (known as a droplet because it runs an AppleScript when you drag and drop files or folders onto it), you’ll transform your once-big images into compact JPEGs. Here’s how the script works:

> Part 1 handles what happens if you simply double-click on the droplet: the script presents a dialog box telling you to drag and drop files instead.



Click here to download all the AppleScripts described in this article. For a quick introduction to this nifty technology, see “Amazing AppleScript.”

> Part 2 tells AppleScript to iterate through the files you dropped one at a time, and to set the

openedFile
variable to the currently open file.

> Part 3 sets two other important variables:

fileLocation
(the folder that contains the current image) and
fileName
(the name of the current image).

> Part 4 converts the image to a JPEG and saves it.

Unfortunately, Image Events isn’t smart enough to rename your file with a .jpg extension. That means if your original file was called horses.tiff, your new JPEG file will still be called horses.tiff. This is a recipe for massive confusion when you try to open the file in Adobe Photoshop or Apple Preview, since your image’s extension ( .tiff ) won’t match its actual format (JPEG).

> Part 5, therefore, tells the Finder to rename your file with the correct .jpg extension. The Finder does this by finding the file you want to rename (

fileName
in
fileLocation
) and then appending
->.jpg
to the end of the file name, producing a fully working JPEG file with the correct extension.

2. Shave a Page Off Your Document

If you write letters and e-mail messages—or computer books, for that matter—you’ve probably experienced the dreaded one-page-too-long problem. It goes like this: you’re typing your document, but when you come to the end, you realize your document is just one page longer than you want it. And if you’re like most people, you sigh deeply and then get to work on shrinking the individual fonts in your document. The trouble with this approach, of course, is that it can take a long time to select blocks of text and change their font sizes, especially if you need to modify multiple fonts on dozens of pages.

Microsoft Word provides an AppleScript command that takes care of all this text-squeezing business for you:

fit to pages
. The trouble is that Word has hundreds of different commands in its dictionary. Does Microsoft really expect you to spend several minutes scanning all the entries in the left pane to find the
fit to pages
command? Yep.

Never fear. To open Word’s dictionary, launch Script Editor, select File: Open Dictionary, find Microsoft Word in the list, and click on Open. Choose Edit: Find: Find and type

Fit to Pages
in the Find field. Click on Next twice.

The definition of the command tells you that it’s meant to squeeze your fonts “just enough so that the document will fit on one fewer pages.” The light-blue

document
text indicates that you have to tell
fit to pages
which document you want it to work with.

Armed with this information, you can write your script in Script Editor:

tell application “Microsoft Word” activate display dialog “Shrink document by a page?” fit to pages (front document) end tell

To shrink a document, open it in Word and then run the script from Script Editor (click on Run). The script will bring Word forward and then display a confirmation dialog box, asking if you want to shrink the document by a page. You’re left with two options:

> If you click on OK, the script proceeds with the next statement (

fit to pages
) and squeezes the front document down by one page.

> If you click on Cancel, the script ends.

After you click on OK, Word finds out what it’ll take to knock one page off your document and adjusts the font sizes throughout the document.

3. Automatically Tell iChat That You’re Away

One of iChat’s coolest features is that it lets you notify your friends as to whether you’re around. When you’re at your computer, you set your iChat status to Available; when you’re out, you set your status to Away. Conveniently for you, AppleScript can do the same thing automatically.

Of course, that’s nothing really unique. To add real power, you can have your script set your status to Away only if your screen saver is running.

Here’s how:

	--Part 1:
	on idle
	
	

	--Part 2:
	tell application “System Events”
	if (the name of every process) contains “ScreenSaverEngine” then
	
	

	--Part 3:
	tell application “iChat”
	set the status to away
	end tell
	end if
	end tell
	
	

	--Part 4:
	return 10
	end idle
	
	

Before your script will run properly, you have to select File: Save. For File Format, choose Application. Select the Stay Open option, so your script runs in the background all the time. Save the file somewhere accessible, because you can run it only by double-clicking on its icon in the Finder.

Here’s how the code works:

> Part 1 is an idle handler. To AppleScript, that means “run the following code whenever this script isn’t busy doing something else.” To you, it means the script will run constantly.

> Part 2 tells System Events to get a list of all currently running programs on your system. Then the script checks to see whether that list contains Screen-SaverEngine—OS X’s screen-saver program. In other words, part 2 checks whether your screen saver is running. If it is, the script proceeds to part 3.

> Part 3 sets your iChat status to Away if your screen saver is running. That way, your friends won’t try to chat with you.

> Part 4 tells the idle handler to check back again in 10 seconds. The result is that your script checks every 10 seconds to see whether your screen saver is running.

When your screen saver isn’t running, the only indication you’ll have that your script is running is that its icon will appear in the Dock. Because you turned on Stay Open for the script, the code continues checking your screen saver forever (or at least until you control-click on the script’s icon in the Dock and choose Quit).

[ Adam Goldstein is the teenage founder of GoldfishSoft , a software company specializing in games and utilities for OS X. Through June 1, 2005, you can get a 25 percent discount and free shipping when you buy his book AppleScript: The Missing Manual. At checkout, use discount code D5APSC. ]

recommended for you

First Look: Pages

Read more »

Subscribe to the Create Newsletter

Comments