Split PDFs - Custom Break

This script split PDFs at a delimiter (see property theDelimiter).

Resulting PDFs are created in the original record’s location.

-- Split PDF at delimiter

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

property theDelimiter : "https://procesojudicial.ramajudicial.gov.co/FirmaElectronica"

tell application id "DNtp"
	try
		set theRecords to selected records
		if theRecords = {} then error "Please select some PDF records."
		show progress indicator "Split PDF at delimiter... " steps (count theRecords) as string with cancel button
		
		repeat with thisRecord in theRecords
			set thisRecord_Type to (type of thisRecord) as string
			if thisRecord_Type is in {"PDF document", "«constant ****pdf »"} then
				set thisRecord_NameWithoutExtension to name without extension of thisRecord
				step progress indicator "... " & thisRecord_NameWithoutExtension
				set thisRecord_Path to path of thisRecord
				set thisRecord_LocationGroup to location group of thisRecord
				set thisRecord_URL to URL of thisRecord
				set theTempDirectoryURL to my splitPDFatDelimiter(thisRecord_Path, thisRecord_NameWithoutExtension, thisRecord_LocationGroup, thisRecord_URL)
			end if
		end repeat
		
		my deleteTempDirectory(theTempDirectoryURL)
		hide progress indicator
		
	on error error_message number error_number
		hide progress indicator
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
		return
	end try
end tell

on splitPDFatDelimiter(theRecord_Path, theRecord_NameWithoutExtension, theRecord_LocationGroup, theRecord_URL)
	try
		set thePDF to current application's PDFDocument's alloc()'s initWithURL:(current application's |NSURL|'s fileURLWithPath:theRecord_Path)
		
		set theResultSelections to (thePDF's findString:theDelimiter withOptions:0)
		set theResultSelections_Count to theResultSelections's |count|()
		if theResultSelections_Count = 0 then error "This PDF doesn't contain delimiter \"" & theDelimiter & "\""
		
		set theNextFirstPage to missing value
		set theTempDirectoryURL to my createTempDirectory()
		set thisPrefix to 0
		
		repeat with i from 0 to (theResultSelections_Count - 1)
			set thisResultSelection to (theResultSelections's objectAtIndex:i)
			set thisResultSelection_Page_Index to (((thisResultSelection's pages()'s firstObject())'s label()) as integer) - 1 -- "pageAtIndex" is zero based, "label" is not
			
			if i = 0 then
				set thisPDF_FirstPage_Index to 0
			else
				set thisPDF_FirstPage_Index to theNextFirstPage_Index
			end if
			set thisPDF_LastPage_Index to thisResultSelection_Page_Index
			
			set thisPDF_FirstPage to (thePDF's pageAtIndex:thisPDF_FirstPage_Index)
			set thisPDF_FirstPage_Data to thisPDF_FirstPage's dataRepresentation()
			set thisPDF to (current application's PDFDocument's alloc()'s initWithData:thisPDF_FirstPage_Data)
			
			set thisPDF_CurrentLastPage_Index to thisPDF_FirstPage_Index as integer
			
			repeat with i from 1 to ((thisPDF_LastPage_Index as integer) - (thisPDF_FirstPage_Index as integer))
				set thisPDF_CurrentLastPage_Index to thisPDF_CurrentLastPage_Index + 1
				(thisPDF's insertPage:(thePDF's pageAtIndex:thisPDF_CurrentLastPage_Index) atIndex:(thisPDF's |pageCount|()))
			end repeat
			
			set thisTempURL to ((theTempDirectoryURL's URLByAppendingPathComponent:(current application's NSProcessInfo's processInfo()'s globallyUniqueString()))'s URLByAppendingPathExtension:"pdf")
			(thisPDF's writeToURL:thisTempURL)
			set thisTempPath to (thisTempURL's |path|()) as string
			
			tell application id "DNtp"
				set thisPrefix to thisPrefix + 1
				set thisImportedRecord_Name to theRecord_NameWithoutExtension & space & "-" & space & (thisPrefix as string)
				set thisImportedRecord to import thisTempPath name thisImportedRecord_Name to theRecord_LocationGroup
				set URL of thisImportedRecord to theRecord_URL
			end tell
			
			set theNextFirstPage_Index to thisResultSelection_Page_Index + 1
		end repeat
		
		return theTempDirectoryURL
		
	on error error_message number error_number
		activate
		if the error_number is not -128 then display alert "Error: Handler \"splitPDFatDelimiter\"" message error_message as warning
		try
			my deleteTempDirectory(theTempDirectoryURL)
		end try
		error number -128
	end try
end splitPDFatDelimiter

on createTempDirectory()
	try
		set theTempDirectoryURL to current application's |NSURL|'s fileURLWithPath:((current application's NSTemporaryDirectory())'s stringByAppendingPathComponent:("_Script - Split PDF at delimiter" & space & (current application's NSProcessInfo's processInfo()'s globallyUniqueString())))
		set {successCreateDir, theError} to current application's NSFileManager's defaultManager's createDirectoryAtURL:theTempDirectoryURL withIntermediateDirectories:false attributes:(missing value) |error|:(reference)
		if theError ≠ missing value then error (theError's localizedDescription() as string)
		return theTempDirectoryURL
	on error error_message number error_number
		activate
		if the error_number is not -128 then display alert "Error: Handler \"createTempDirectory\"" message error_message as warning
		error number -128
	end try
end createTempDirectory

on deleteTempDirectory(theTempDirectoryURL)
	try
		set {successDeleteDir, theError} to (current application's NSFileManager's defaultManager()'s removeItemAtURL:(theTempDirectoryURL) |error|:(reference))
		if theError ≠ missing value then error (theError's localizedDescription() as string)
	on error error_message number error_number
		activate
		if the error_number is not -128 then display alert "Error: Handler \"deleteTempDirectory\"" message error_message as warning
		error number -128
	end try
end deleteTempDirectory

4 Likes