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


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: 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 | # ]