• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Reloading PDF-document in Preview using AppleScript
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Reloading PDF-document in Preview using AppleScript


  • Subject: Reloading PDF-document in Preview using AppleScript
  • From: Stian Soiland <email@hidden>
  • Date: Thu, 3 Nov 2005 12:39:16 +0000

Hi, I'm using latex with a Makefile that does all the nasty work of
building my PDFs. I have added a makefile target "view" that depends
on the produced PDF and opens the PDF for viewing in Preview:

view: basal_ganglia.pdf
    open $<

However, as some of you might have noticed, Preview is rather stupid
when you try to open a document while it is being shown. The document
is not reloaded. Preview does have a menu option "Revert" that will
reopen the file, but unfortunately this will roll back to the very
first page. Needless to say, for a ~80 pages thesis PDF, this is not
much fun.

But what does work is to close the PDF document in Preview first, and
then reopen it. Preview will remember the old location, but use the
new file data. It is a bit important that only the relevant document
is closed, as several Preview windows could be open.

So I figured that I could make an AppleScript that closes the PDF
before it is reopened. Since Preview does not support scripting
natively, this has to be done by through System Events interaction
with the menus.

However, I'm not that fluent in these things, and I can't get it to
work. Here is close_preview.applescript, meant to be run from the
command line (ie. the Makefile). (The basis of this code was
originally posted to the OmniGraffle scripting list if I recall
correctly)


on run argv
    if length of argv is 0
        return "Usage: osascript close_preview.applescript [file]"
    end if
    set theFile to item 1 of argv
    tell application "System Events"
     if exists process "Preview" then
      tell application "Preview" to activate
      tell process "Preview"
       tell menu bar 1
        repeat with currentItem in (every menu item of menu "Window"
of menu bar item "Window" whose name contains theFile)
         click currentItem
         click menu item "Close" of menu "File" of menu bar item "File"
        end repeat
       end tell
      end tell
     end if
    end tell
end run

Running this script will active Preview, but fails to do the menu
actions, and gives a nasty error:

: stain@sandra ~/master/thesis/txt;open basal_ganglia.pdf

: stain@sandra ~/master/thesis/txt;osascript close_preview.applescript
basal_ganglia.pdf
close_preview.applescript:312:556: execution error: System Events got
an error: NSReceiverEvaluationScriptError: 4 (1)

Running a modified version[1] of the script from Script Editor, with
hardcoded set theFile to "basal_ganglia.pdf" (since argv does not work
from here), will highlight the whole repeat-loop. So I guess the
problem is this maybe-a-bit-too-complex-list filtering thing..

Any suggestion to what the evaluation script error is due to? How to
make it work? Or any other way to "fix" preview? I am quite new to
AppleScript and find the lack of proper syntax definitions and
non-descriptive error messages quite confusing..


Btw, if anyone knows how to make the she-bang-ish behaviour for an
applescript I would be thrilled.. I tried using "exec osascript
<<\EOL" or something, but then argv does not work properly so it is
not possible to pass arguments.


[1]:
set theFile to "basal_ganglia.pdf"
tell application "System Events"
	if exists process "Preview" then
		tell application "Preview" to activate
		tell process "Preview"
			tell menu bar 1
				repeat with currentItem in (every menu item of menu "Window" of
menu bar item "Window" whose name contains theFile)
					click currentItem
					click menu item "Close" of menu "File" of menu bar item "File"
				end repeat
			end tell
		end tell
	end if
end tell

--
Stian Søiland               Physics is like sex. Sure, it may
Birmingham, UK              give some practical results, but
http://soiland.no/          that's not why we do it. [Feynman]
                     =/\=
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Reloading PDF-document in Preview using AppleScript
      • From: Emmanuel <email@hidden>
  • Prev by Date: RE: Excel autorecover
  • Next by Date: Re: iPhoto "launch" bug?
  • Previous by thread: Re: Folder Actions on a Server
  • Next by thread: Re: Reloading PDF-document in Preview using AppleScript
  • Index(es):
    • Date
    • Thread