Thanks @pete31! I am getting single page PDFs, which contain theDelimiter. The pages between delimiters are being discarded.
I spent a couple of hours trying to understand the logic behind your genius script. I think the difficulty might be in defining thisPDF_LastPage_Index. In the previous script defining the last page was “straightforward”, as it was the page that contained the theDelimiter; so was definining the next starting page: the one right after the previous theDelimiter.
Here, the first page should be the one containing the first instance of theDelimiter, and the last page the one before the next instance of theDelimiter. With my (very) limited understanding of loops, seems tough to get. Nice try though! Thanks!
-- Split PDF at delimiter (using the next page after the delimiter as first page)
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
property theDelimiter : "Proceso 31-2891"
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 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 thisResultSelection_Page_Index
end if
if (i < theResultSelections_Count - 1) then
set theNextResultSelection to (theResultSelections's objectAtIndex:(i + 1))
set theNextPDF_FirstPage_Index to (((theNextResultSelection's pages()'s firstObject())'s label()) as integer) - 1 -- "pageAtIndex" is zero based, "label" is not
set thisPDF_LastPage_Index to theNextPDF_FirstPage_Index - 1
else
set thisPDF_LastPage_Index to ((thePDF's |pageCount|()) - 1)
end if
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