Force Preview to reload a document

Jan 05, '06 05:53:00AM

Contributed by: vocaro

The Preview utility has a known bug in which an open document whose contents have changed can't be refreshed unless you close and reload the document. This is especially frustrating for LaTeX users who like to use Preview for tracking the progress of their work.

Drop-in replacements for Preview, such as TeXniscope and PostView can solve this problem by automatically detecting modifications in a document and reloading it. But TeXniscope isn't as versatile as Preview, and PostView requires a paid license.

All I really needed was a way to tell Preview to reload the document I'm working on. AppleScript is the obvious solution here, but apparently Apple is a bit of a hypocrite: Despite claiming that scripting support has 'never been easier' to implement, their own Preview utility remains totally unscriptable.

Well, sort of. Luckily, AppleScript's GUI Scripting can script the unscriptable. Though somewhat of a hack, GUI Scripting controls applications by selecting menu items rather than calling pre-defined AppleScript commands. Using this technique, I was able to write a script that tells Preview to close and then reopen a given document, thereby refreshing any changes in my LaTeX source code.

After downloading my script, Reload Preview Document, you can use the following command to reload a given document in Preview:

  osascript "Reload Preview Document.scpt" document.pdf
This solution isn't ideal for GUI users, but it's great for command-line junkies like me who use LaTeX a lot.

[robg adds: I tested this, and it works. I have also mirrored the script here on macosxhints.com]

Comments (37)


Mac OS X Hints
http://hints.macworld.com/article.php?story=2006010200141989