The following script will rotate all pages of PDFs supplied as filenames in its arguments.Have you tried to rotate a field in a PDF form after it was created in Acrobat? If so, you may have scratched your head a bit.īefore we get to the how, let’s first talk about the why: When you have two different documents, one having a page rotation of 0 degrees, and the second one with a page rotation of 90 degrees (or, two different pages in the same document with different page rotations), and you copy a form field from one page to a page with a different page rotation, the form field will be rotated, and you will have to rotate it back in order to get the correct alignment and orientation. The Shortcuts.app includes an option to use Swift in its "Run Shell Script" action. You can now use Swift as a scripting language. (thisPage's setRotation:((thisPage's |rotation|()) + r)) - add 90 to the current rotation, note: at 360, the value of the rotation will be set to 0, not to 360 Set thisPage to (thisDoc's pageAtIndex:i) Set thisDoc to current application's PDFDocument's alloc()'s initWithURL:(current application's NSURL's fileURLWithPath:thisPath) My rotatePages(thisPath, 90) - rotate right, use -90 to rotate left Keystroke thisKey using with prompt "Choose a PDF") - a POSIX path, not an alias or an HFSPath # occurs on the 'Thumbnails', not the body of the document.
# This is being done so the subsequent keystroke ⌘A 'Select All' # Process keystrokes based on which 'View' menu item is checked. If item i in theViewMenuCheckedList is not missing value then Set theViewMenuCheckedList to (value of attribute "AXMenuItemMarkChar" of menu items 1 thru 6 of menu 1 of menu bar item 5 of menu bar 1 of application process "Preview") # Ascertain which of the first six 'View' menu items is checked. Perform action "AXRaise" of window 1 of application process "Preview" - # Just to make sure 'window 1' is front-most. This is the only 'delay' not defined by the value of 'thisLong'. Tell application "Finder" to open file thisItem - # By default, in this use case, the PDF file will open in Preview.ĭelay 1 - # Adjust as necessary. Set theRotation to "r" - # Valid values are 'l' or 'r' for Rotate Left or Rotate Right. Set thisLong to 0.25 - # The value of 'thisLong' is decimal seconds delay between keystrokes, adjust as necessary.
HOW TO ROTATE PDF DOCUMENT USING KEYBOARD CODE
It is also setup as an Automator workflow. 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. 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. 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. 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. 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. 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).