Thursday, October 18, 2018

#1 2017-12-30 03:35:55 pm

Registered: 2010-04-04
Posts: 80

Splitting multipage PDF document

I have failed at all attempts to  split a multipage PDF document into individual PDF documents.

My goal is to name each split PDF by the first three words in the  newly created PDF document, without relying on GUI.

I appreciate any insight on this project.



#2 2017-12-30 08:36:18 pm

Shane Stanley
From:: Australia
Registered: 2002-12-07
Posts: 5453

Re: Splitting multipage PDF document

This should get you started:


use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "Quartz" -- required for PDF stuff

set inPath to POSIX path of (choose file with prompt "Choose a PDF file:")
set folderPath to POSIX path of (choose folder with prompt "Choose a folder to save the separated pages to.")
its splitPagesInPath:inPath savingInFolder:folderPath

on splitPagesInPath:inPath savingInFolder:folderPath
   -- make URL of the PDF
   set inNSURL to current application's |NSURL|'s fileURLWithPath:inPath
   -- get name of PDF
   set docName to inNSURL's lastPathComponent()
   -- make URL of output folder
   set outFolderNSURL to current application's |NSURL|'s fileURLWithPath:folderPath
   -- make PDF document from the file
   set theDoc to current application's PDFDocument's alloc()'s initWithURL:inNSURL
   -- store media bounds of page 1; unnecessary in most cases
   set theBounds to (theDoc's pageAtIndex:0)'s boundsForBox:(current application's kPDFDisplayBoxMediaBox)
   -- count the pages
   set theCount to theDoc's pageCount() as integer
   repeat with i from 1 to theCount
       -- build new document's name
       set newDocName to (its addString:("-" & i) beforeExtensionIn:docName)
       -- make URL for new PDF
       set outNSURL to (outFolderNSURL's URLByAppendingPathComponent:newDocName)
       -- get page of old PDF
       set thePDFPage to (theDoc's pageAtIndex:(i - 1)) -- zero-based indexes
       -- set media bounds if you stored it above
       (thePDFPage's setBounds:theBounds forBox:(current application's kPDFDisplayBoxMediaBox))
       -- make new PDF document
       set theNewPDFDocument to current application's PDFDocument's new()
       -- insert the page
       (theNewPDFDocument's insertPage:thePDFPage atIndex:0)
       -- save the new PDF
       (theNewPDFDocument's writeToURL:outNSURL)
   end repeat
end splitPagesInPath:savingInFolder:

on addString:extraString beforeExtensionIn:aPath
   set aString to current application's NSString's stringWithString:aPath
   set newString to current application's NSString's stringWithFormat_("%@%@.%@", aString's stringByDeletingPathExtension(), extraString, aString's pathExtension())
   return newString as text
end addString:beforeExtensionIn:

Shane Stanley <>



#3 2017-12-30 09:15:55 pm

Registered: 2017-12-30
Posts: 7

Re: Splitting multipage PDF document

Wow, Stan's solution is so much more elegant...

Anyway, I had this problem for ages, ppl asking me to crack their documents. I was coding this below. Relies on Ghostscript, that can be installed from brew.

I guess you can mix mine with his, and get the three name nomenclature working. btw I am sorry about the "pasta mix coding", I am new to this.


set pdfFile to choose file with prompt "Select pdf:"
set filePath to POSIX path of pdfFile
set nPages to do shell script "/usr/local/bin/gs -q -dNODISPLAY -c \"(" & filePath & ") (r) file runpdfbegin pdfpagecount = quit\";"
tell application "Finder"
   set currentPath to container of (path to me) as alias
   set destFolder to the POSIX path of currentPath
end tell

tell application "System Events" to tell disk item (pdfFile as text) to set {theName, theExtension} to {name, name extension}
if theExtension is not "" then set theName to text 1 thru -((count theExtension) + 2) of theName

if theExtension is not "pdf" then
   display dialog "© ¤ ¤ ¤ ¤ ¤ Feed me PDFs..." buttons {"Cancel"}

   set nameCount to count words of theName
   if nameCount = 1 then
       set outName to first word of theName
   else if nameCount = 2 then
       set out1Name to first word of theName
       set out2Name to second word of theName
       set outName to [out1Name & " " & out2Name]
   else if nameCount > 2 then
       set out1Name to first word of theName
       set out2Name to second word of theName
       set out3Name to third word of theName
       set outName to [out1Name & " " & out2Name & " " & out3Name]
   end if
   if nPages is equal to "1" then
       display dialog "© ¤ ¤ ¤ ¤ ¤ Feed me lots of pages..." buttons {"Cancel"}
   end if
   set mainDialog to display dialog "Split (" & outName & ") into " & nPages & " pdf files ?" buttons {"Split", "Cancel"} default button "Split"
   if (button returned of mainDialog = "Split") then
       do shell script "/usr/local/bin/gs -sDEVICE=pdfwrite -dNOPAUSE -dQUIET -dBATCH -sOutputFile='" & destFolder & outName & "-%d.pdf' " & "\"" & filePath & "\";"
   end if
end if

Last edited by mauricioa (2017-12-30 09:48:23 pm)



#4 2017-12-31 01:48:45 am

Registered: 2010-04-04
Posts: 80

Re: Splitting multipage PDF document

Shane, I agree with Mauricioa. Your solution is amazing! Thank you.



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)