Tuesday, May 11, 2021

#1 2021-03-25 12:47:37 pm

Registered: 2019-10-23
Posts: 707

Swap a specific page in PDF with a single page PDF

Here is simple ASObjC Script that do follow:

Choose a PDF file of multiply pages, swap page with pageAtIndex:indexNumber
with a single page PDF. In other words page 5 of choose file (PDF) will swap with singlePageDoc.pdf

It works, but I wonder could this be done in more simple way ??


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

set insertPath to POSIX path of (path to desktop) & "singlePageDoc.pdf"
set insertURL to current application's |NSURL|'s fileURLWithPath:insertPath

my swapPageWith:theURL insertPage:insertURL pageAtIndex:5 savePath:saveFile

on swapPageWith:theURL insertPage:insertURL pageAtIndex:indexNumber savePath:saveFile
   set pdfDoc to current application's PDFDocument's alloc()'s initWithURL:theURL
   set insertPage to current application's PDFDocument's alloc()'s initWithURL:insertURL
   set pageCount to pdfDoc's pageCount()
   if indexNumber is greater than pageCount then return
       if (pdfDoc is not missing value) then
           (pdfDoc's insertPage:(insertPage's pageAtIndex:0) atIndex:(indexNumber - 1))
           (pdfDoc's removePageAtIndex:indexNumber)
           (pdfDoc's writeToURL:saveFile)
       end if
   end try
end swapPageWith:insertPage:pageAtIndex:savePath:

Last edited by Fredrik71 (2021-03-25 01:33:25 pm)

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)