Monday, May 29, 2017

#1 2017-01-10 08:40:01 pm

Firefighting09
Member
Registered: 2017-01-10
Posts: 2

Help: Have 20 page pdf need script to extract first 5 pgs into new PDF

I use hazel as an automation tool. I have been having issues trying to take a 20 pg OCRed Pdf and extract the first 5 pgs and saving that as a new pdf. So, I only need the 5 pages each time I do the workflow. Any help is greatly appreciated. I have pdfpen if that helps too. big_smile


Filed under: PDF, Automator, Hazel, PDFpen

Offline

 

#2 2017-01-11 04:35:55 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3010

Re: Help: Have 20 page pdf need script to extract first 5 pgs into new PDF

Here is a Shane STANLEY's script.
I don't remember where I got it.

Applescript:

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 file." 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:{4} 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:{1, 2, 3, 4, 5} dropThem:false

As is the caller instruction drop every page after the 5th one.

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) mercredi 11 janvier 2017 11:34:42

Offline

 

#3 2017-01-13 09:34:12 am

Firefighting09
Member
Registered: 2017-01-10
Posts: 2

Re: Help: Have 20 page pdf need script to extract first 5 pgs into new PDF

Thank you for your post. I will give it a shot. Thanks!!

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.040 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)