Wednesday, August 10, 2022

#1 2021-03-25 02:32:54 pm

Registered: 2019-10-23
Posts: 961

Swap a specific page in same PDF file

If we like to swap page from the same PDF file we could do something like this.
If you run the script twice it will return to original order.

pageAtIndex{1,2} first page in PDF is 1


use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "Quartz"
use scripting additions

set thePath to POSIX path of (choose file)
set theURL to current application's |NSURL|'s fileURLWithPath:thePath

set savePath to POSIX path of (path to desktop) & "newSavedFile.pdf"
set saveFile to current application's |NSURL|'s fileURLWithPath:savePath

my swapPageWith:theURL pageAtIndex:{1, 2} savePath:saveFile

on swapPageWith:theURL pageAtIndex:{a, b} savePath:saveFile
   set pdfDoc to current application's PDFDocument's alloc()'s initWithURL:theURL
   set pageCount to pdfDoc's pageCount()
   if (a is greater than pageCount) or (b is greater than pageCount) then return
       if (pdfDoc is not missing value) then
           (pdfDoc's exchangePageAtIndex:(a - 1) withPageAtIndex:(b - 1))
           (pdfDoc's writeToURL:saveFile)
       end if
   end try
end swapPageWith:pageAtIndex:savePath:

if you are the expert, who will you call if its not your imagination.



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)