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.

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:

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"}

else
	
	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


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