PDF page extraction code not working under High Sierra /File corrupt

I have modified an Applescript code from Shane Stanley https://macscripter.net/viewtopic.php?pid=192080#p192080, to extract a single page from a multipage PDF and store it as PDF again. The original code splits a multipage PDF file into individual PDF files for all pages.

My modified script works from the command line (with osascript) on Catalina and High Sierra.

I then bundled it into a program which I have written in XOJO. No matter if I call the Applescript directly out of XOJO or via shell and osascript, it ONLY WORKS on Catalina. On High Sierra the new created PDF file is somehow corrupted and cannot be opened. The file size is a few kB less than as it should!

From my XOJO code I call the Applescript in this way:
osascript Scriptname.scpt FullPOSIXPathToPDFFile PageNumber FullPOSIXPathToOutputFolder

Can someone please have a look if the syntax is right?

I have left the original code as is and just commented out the few lines that are not needed in my mind.

Thanks!!!



-- Original source: Shane Stanley
-- https://macscripter.net/viewtopic.php?pid=192080#p192080

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

on run {aPDF, aPage, aFolder}
	
	set inPath to POSIX path of aPDF
	set folderPath to POSIX path of aFolder
	
	-- 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 selectedPage:aPage savingInFolder:folderPath
	
end run


on splitPagesInPath:inPath selectedPage:i 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:selectedPage: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: