You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Automator help - quick action to split the first page of a PDF

I’m wondering if any of you helpful people can write some applescript to help me “split” (not remove) the first page of an approx 11 page pdf.


e.g. The first page is a worksheet and the other pages are the report i create. I need to split so that one can go to my boss and the rest to the customer.


The original document would be called “Case Report - Client Name.pdf” and I would like the single worksheet after splitting to be called “Worksheet - Client Name.pdf”


This would help me greatly every single day and save me untold time over the course of a year. Appreciative of any help.

Posted on Jan 23, 2021 6:10 AM

Reply
Question marked as Top-ranking reply

Posted on Jan 23, 2021 12:34 PM

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.

7 replies
Question marked as Top-ranking reply

Jan 23, 2021 12:34 PM in response to smh9321

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.

Jan 23, 2021 9:25 AM in response to smh9321

I just coded up an Automator solution that will write the first page of the input PDF as your designated PDF worksheet file, and then remove that page from the original PDF, before updating it. The Automator application can be your choice of just drag and drop of the original PDF onto the application, or an interactive application that allows you to choose the Case Report PDF location.


Tested on macOS 11.1.


What is your preference?

Automator help - quick action to split the first page of a PDF

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.