Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm working on a program that generates an image. I can keep the image open in Preview and when the image changes, Preview refreshes it, but only when I alt-tab (sorry, command-tab) to it. I'd like to keep the image in view and have it auto-refresh without touching it. Is that possible?

(One ridiculous way I've found to do this is with a script that converts the image to a PDF file which I can keep open in Skim.app (a PDF reader) which has that ability.)

share|improve this question
    
A program does not have a feature you want it to have, and using another application to solve your problem is a "ridiculous way"? – Daniel Beck Jan 12 '11 at 5:37
2  
The conversion to PDF is what makes it an ugly solution. If Skim.app could view images (like .png) it would be a great solution. – dreeves Jan 12 '11 at 7:48
up vote 6 down vote accepted

Open AppleScript Editor and enter the following:

tell application "Preview" to activate
tell application "AppleScript Editor" to activate

Replace the second application's name with the program you want to have frontmost. Save as scpt and incorporate a call to this script into your program to refresh Preview:

osascript refresh-preview.scpt

You can alternatively use Automator to create a Service that receives no input with the Run AppleScript action and the above code. Save, and assign a keyboard shortcut in System Preferences. Not exactly automatic, but easier than getting the mouse to focus Preview.


Using the program open would be another idea, although using the switch -g (don't bring to foreground) opens the file in a new preview window every time its been modified, even if it's already open. All windows get updated though, without being brought to the foreground.

share|improve this answer
    
Oh! This is perfect! It looks like open -g image.png actually does exactly what I want. It doesn't seem to open a new preview window each time. Thanks so much for the help! – dreeves Jan 12 '11 at 10:57
    
@dreeves Probably depends on the way your program handles changes to the file. I tried it with Automator and the rotate image action, and it behaved as I describe. – Daniel Beck Jan 12 '11 at 11:05

You could use applescript to give Preview focus every x seconds.

share|improve this answer

You can perform the task simply by appending the following alias to your shell startup scripts (bashrc (or similar))

alias prefresh='echo "tell application \"Preview\" to activate" | osascript -'

Then just invoke the alias when you need it.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .