Preview crashes when reopening PDFs created with LaTeX

Alec Jacobson

March 17, 2013

weblog/

To typeset documents I use pdflatex. I edit my tex files using vim and build the pdfs using a makefile. At the end of my make sequence I like to have:
open my.pdf
Which on my mac causes preview to open my pdf document. When I recompile and this command runs, sometimes preview just focuses the already open document, but other times it opens a new window. I haven't backwards engineered how it decides which to do, but it might be correlated with the amount of change in between. In any case, Preview recently has been crashing when it tries to open the same document in a new window. For small, short-term articles this is not so annoying. But now, writing my t****s, I'm really getting frustrated. I wrote an applescript to replace the open command above. First you'll need to enable applescript for Preview.app. Then you can save the following in preview.scpt.
on run argv
	set file_name to item 1 of argv
	try
		tell application "Preview"
			activate
			set open_already to false
			repeat with this_doc in every document
				set open_already to open_already or (name of this_doc as string is equal to file_name)
				if open_already then
					return "open already"
				end if
			end repeat
			if not open_already then
				open (do shell script "pwd") & "/" & file_name
				return ""
			end if
		end tell
	on error errMsg
		return errMsg
	end try
end run
You can run with:
osascript preview.scpt my.pdf
The rerun to see that no action is taken.