I’m a newb at applescript so what I’m trying to do may be super easy.
Right now I have two different ways of opening PDF’s. I would rather use the first one as it will open any PDF reader that the user has. But in neither of them can I figure out how to open to a specific page.
First script opens using any app.
tell application "Finder"
open file "testdisk:short.pdf"
end tell
Second script opens specific app. And I can set the size, but don’t know the goto page code.
tell application "Adobe Acrobat Professional"
open file "testdisk:short.pdf"
zoom first PDF Window to 150
end tell
The script
tell application “Adobe Reader”
open file “testdisk:short.pdf” options “page=4”
end tell
does not work. When I paste this script, the Apple script editor refuses to compile with the error pointed at “options”.
My Adobe reader version is 10.1.3 and I need to open a particular PDF and jump to a page when “Page” is clicked or jump to chapter when “chapter” is clicked. Please help. Have spent the entire day without results.
Mac OS - 10.6.8
Even if someone can point how to do the opening of a PDF file using Preview or Acrobat Reader from terminal will also be useful. Currently I can simply open the PDF using these apps at the terminal, but not able to JUMP to a specific page or chapter.
If anyone here can help you, they will. Importuning or urging importance won’t make any difference.
You can with Adobe Reader 9.4.1, so maybe it’s just “Adobe Reader X” which is lacking in that respect.
The desired effect can also be obtained in Adobe Reader 9.4.1, or Preview 5.0.3, by means of GUI scripting, which requires “Enable access for assistive devices” to be enabled in System Preferences’s “Universal Access” pane.
set pageNum to "4"
tell application "Adobe Reader" to open (choose file)
tell application "System Events"
tell application process "Adobe Reader"
set frontmost to true
set windowCount to (count windows)
-- Execute an option-command-"n" keystroke to get the "Go to page." dialog.
keystroke "n" using {shift down, command down}
-- The dialog's another window in Adobe Reader 9.4.1. Wait for it to appear.
repeat until ((count windows) > windowCount)
delay 0.2
end repeat
-- "Type in" the page number and press Return.
keystroke pageNum
keystroke return
end tell
end tell
Or:
set pageNum to "4"
tell application "Preview" to open (choose file)
tell application "System Events"
tell application process "Preview"
set frontmost to true
-- Execute an option-command-"g" keystroke to get the "Go to page." dialog.
keystroke "g" using {option down, command down}
-- The dialog's a sheet in Preview 5.0.3. Wait for it to appear.
repeat until sheet 1 of window 1 exists
delay 0.2
end repeat
-- "Type in" the page number and press Return.
keystroke pageNum
keystroke return
end tell
end tell
The solution poses a problem that the Acrobat should be installed and available on Mac. I have two questions
Is there no way that I can achieve this through Preview app? Does Preview not have its internal dictionary?
Can I just do what the script seems to do through Terminal on Mac? On Windows a simple command at the command prompt does this easily as
/A page=2 “E:\My.pdf”
Why cant this work at the terminal? I can launch the application and make it open the PDF, but cannot jump to the page desired as that is the main requirement
If you google a little (this is from memory) you can make a en entry in the property list file … info.plis to make it scriptable but there isn’t much which will be revealed to you, to make you happy.
And if you are on Lion, you are sort of jailbreaking the app, due to code signing, so then you have to make a copy of first, in order for the trick to work.
There is however a free solution named Skim.app, which is a pdf viewer of merit, that is is very userfriendly, and is fully scriptable.
I haven’t tried to script it myself, but it should be a lot examples out there for you to look at, and maybe even you are in luck with specifying a page from the terminal window, I would believe so!
-- Tested in Mac OS 10.6.8.
on run {pathToPDF, pageNum}
set pdfFile to pathToPDF as POSIX file
tell application "Preview" to open pdfFile
tell application "System Events"
tell application process "Preview"
set frontmost to true
-- Execute an option-command-"g" keystroke to get the "Go to page." dialog.
keystroke "g" using {option down, command down}
-- The dialog's a sheet in Preview 5.0.3. Wait for it to appear.
repeat until sheet 1 of window 1 exists
delay 0.2
end repeat
-- "Type in" the page number and press Return.
keystroke pageNum
keystroke return
end tell
end tell
end run
. it can be invoked from the Terminal like this ” say, to open ‘myPDF.pdf’ at page 4:
And then we learned, or became aware of (maybe again), that GUI scripting via System Events works even if applications aren’t made scriptable. How comforting nowadays!
Great script Nigel! Snagged! for the record, I think I did not see your post when I posted mine!
Nigel, I appreciate your feedback, it was just a state of mind that I had so many things pending and here I was back to scripting after 6 yrs. Will take care while posting in future.
Actually, Nigel, your post was missing and I could see only “ame’s” solution and hence did not acknowledge your solution.
Thanks to all who contributed to help me solve the issue.
This is a particular useful solution when making latex documents, when I want to inspect the output of a certain page.
I also see the usage for this, when jumping to predfined pages in a shell script.
So I wrapped a shell script around it. The filename should be within ticks or double ticks if it contains spaces.
You must also chmod u+x the script from the commandline in order for it to work, and supply two arguments.
Maybe I’ll reinforce it with help and parameter checking later.
osascript <<END! - “$@”
on run {pathToPDF, pageNum}
set pdfFile to pathToPDF as POSIX file
tell application “Preview” to open pdfFile
tell application "System Events"
tell application process "Preview"
set frontmost to true
-- Execute an option-command-"g" keystroke to get the "Go to page." dialog.
keystroke "g" using {option down, command down}
-- The dialog's a sheet in Preview 5.0.3. Wait for it to appear.
repeat until sheet 1 of window 1 exists
delay 0.2
end repeat
-- "Type in" the page number and press Return.
keystroke pageNum
keystroke return
end tell
end tell