Help with splitting a PDF using PDFPen please?

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Help with splitting a PDF using PDFPen please? Wed Mar 10, 2021 8:02 am • by MacOCD
I need to split a potentially 100 page pdf using Hazel. PDFPen seems like it may be the tool for the job. I don't really know what I'm doing, but here is the script I currently us to OCR a PDF using PDFPen
Code: Select all
tell application "PDFpenPro"
   open theFile as alias
   tell document 1
      ocr
      repeat while performing ocr
         delay 1
      end repeat
      delay 1
      close with saving
   end tell
end tell


The sample code from the makers of PDFPen for splitting PDFs is more complex, it allows you to choose an output location, which I don't need. I simply wish to split a PDF, outputted to a location coded into the script, when the source PDF matches my conditions. .
Code: Select all
-- Split PDF script / droplet
--
-- Split the current document in PDFpen into one PDF file per page

on split(aDocument)
   tell application "PDFpenPro"
      set savePath to choose folder with prompt "Store new PDF files here:"
      set docName to name of aDocument
      if docName ends with ".pdf" then
         set docNameLength to (length of docName)
         set docName to (characters 1 thru (docNameLength - 4) of docName as string)
      end if
      
      set pageCount to 1
      set originalDoc to aDocument
      repeat with aPage in pages of originalDoc
         set newDoc to make new document
         copy page pageCount of originalDoc to end of pages of newDoc
         set fullSavePath to (savePath as rich text) & docName & "_"
         set zeroCount to 3
         if (pageCount div 10) > 0 then
            set zeroCount to 2
         end if
         if (pageCount div 100) > 0 then
            set zeroCount to 1
         end if
         if (pageCount div 1000) > 0 then
            set zeroCount to 0
         end if
         repeat zeroCount times
            set fullSavePath to fullSavePath & "0"
         end repeat
         set fullSavePath to fullSavePath & pageCount & ".pdf"
         -- The try block is necessary to avoid a sandbox issue
         try
            close document 1 saving in fullSavePath
         end try
         set pageCount to pageCount + 1
      end repeat
      return pageCount - 1
   end tell
end split

on run
   tell application "PDFpenPro"
      if (count documents) > 0 then
         return split(document 1) of me
      else
         return
      end if
   end tell
end run

on open fileList
   repeat with aFile in fileList
      set totalPages to 0
      tell application "PDFpenPro"
         open (aFile as alias)
         set aDocument to document 1
         set totalPages to totalPages + (split(aDocument) of me)
         close aDocument
      end tell
   end repeat
end open


I've been trying for a week or so, but I just dont understand what I'm doing here. Could someone help please?

Many thanks,
Mark.

PS. Alternative solutions also welcome if PDFPen isn't the best way to do this :)
MacOCD
 
Posts: 44
Joined: Fri Sep 26, 2014 11:02 am

There are several issues with that script. The main one, with regards to Hazel, is that that script has handlers in it. If you want to use it as an embedded script, you can't have those handlers. If you want it as an external script, then you need to use a special handler (the manual has details on that). There are other issues with input arguments and such you would have to address as well.

As an alternative, it might be worth looking into Automator. I do see there is a "Split PDF" action. Not sure if it does what you want but it might be worth looking into.
Mr_Noodle
Site Admin
 
Posts: 11247
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

I'll look into Automator for this task. Thank you.
MacOCD
 
Posts: 44
Joined: Fri Sep 26, 2014 11:02 am


Return to Support

cron