Add AppleScript support to Preview

AppleScript is one of those technologies that has amazing potential, but seems to be “not quite there yet” in many ways. The basic problem is that supporting AppleScript is up to the application’s authors, not the OS. So in order for an application to be scriptable, the developers must provide the support for it. As a result, we wind up with many apps that won’t speak a word of AppleScript— Quicken, for instance. Many other apps, such as Firefox, have just the bare necessities, making it difficult to write useful scripts for these programs.

OK, so some third-party apps have poor AppleScript support. Surely that’s not a problem for Apple’s own apps, right? Not completely right. Some apps, such as iPhoto and iTunes, have strong support for AppleScript. Others, though, lack support entirely— iMovie, Backup, Calculator, etc. Perhaps the biggest surprise, though, is that Preview is on the list of non-supported applications. Given its importance in opening nearly any kind of image on your machine, this seems like a fundamental oversight. For instance, consider this simple AppleScript, which tries to close an open window in Preview:

	tell application "Preview"

close window 1
end tell

Try to run that in the Script Editor with an image open in Preview, and you’ll get this AppleScript error:

Preview got an error: window 1 doesn’t understand the close message.

That’s because Preview doesn’t support even the basic AppleScript dictionary. So here’s the tip—you can add basic (not perfect, but functional) AppleScript support to Preview—or any other application which lacks it. You won’t gain full access to the app via AppleScript, of course, but you will gain access to the standard AppleScript suites—Standard Suite, Text Suite, and Type Definitions. These basic suites will let you do things like manipulate windows, print images, open and close files, etc.

So how do you add this support? There are two methods—one via Terminal, and the other through (fittingly) an AppleScript. Before using either, quit Preview if it’s running. And just to be safe, make a copy of Preview and name it “Preview Original Version” or somesuch.

Terminal method

Open Terminal (in /Applications -> Utilities) and run this command:

defaults write /Applications/Preview.app/Contents/Info NSAppleScriptEnabled -bool YES

That’s it for the Terminal method—not too hard, was it?

AppleScript method

The AppleScript method merely implements the Terminal solution above as an AppleScript. Open Script Editor (in /Applications -> AppleScript), and enter this code:

	try

tell application "Finder"
set the Preview_app to (application file id "com.apple.Preview") as alias
end tell
set the plist_filepath to the quoted form of ¬
((POSIX path of the Preview_app) & "Contents/Info")
do shell script "defaults write " & the plist_filepath & space ¬
& "NSAppleScriptEnabled -bool YES"
end try

Click Compile, then Run. That’s it, you’re done. (Thanks to Apple’s AppleScript guru, Sal Soghoian, for posting the above script as a comment to this post on the red sweater blog.

What you did

Regardless of which method you used, you just told the system to modify the Info.plist file for Preview, and add one boolean (

YES
or
NO
) variable ( NSAppleScriptEnabled ), and set it to
YES
(enabled, in other words). Just by doing that, you’ll gain a fair bit of AppleScript functionality in Preview.

As an example, launch Preview, open an image, and then try the original AppleScript again:

	tell application "Preview"

close window 1
end tell

Instead of an error, the window will simply vanish. Nice! You can use this same technique to add basic AppleScript support to other applications, though you’ll have the best luck with Cocoa applications (as their basic AppleScript support is much nicer than that of Carbon applications). Read this post on the red sweater blog for more info on this tip, including a more advanced Preview script that takes advantage of the newly-added AppleScript support.

Of course, if anything goes wrong, you can just delete your modified application and rename the backup you created before you started. You did create one first, right?

Shop Tech Products at Amazon