(* splitInSeveralPDFs.scpt *)
# Most of this code was borrowed from Shane Stanley.
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "Quartz" -- required for PDF stuff
#=====#=====#=====#=====#=====#=====
on splitInSeveralPDFs:thePath nbFiles:cnt
set inNSURL to current application's |NSURL|'s fileURLWithPath:thePath
set theDoc to current application's PDFDocument's alloc()'s initWithURL:inNSURL
set theCount to theDoc's pageCount() as integer
set nbPages to theCount div cnt (*theMiddle = 0*)
set beg to 0
set thePaths to {}
repeat with knt from 1 to cnt
set newPath1 to (its addString:("-part" & knt) beforeExtensionIn:thePath)
set end of thePaths to newPath1 as text
set outNSURL1 to (current application's |NSURL|'s fileURLWithPath:newPath1)
set newPDFDoc1 to current application's PDFDocument's alloc()'s init()
if knt = cnt then
set lastPage to theCount - 1
else
set lastPage to beg + nbPages - 1
end if
set newPage to 0
repeat with i from beg to lastPage
set thePDFPage to (theDoc's pageAtIndex:i) -- zero-based indexes
(newPDFDoc1's insertPage:thePDFPage atIndex:newPage)
set newPage to newPage + 1
end repeat
(newPDFDoc1's writeToURL:outNSURL1)
set beg to beg + nbPages
end repeat
(*
# Efface le PDF initial
set theFileManager to current application's NSFileManager's |defaultManager|()
theFileManager's removeItemAtURL:inNSURL |error|:(missing value)
*)
return thePaths
end splitInSeveralPDFs:nbFiles:
-- inserts a string in a path before the extension
on addString:extraString beforeExtensionIn:aPath
set pathNSString to current application's NSString's stringWithString:aPath
set newNSString to current application's NSString's stringWithFormat_("%@%@.%@", pathNSString's stringByDeletingPathExtension(), extraString, pathNSString's pathExtension())
return newNSString as text
end addString:beforeExtensionIn:
#=====#=====#=====#=====#=====#=====
on returnSizeFor:POSIXPath # get the size
set aURL to current application's |NSURL|'s fileURLWithPath:POSIXPath -- make URL
set {theResult, theSize} to aURL's getResourceValue:(reference) forKey:(current application's NSURLFileSizeKey) |error|:(missing value)
if theSize = missing value then return {} -- because when there are none, it returns missing value
return theSize as list
end returnSizeFor:
#=====#=====#=====#=====#=====#=====
on countPages:POSIXPath
local inNSURL, theDoc
set inNSURL to current application's class "NSURL"'s fileURLWithPath:POSIXPath
-- make PDF document from the URL
set theDoc to current application's PDFDocument's alloc()'s initWithURL:inNSURL
return theDoc's pageCount()
end countPages:
#=====#=====#=====#=====#=====#=====
set originalPosixPath to POSIX path of (choose file of type {"pdf"})
set itsSize to item 1 of (my returnSizeFor:originalPosixPath)
set itsPagesCount to my countPages:originalPosixPath
set sizeAllowed to 2000000 # Adjust to fit your needs.
set cntFiles to round (itsSize / sizeAllowed) rounding up
set thePaths to my splitInSeveralPDFs:originalPosixPath nbFiles:cntFiles
--> List of posix paths of the separated files.