Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Force Preview to reload a document Apps
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]
    •    
  • Currently 3.67 / 5
  You rated: 5 / 5 (6 votes cast)
 
[21,345 views]  

Force Preview to reload a document | 37 comments | Create New Account
Click here to return to the 'Force Preview to reload a document' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Force Preview to reload a document
Authored by: gustou on Jan 05, '06 07:01:39AM

Did you try the pdf viewer of TexShop ? This one is updates himself as soon as the document changes and if there was no compilation errors then it reopens the previous page.... Really cool you should give it a try !!!



[ Reply to This | # ]
Force Preview to reload a document
Authored by: paperwings on Jan 05, '06 07:46:40AM

Texshop has its place, but some of us *nix converts still like to do things from the command line. I personally still prefer vim as an editor, as (with some experience, of course) I can do pretty much anything and more with vim that I can with a gui editor. Refreshing Preview would be ideal for using pdflatex or dvipdf. For now, X11+xdvi works well (pressing ^r or just changing the page in xdvi refreshes).



[ Reply to This | # ]
Clarification on TeXShop with external editors
Authored by: Han Solo on Jan 05, '06 08:38:22AM
The command line has its place, but you should try the GUI some time. I know the thought of interacting with the GUI is painful for those of us with Unix backgrounds (been there, done that) but it's, like, a main advantage of the Mac. ;) If you're not a fan of the TeXShop editor itself — which has some nice (La)TeX-specific customizations that do not require a Ph.D. in Comp Sci to install — may I recommend TextMate?

As for gustou's comment, I think the suggestion is to configure TeXShop to be your PDF viewer application. This feature is built into TeXShop: take a look under Preferences. Then one can use Vim or Emacs or any other cool retro 1980s-style editor — or TextMate, BBEdit, etc. for that matter — to "interact" with TeXShop-cum-viewer, and reloading of updated PDF output from (La)TeX works fine... no AppleScript hacks necessary. (Which is not to denigrate this otherwise useful hint.)

[ Reply to This | # ]

Clarification on TeXShop with external editors
Authored by: simonpie on Jan 05, '06 08:53:51AM

I would like to add to this that texshop is a real pdfpreviewer for latex coder whilst preview is not. Texshop offers the magnifying glass on double and tripple click which is a must for anyone who writes mathematics. And texshop does syncronise well with emacs or carbon emacs. Texshop is also much faster than preview, another must for large document and compulsive recompilers like myself.



[ Reply to This | # ]
Except that it does not work...
Authored by: hamarkus on Jan 05, '06 09:44:09AM

TeXShop does not update pdf files that are changed by an external application.

Opening a pdf file from Matlab with TeXShop (via 'system('open -a Texshop filename.pdf')) does not cause TeXShop to update the display. One first has to close the pdf file in TeXShop for any changes to become visible.

I wish I knew the (shell) command to close the file directly from Matlab.



[ Reply to This | # ]
Trying to use the script from Matlab
Authored by: hamarkus on Jan 05, '06 10:03:04AM

Just out of fun, I tried to open a pdf file from Matlab with this script:

system('open Reload_Preview_Document.scpt filename.pdf');

It opens first the script editor and then the pdf file in Acrobat, seconds later both programs collapse.



[ Reply to This | # ]
Except that it does not work...
Authored by: Spartacus on Jan 10, '06 02:18:43PM

And what is pdflatex, if not an external application?



[ Reply to This | # ]
Except that it does not work...
Authored by: ehmsen on Dec 02, '06 06:49:33AM

Try opening the preferences, select the Preview tab and check the box "Automatic Preview Update" under "External Editor" :-)



[ Reply to This | # ]
Clarification on TeXShop with external editors
Authored by: vocaro on Jan 05, '06 06:35:05PM

The command line has its place, but you should try the GUI some time.

The GUI is not sufficient for LaTeX users like myself who rely on fine-tuned Makefiles for their workflow. Mine checks all the dependencies, generates a PDF, and then displays it in one shot. Using the GUI as you suggest would add an unnecessary extra step to this process.



[ Reply to This | # ]
Force Preview to reload a document
Authored by: rhowell on Jan 05, '06 07:18:46AM

Thank you so much for this. I have always wanted a minimal latex suite, consisting of latex->dvipdf->Preview. No additional 3rd party software, thank you.

Now, I wonder if there's a way to adjust the script so that it can reload the front-most Preview window, and then bind it to a keystroke like Command-Option-R. That way, it'll almost have the behavior of latex->xdvi. That would be awesome.



[ Reply to This | # ]
Force Preview to reload a document
Authored by: dhosek on Jan 05, '06 08:37:41AM

You may as well switch to pdflatex then, cut out a step in your system...



[ Reply to This | # ]
Force Preview to reload a document
Authored by: Han Solo on Jan 05, '06 08:43:34AM
To bind keystrokes to scripts, you could use Butler, Quicksilver, iKey, or any of a dozen or so possible apps....

Oh wait: you don't want to install any third-party apps. Right....

[ Reply to This | # ]

[OT] Why no third party software?
Authored by: derrickbass on Jan 05, '06 11:11:50AM

Can someone explain this to me? I've never quite understood the whole "no third-party software" attitude. I mean, if it's a haxie or kernel extension or something, then sure, it could destabilize the system. But a plain ol' app? Why would you go out of your way to avoid it?



[ Reply to This | # ]
[OT] Why no third party software?
Authored by: hamarkus on Jan 05, '06 11:38:52AM

Maybe to reduce complexity and clutter?
With about 100 pieces of third party software on my computer it becomes almost a full time job to keep track of all updates to them.



[ Reply to This | # ]
[OT] Why no third party software?
Authored by: vocaro on Jan 05, '06 06:40:18PM

With about 100 pieces of third party software on my computer it becomes almost a full time job to keep track of all updates to them.

I had the same problem until I discovered VersionTracker Pro. It does a great job of scanning my system and alerting me to any new updates of my software, which saves me a lot of time because I don't have to constantly check for updated packages or worry that I might have out-of-date software. It's $40/year but worth every penny IMO.



[ Reply to This | # ]
Widget to do the work for free
Authored by: SeanAhern on Mar 22, '06 05:51:22PM

I've found that the Dashboard Widget "App Update" seems to do a similar job for free.



[ Reply to This | # ]
[OT] Why no third party software?
Authored by: vocaro on Jan 05, '06 06:47:51PM

Can someone explain this to me? I've never quite understood the whole "no third-party software" attitude.

I can't speak for rhowell, but in this case, I simply didn't like any of the third-party replacements for Preview. Some required a fee and were overpriced for what they offered, and others just didn't offer any advantages over the existing Preview (other than the ability to automatically reload changed documents). I like third-party software in general -- I have around three dozen such programs on my Mac -- but in this case none of them did what I wanted.



[ Reply to This | # ]
Force Preview to reload a document
Authored by: stochasticus on Jan 05, '06 08:16:59AM

Please excuse my stupidity but where do you need to put scripts like this so that they can be run from anywhere from the terminal? I found that the script worked, and is just what I need, but only when the script was in the same directory as I was in when running it.



[ Reply to This | # ]
Force Preview to reload a document
Authored by: vocaro on Jan 05, '06 06:52:33PM

Please excuse my stupidity but where do you need to put scripts like this so that they can be run from anywhere from the terminal?

You should be able to put them anywhere as long as you reference them correctly (e.g., quoting the directory if it contains spaces). For instance, here's what I do on the command line:

osascript "/Users/trevor/Development/AppleScript/Reload Preview Document.scpt" document.pdf

[ Reply to This | # ]
Force Preview to reload a document
Authored by: Christoph on Jan 06, '06 12:11:58AM
The environment variable PATH consists of a colon seperated list of directories that are searched for commands. Hence you can proceed as follows: 1) create a directory ~/bin with
mkdir $HOME/bin
2) put your private scripts therein 3) add this directory to your search path with

PATH="$PATH:$HOME/bin"
export PATH
The commands under 3) should go into a file that is loaded whenever the Terminal is started. I think $HOME/.profile should work (at least on my system it works because I do not use Terminal.app but "xterm -l"). As I have noticed getting the profile actually read can be a bit tricky and it will be ignored when you start applications from the Aqua GUI.

[ Reply to This | # ]
Force Preview to reload a document
Authored by: stochasticus on Jan 06, '06 02:16:45AM

Thanks but I know about *nix paths already. I don't think osascript does though so you need to put the full path. I've done something similar to Doc Drang below and written a shell script to make it easier.



[ Reply to This | # ]
Force Preview to reload a document
Authored by: Doc Drang on Jan 05, '06 08:18:06AM
Thanks, it's quite fast for an AppleScript. I've put my copy in my Library/Scripts folder and written this shell script
#!/bin/bash
osascript '/Users/me/Library/Scripts/Reload Preview Document.scpt' $1
which I call "repreview" and have put in my $PATH. Now I can call
repreview document.pdf
One other thing: since I already have the "Enable access for assistive devices" option checked, I commented out the "my UIscript_check()" line in the "on run" portion of the script.

---
Doc Drang
http://www.leancrew.com/all-this

[ Reply to This | # ]

Force Preview to reload a document
Authored by: hamarkus on Jan 05, '06 10:08:02AM

Can you now call this script from Matlab:
system('repreview document.pdf')

I had some trouble with that (see my comment further up).



[ Reply to This | # ]
Force Preview to reload a document
Authored by: hamarkus on Jan 05, '06 10:21:34AM

I removed the UI-scripting check (and modified the shell scripting part withing the OSA "open -a Preview" instead of "open"), it still opens Acrobat, Acrobat does not crash anymore but the scripteditor is still opened (and stays open).



[ Reply to This | # ]
Force Preview to reload a document
Authored by: FFFish on Jan 05, '06 10:02:48AM

Am I barking up the wrong tree when I suggest "File > Revert" does exactly what y'all want? It reloads the file, thus showing any changes since the last load.



[ Reply to This | # ]
Force Preview to reload a document
Authored by: dw on Jan 05, '06 10:48:54AM

Ah! That is wonderful (and slightly hilarious). Preview "reverts" to the more recently saved version.



[ Reply to This | # ]
Force Preview to reload a document
Authored by: osxpounder on Jan 05, '06 12:15:31PM

If "Revert" does, indeed, simply reload the file, then it's badly named. It should be called "Reload", of course. When I see "Revert", I assume it works as the name describes: it reverts to an earlier state, not to the file currently on disk. When I hit "Revert" in Photoshop, I count on PS restoring the document as it was when I opened it, regardless of what I've saved to disk since opening.

---
--
osxpounder



[ Reply to This | # ]
Force Preview to reload a document
Authored by: vocaro on Jan 05, '06 08:37:37PM

Am I barking up the wrong tree when I suggest "File > Revert" does exactly what y'all want? It reloads the file, thus showing any changes since the last load.

Interesting; I always thought Revert simply reverted changes. I never realized it reloaded the document. However, my script is still useful for two reasons:

  1. A script can be called from the command line, which is useful for LaTeX users like myself who rely on fine-tuned Makefiles for their workflow.
  2. The Revert command resets Preview to the first page of the document, which is very annoying when you're working on, say, page 17 and suddenly you're looking at page 1 and have to jump back. On the other hand, closing and re-opening the document, which is what my script does, keeps Preview on the same page of the document.


[ Reply to This | # ]
Force Preview to reload a document
Authored by: alblue on Jan 05, '06 02:37:59PM

Firstly, you don't need a script to do this. There's a Revert menu under the File view, and this allows you to reload the document. It's also trivial to bind it to a keystroke, since you can open up System Preferences, go to the Keyboard and Mouse pane, and then in the Keyboard Shortcuts page, click on [+] to add a new shortcut. Simply choose 'Preview' and then type in 'File' and 'Revert' in the boxes (assuming that you're running in English) and a suitable keystroke (I used Command+Shift+R). IIRC you have to restart Preview to get the new binding (after saving the keystrokes by closing System Preferences first!).

Secondly, the reason it's 'Revert' rather than 'Reload' is because you can make changes in Preview. If you do Command+L or Command+R, the PDF document is rotated. Alternatively, you can use the annotate tool (Command+4) to put a 'post-it' style note onto the PDF. Either of these options changes the document, and the close button shows the red zit with the dot in the middle to indicate that the document has changed. You can save it in this new orientation, or with notes on, or in a variety of different formats (e.g. Jpeg/Gif/Png...). In other words, it allows you to revert your changes to the current version on disk (even if that means you haven't had any changes).



[ Reply to This | # ]
Force Preview to reload a document
Authored by: rhowell on Jan 05, '06 03:46:09PM

Brilliant! Thank you for clearly explaining this, step-by-step. This is the solution I've been looking for, as others have I'm sure.

To be clear, in System Preferences->Keyboard & Mouse->Keyboard Shortcuts, the "Menu Title" one adds after clicking the plus (+) button is "Revert" (without the quotes). No "File" should be there.

A quick test to see if your keyboard shortcut has been properly assigned (it took me a while to find one that works), is to then quit System Preferences, quit and reload Preview, and select the File menu. The shortcut will appear next to revert if successful.



[ Reply to This | # ]
Force Preview to reload a document
Authored by: vocaro on Jan 05, '06 08:44:20PM

There's a Revert menu under the File view, and this allows you to reload the document. It's also trivial to bind it to a keystroke

Interesting; I always thought Revert simply reverted changes. I never realized it reloaded the document. However, my script is still useful for two reasons:

  1. Even though you can bind Revert to a keystroke, programmatically reloading a document with a script is useful for LaTeX users like myself who rely on fine-tuned Makefiles for their workflow. For instance, instead of running "make tex" and then typing, say, Command+Shift+R to reload, I can just do "make show" which rebuilds the document and reloads it, saving me a step.
  2. The Revert command resets Preview to the first page of the document, which is very annoying when you're working on, say, page 17 and suddenly you're looking at page 1 and have to jump back. On the other hand, closing and re-opening the document, which is what my script does, keeps Preview on the same page.


[ Reply to This | # ]
Force Preview to reload a document
Authored by: nxg on Jan 09, '06 04:06:58AM

> Simply choose 'Preview' and then type in 'File' and 'Revert'
> in the boxes (assuming that you're running in English) and
> a suitable keystroke (I used Command+Shift+R).

For those of us who are hard-of-thinking, can you be even more explicit?

In that System Preference, there's a single `Menu Title' field. I tried entering "File Revert" (without the quotes), and "'File' 'Revert'", and "File/Revert", but none seem to make any difference, when bound to Control-Command-R. The field has as `explanation' the text `Enter the exact name of the menu command you want to add' which, like AppleScript, gives off an air of being friendly without being in the least bit informative.

Separately, Command-Shift-R appears to be already taken, by the View/Automatically Resize command. Will a System Preference binding of the same keystroke override that?

Thanks for pointers



[ Reply to This | # ]
Force Preview to reload a document
Authored by: peterbacsi on Jul 09, '08 09:28:16AM

My Preview (version 3.0.9) is buggy, since after using Revert,
when I want to navigate to another part of the document using
the drawer, it crashes. The GUI script does not have this problem
as it does not go through Revert.




[ Reply to This | # ]
Force Preview to reload a document
Authored by: theegor on Jan 09, '06 02:04:31AM
I wrote a new script, based on alblue's hint. It requires that you setup alblue's keyboard shortcut:
Firstly, you don't need a script to do this. There's a Revert menu under the File view, and this allows you to reload the document. It's also trivial to bind it to a keystroke, since you can open up System Preferences, go to the Keyboard and Mouse pane, and then in the Keyboard Shortcuts page, click on [+] to add a new shortcut. Simply choose 'Preview' and then type in 'File' and 'Revert' in the boxes (assuming that you're running in English) and a suitable keystroke (I used Command+Shift+R). IIRC you have to restart Preview to get the new binding (after saving the keystrokes by closing System Preferences first!).

My script either loads the document, or reverts it, and makes it the foremost window. Additionally, it jumps to the page that was last viewed (but it assumes that your Preview has the standard toolbar layout so that it can tab around the tool bar, because AppleScript sucks).

I integrate this script into my LaTeX build process. I execute: make preview-paper.pdf where I'm compiling paper.tex to paper.pdf. The Makefile has a rule like this:


preview-%: %.pdf
        @osascript tools/preview-revert.scpt $<

and the following AppleScript is saved as tools/preview-revert.scpt:


on run params
	set filename to item 1 of params
	set PWD to system attribute "PWD"
	set full_path to POSIX file (PWD & "/" & filename)
	
	tell application "Finder"
		--- Opens the document, and makes it the active window.
		open full_path
	end tell
	tell application "Preview"
		--- Ensure that Preview is the front application.
		activate
	end tell
	
	tell application "System Events"
		tell process "Preview"
			--- I assume that you have created an application shortcut
			--- to invoke Revert when pressing Command-Shift-R.
			keystroke "R" using {command down, shift down}
			--- Restore the original page.  I assume that you
			--- have the standard toolbar layout.
			tell window 1
				keystroke tab & tab & tab & return
				keystroke tab & tab & tab & tab using {shift down}
			end tell
		end tell
	end tell
end run

Now I want to cleanse myself of AppleScript (maybe write some nice x86 assembler). Hopefully I'll never again have to use this horrible AppleScript language.

[ Reply to This | # ]

Preview useless for LaTeX
Authored by: lee1 on Jan 09, '06 07:40:38AM

I've found that Preview (as of v. 2.1.0) has two problems that make it useless for displaying the pdfs that I generate from LaTeX. First, it fails to display certain kinds of pdf pictures correctly: for example, imagemaps made with gnuplot are displayed in Preview with strange artifacts; second, Preview does not handle certain pdf features such as movies referenced with a pdfannotation and page-flipping instructions.

I don't know about the most recent versions of Techniscope or TexShop, but fairly recent versions of these also did not handle enough of the pdf spec to be useful. Acrobat Reader handles all this fine, so, although I don't like the interface, that's what I'm left with.



[ Reply to This | # ]
Force Preview to reload a document
Authored by: flyingillini on Dec 08, '09 09:28:56AM

This thread is the top hit on google for this topic, so I thought I'd update it. Preview seems to reload a document if it has changed if you switch focus away from it and then switch back to it. So it seems preview works (almost) perfectly with pdflatex now! I say almost because it would be better if preview would detect the change and reload while it has focus.



[ Reply to This | # ]
Force Preview to reload a document
Authored by: danjo on Nov 13, '11 05:06:02AM
Adding to flyingillini's comment, here is a short AppleScript, which will toggle focus on Preview to force it to reload.

set frontApp to (path to frontmost application as Unicode text)
if "Preview" is in frontApp then
  tell application "Finder" to activate
  tell application "Preview" to activate
else
  tell application "Preview" to activate
  tell application frontApp to activate
end if


[ Reply to This | # ]