AppleScript Preview in Snow Leopard and Lion
The one app that didn’t seem to support AppleScript control over window location was the one app it would have been most useful in: the built-in Preview application on Mac OS X.
It turns out, Preview does support AppleScript, but that support is turned off by default. Thanks to Trash Man at MacScripter.net for the commands that turn it back on:
- sudo defaults write /Applications/Preview.app/Contents/Info NSAppleScriptEnabled -bool true
- sudo chmod 644 /Applications/Preview.app/Contents/Info.plist
- sudo codesign -f -s - /Applications/Preview.app
The first line enables AppleScripting Preview; the next two lines are necessary in Snow Leopard, Lion, and, presumably, above, because starting with Snow Leopard Mac OS X signs the built-in applications. When you change the default, that changes the signature, and Preview will fail to start because its signature doesn’t match what the OS expects. The next two lines re-sign Preview so that the OS will accept it.
If you use Leopard or earlier, you should only need the first line.
Once you enable scripting, we can stack PDFs or other documents in Preview directly on top of each other for easy comparison using command-~ document switching.
[toggle code]
-
tell application "Preview"
- copy (every window whose visible is true and resizable is true) to windowList
- copy the bounds of the first item of windowList to windowBounds
-
repeat with aLowerWindow in the rest of windowList
- set the bounds of aLowerWindow to windowBounds
- end repeat
- end tell
Works great for comparing the differences between an original image and an optimized image, or for minor differences in the text of a PDF document.
Note that this is an improvement on the script in the earlier article. In the earlier script, if the first window happened to be an invisible or unmodifiable one, the script might end up resizing all of them to be too small to work with.
In response to Stack windows on top of each other: If you want to stack multiple windows directly on top of each other, it’s easy to do in any well-behaved application, such as Nisus Writer Pro, Safari, Mail, and even older applications like AppleWorks 6 and Microsoft Word X.
- Scripting Preview
- “Does anyone out there know if there is a way to give Preview commands without driving the GUI like a service or can it accept a command line?”
More AppleScript
- Copying album art with GraphicConverter
- One of the many useful features of GraphicConverter is its extensive AppleScript dictionary. I use it, along with Unskew, to use photos of album covers as album art in iTunes.
- Apple Mail on the Desktop with GeekTool
- Here’s a simple AppleScript to use with GeekTool to put your inbox on the Desktop.
- Stack windows on top of each other
- If you want to stack multiple windows directly on top of each other, it’s easy to do in any well-behaved application, such as Nisus Writer Pro, Safari, Mail, and even older applications like AppleWorks 6 and Microsoft Word X.
- Getting the selected playlist in iTunes
- It took a while to figure out how to get iTunes’s selected playlist as opposed to the current playlist in AppleScript.
- Converting FileMaker to Django
- Appscript and the Django API make it easy to transfer data from Mac OS X scriptable applications into a Django-powered database.
- 10 more pages with the topic AppleScript, and other related pages
More Mac OS X tricks
- Enable AirPrint for all connected Mac printers
- I have an iPad and an old workhorse of a printer, an HP 1012 LaserJet, connected to my iMac. I almost never need to print from the iPad, but when I do, handyPrint works.
- Quality compressed PDFs in Mac OS X Lion
- The instructions for creating a “reduce PDF file size” filter in Lion are the same as for earlier versions of Mac OS X—except that for some reason ColorSync saves the filter in the wrong place (or, I guess, Preview is looking for them in the wrong place).
- Command-line mail on OS X: re-alpine and Geektool
- If you do a lot of automated command-line scripts, you probably also generate a lot of mail to /var/mail. OS X only has the mail program built-in, and its GUI mail client hasn’t been able to add simple mail accounts since about OS X 10.2. Alpine can get you a better mail client, and Geektool can provide better notices.
- Stack windows on top of each other
- If you want to stack multiple windows directly on top of each other, it’s easy to do in any well-behaved application, such as Nisus Writer Pro, Safari, Mail, and even older applications like AppleWorks 6 and Microsoft Word X.
- Leopard setuid and passwd file changes
- Leopard Server introduced two major changes to two lesser-used features: setuid root script wrappers and BSD flat file authentication.
- 10 more pages with the topic Mac OS X tricks, and other related pages

Update August 30, 2012: added “and resizable is true” to window query.