Given a file path, I need to rotate all the pages in a PDF at that file path using nothing that isn't included with macOS. The wrapper for this will be AppleScript, so I also have access to the command line and therefore all of the scripting languages installed by default with macOS, but nothing that would require brew
or, for example, Python's pip
.
How to programmatically rotate all the pages in a PDF using native macOS tools
applescriptpdfterminal
Related Question
- How to find the application name to open a file using AppleScript
- How to tell an Automator action, that the source path of a document should also be the destination path
- Python path added to $PATH — cannot find origin
- How to copy multiple files to macOS’s clipboard programmatically
- Quick way to preview all pages in a PDF file
- Regarding AppleScript, is it possible to change “page layout” export option when exporting Numbers documents to PDF
- I cannot rearrange pages of a PDF in Preview
Best Answer
To my knowledge macOS does not have any one native command line Unix executable that can rotate all pages in a PDF (while keeping text based ones text based).
sip
can rotate a single page PDF however the resulting PDF is an encapsulated image, not text if it was text base to begin with. Also, not sure if there is a way with just plain AppleScript, other then via UI Scripting the default Preview application, without going to AppleScriptObjC (Cocoa-AppleScript) and or Python, etc.Using third-party command line utilities is probably the easiest, but you said it has to be done only using what's a default part of macOS. So, I'll offer an AppleScript solution that uses UI Scripting the default Preview application, that can be used in the event there isn't another way with AppleScriptObjC or without third-party utilities, etc.
This solution, as offered (and coded), assumes that Preview is the default application for PDF documents and uses it to rotate all the pages in the PDF document. It is also setup as an Automator workflow. (Although there are other ways to incorporate the AppleScript code shown below.)
First, in Finder, make a copy of the target PDF documents and work with those.
In Automator, create a new workflow document, adding the following actions:
AppleScript code:
Notes:
delay
commands may need to be changed for use on your system (and or additionaldelay
commands added as appropriate, although in the case additionaldelay
commands should not be needed). It should go without saying however test this on a set of a few document first to make sure the value set forthisLong
works on your system. On my system this worked as coded.If you need to rotate more then one time, add additional
theRotation as text,
to:Example: