Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScript, Preview, and PDF Page access

Hello,


Writing an AppleScript for Preview to be used with a PDF, how does one access a particular page (identified by serial page number) or get each page by iterating through its pages? I could not find anything either in AppleScript's online manual or Preview's AppleScript dictionary. Nor could I find any meaningful Preview example scripts. (It is possible to do so in both PDFPen and Skim.)


However, I think there must be a way to do what I am trying to because AppleScript's concept is that everything from a single character in one document all the way up to the hard drive is an object, and that each and every object falls within a hierarchy of objects. Therefore, a PDF's page is an object that falls between, I suppose, some component of the page (paragraph?) and the PDF file, and, therefore, it has to be accessible.(?)


Thanks,

kds-kds

MacBook Air, macOS Sierra (10.12.6)

Posted on Jan 12, 2018 1:57 AM

Reply
4 replies

Jan 12, 2018 4:38 AM in response to KDS-KDS

What are you actually attempting to do with the PDF document?


The AppleScript dictionary support designed for Preview by Apple is the limiting factor on what you can, and mostly can not do. Conceptual AppleScript organization and objects has nothing to do with it.


If you want to directly address a given page in Preview, then you must resort to System Events GUI scripting, which could break with the next update to Preview. Without System Events, you could not address a page in the PDF because the Preview scripting dictionary does not directly support a page entity.


tell application "Preview"

activate

tell front document

tell application "System Events"

keystroke "g" using {option down, command down} -- Go menu : Go to Page…

keystroke 3 -- page 3

delay 1

keystrokereturn

end tell

end tell

end tell

Although the Skim application also uses Apple's PDFKit Framework, they wrote a far more robust scripting dictionary for the product, and additionally, provide a command-line tool that can also be used in Bash scripts to extract/merge/embed ranges of PDF pages.


In Automator, the PDF library provides you with a series of actions that are not available to you in AppleScript:

User uploaded file

Jan 14, 2018 6:02 AM in response to VikingOSX

Thanks for the reply.


You wrote: "What are you actually attempting to do with the PDF document?" and "Although the Skim application also . . ." I am trying to write a page-numbering script in Preview, which, oddly enough, I have already done in Skim (as annotations of course, which become part of the [new] PDF upon Print->Save as PDF). In Preview I cannot obtain even the count/number of pages.


The problem is that I also need to annotate the same PDFs I need to page-number, but annotations (manually, individually) using Skim are just about the biggest hassle I have experienced using any PDF markup/annotation app, even the el cheapo ones. They're straightforward and hassle-free using Preview so I thought I would do both, manual annotations and page-numbering, in a single app. (The inexpensive PDF apps do not support AppleScript at all.)


I am at the point of concluding that page-numbering cannot be done in Preview. It blows my mind that Preview's Standard Suite lacks a Page object (how can an app that is primarily and frequently used for PDFs not have a Page object in that app's scripting dictionary?) If you look at Preview's Standard and Text Suites, the impression one would come away with is that Preview supports only single-page PDFs.(!) Or have I missed Preview's page-based access somewhere?

Jan 14, 2018 6:33 AM in response to KDS-KDS

The Preview application doesn't have any unique scripting terminology, and as far as I can recall, it never has. The application has an Info.plist setting that tells the system that it is scriptable, so the system includes a standard suite, but it has no terminology of its own. You are limited to those base terms, which are application agnostic, and GUI scripting, I'm afraid.

Jan 14, 2018 6:44 AM in response to KDS-KDS

Though Skim and Preview use the underlying Apple PDFKit framework, they are designed, and implemented differently. Neither are PDF editors, which can be found in the Mac App Store, and which might make quick work of your current page numbering challenge.


I mentioned that the AppleScript Scripting dictionary for Preview omits any page entity (and nearly everything else). Both red_menace and I agree that AppleScript solutions with Preview are quite limited.

AppleScript, Preview, and PDF Page access

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.