I have tested the script in Automator as a Quick Action and it works as expected from Finder.
In Automator, you create a new Document, select Quick Action, and click Choose. Under the left-hand Libraries > Utilities library, drag and drop the Run AppleScript Action into the larger workflow window.
Set the header part of the Quick Action as:
And in the Run AppleScript, select everything in it and remove that content, as it will be replaced by the following content that you copy/paste into the Run AppleScript action:
use framework "Foundation"
use framework "PDFKit"
use AppleScript version "2.4"
use scripting additions
property WORKSHEET : "Worksheet"
property NSString : a reference to current application's NSString
property NSURL : a reference to current application's NSURL
property PDFDocument : a reference to current application's PDFDocument
on run {input, parameters}
# set thePDF to POSIX path of (choose file of type {"com.adobe.pdf"} default location (path to desktop)) as text
set status to {}
set thePDF to POSIX path of (input as text)
if thePDF does not end with ".pdf" then return
set status to my split_PDF(thePDF) as list
if (item 1 of status) is true then
display dialog "PDF has been sucessfully split" & return & return & thePDF & return & (item 2 of status) with title "PDF Split Results"
else
display dialog "PDF Split has failed."
end if
return
end run
on split_PDF(apdf)
# write first page of input PDF to a separate PDF, remove that page from the original
# and update the original PDF.
# Case Report - Acme.pdf splits off first page as Worksheet - Acme.pdf
# eventually will be the Worksheet filename
set ws_file to (NSString's stringWithString:apdf)'s mutableCopy()
# original PDF object
set pdf_url to NSURL's fileURLWithPath:apdf
set pdf to PDFDocument's alloc()'s initWithURL:pdf_url
# character length of filename
set charcnt to ws_file's |length|()
# change output name and reference of Worksheet PDF
ws_file's replaceOccurrencesOfString:"Case Report" withString:WORKSHEET options:0 range:(current application's NSMakeRange(0, charcnt))
set ws_url to NSURL's fileURLWithPath:ws_file
# extract first page of the PDF
set page_1 to (pdf's pageAtIndex:0)'s dataRepresentation()
# if the Worksheet was written successfully, remove first page and update original PDF
if (page_1's writeToURL:ws_url atomically:true) then
# remove first page of original PDF and update it
pdf's removePageAtIndex:0
pdf's writeToURL:pdf_url
return {true, ws_file as text} as list
else
return false
end if
end split_PDF
Then you click the hammer (compile) button in the Run AppleScript action, and then save the Quick Action. I called mine Split PDF. It will automatically be enabled in System Preferences > Extensions > Finder, so all you have to do is right-click on a Case Report PDF and watch for its corresponding Worksheet PDF to appear in the same location as the original PDF.