Is there an applescript way to just grab a range of pages from a PDF and save it out to a new PDF from Acrobat Mac? Not much there in the scripting dictionary for this. Right now I am having to start with an existing blank.pdf file, delete pages before and after my selected range in the master pdf file and save to the blank pdf file. Would like do this in a less complicated way.
hi inyo55
is it odd pages only, or even pages only, or pages from 1 to 50 etc etc, what sort of page range are we looking at?
Hello
I don’t know if the result will match the wanted level of quality but it costs nothing to try this code built upon a Shane Stanley’s one.
use AppleScript version "2.3.1"
use scripting additions
use framework "Foundation"
use framework "Quartz" -- required for PDF stuff
#===== Handlers
-- Remove from a PDF file the pages whose index is in theList, saving as a new version
on removePagesInPDF:thePath outputTo:newPath pagesList:theList dropThem:flag
set inNSURL to current application's |NSURL|'s fileURLWithPath:thePath
set outNSURL to current application's |NSURL|'s fileURLWithPath:newPath
set thePDFDocument to current application's PDFDocument's alloc()'s initWithURL:inNSURL
# CAUTION. theList contain indexes of pages numbered starting from 1, but ASObjC number them starting from 0
set theCount to thePDFDocument's pageCount() as integer
# Remove pages starting from the PDF's end
if flag is true then
# drop the pages listed
repeat with i from theCount to 1 by -1
if i is in theList then (thePDFDocument's removePageAtIndex:(i - 1))
end repeat
else
# keep the pages listed
repeat with i from theCount to 1 by -1
if i is not in theList then (thePDFDocument's removePageAtIndex:(i - 1))
end repeat
end if
thePDFDocument's writeToURL:outNSURL
end removePagesInPDF:outputTo:pagesList:dropThem:
-- inserts a string in a path before the extension
on addString:extraString beforeExtensionIn:aPath
set pathNSString to current application's NSString's stringWithString:aPath
set newNSString to current application's NSString's stringWithFormat_("%@%@.%@", pathNSString's stringByDeletingPathExtension(), extraString, pathNSString's pathExtension())
return newNSString as text
end addString:beforeExtensionIn:
#===== Caller
set thePath to POSIX path of (choose file with prompt "Choose a PDF." of type {"PDF"})
# If the number of pages to keep is small use this code
set newPath to its addString:"-stripped" beforeExtensionIn:thePath
its removePagesInPDF:thePath outputTo:newPath pagesList:{5, 10, 15} dropThem:false
# If the count of pages to drop is small, use this code
set newPath to its addString:"-strippedAlt" beforeExtensionIn:thePath
its removePagesInPDF:thePath outputTo:newPath pagesList:{5, 10, 15} dropThem:true
Yvan KOENIG running Yosemite 10.10.4 in French (VALLAURIS, France) mardi 8 septembre 2015 22:29:19