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 pdf document.


Document are invoice, and we need to keep only the first page to sent to other people.


I found almost the solution here.

Automator help - quick action to split th… - Apple Community

As I'm not using same file name, that script doesn't create the file with the extraction. :-(


The original document would be "original name" with add "split "

and the single page document after splitting to be called “original name" with add "1" .pdf”


it will save me a lot to time.

Thanks in advance


MacBook Pro 13″, macOS 11.7

Posted on Feb 18, 2023 9:34 AM

Reply
Question marked as Top-ranking reply

Posted on Feb 18, 2023 2:57 PM

Ok. Here is the Quick Action.


Launch Automator and Choose new document > Quick Action > Choose.


  1. Workflow receives current PDF Files in Finder
  2. Utility LIbrary > Run AppleScript action. Drag this into the larger right-hand workflow window.
    1. Remove all content from the Run AppleScript action as it will be entirely replaced by the following code
    2. When you have copy/pasted the following code into the Run AppleScript action, click the hammer icon in the action to compile the AppleScript.
    3. Save the Quick Action with a brief but unmistable name
    4. In System Preferences > Extensions > Finder, be certain that your named Quick Action is enabled.
    5. Select one or more PDFs in the Finder and from a right-click Finder secondary menu, choose Quick Actions > Your Quick Action name


The original PDFs will be untouched, and you will have the first page and remainder (_split.pdf) filenames for each originally selected PDF. This was tested on Ventura 13.2, instead of Big Sur.


Code:


use framework "Foundation"
use framework "PDFKit"
use AppleScript version "2.4" -- on macOS 10.10 (Yosemite or later)
use scripting additions

property ca : current application
property SUFFIX_1 : "_1"
property SUFFIX_SPLIT : "_split"

on run {input, parameters}
	
	if input = {} then return
	
	repeat with apdf in input
		
		if (apdf as text) ends with ".pdf" then
			
			set thePDF to (POSIX path of apdf) as text
			set pdfPageOne to (ca's NSURL's fileURLWithPath:(my add_PDF_suffix(thePDF, SUFFIX_1)))
			
			set pdf to (ca's PDFDocument's alloc()'s initWithURL:(ca's NSURL's fileURLWithPath:thePDF))
			set page_one to (pdf's pageAtIndex:0)'s dataRepresentation()
			set file_exists to false
			try
				tell application "Finder" to set file_exists to (exists POSIX file pdfPageOne as alias) as boolean
			end try
			if not file_exists then
				set status to (page_one's writeToURL:pdfPageOne atomically:true) as boolean
			end if
			
			set pdfSplitFile to my add_PDF_suffix(thePDF, SUFFIX_SPLIT)
			set pdfSplitURL to (ca's NSURL's fileURLWithPath:pdfSplitFile)
			set file_exists to false
			try
				tell application "Finder" to set file_exists to (exists POSIX file pdfSplitFile as alias) as boolean
			end try
			
			if status and not file_exists then
				(pdf's removePageAtIndex:0)
				(pdf's writeToURL:pdfSplitURL)
			end if
		end if
	end repeat
	return input
end run

on add_PDF_suffix(apdf, suffix)
	-- change filename.pdf to filename_1.pdf
	set ext to (ca's NSString's stringWithString:apdf)'s pathExtension()
	set basename to (ca's NSString's stringWithString:apdf)'s stringByDeletingPathExtension()
	set base_with_suffix to (basename's stringByAppendingString:suffix)'s stringByAppendingPathExtension:ext
	return base_with_suffix as text
end add_PDF_suffix


Similar questions

12 replies
Question marked as Top-ranking reply

Feb 18, 2023 2:57 PM in response to xtwxtw

Ok. Here is the Quick Action.


Launch Automator and Choose new document > Quick Action > Choose.


  1. Workflow receives current PDF Files in Finder
  2. Utility LIbrary > Run AppleScript action. Drag this into the larger right-hand workflow window.
    1. Remove all content from the Run AppleScript action as it will be entirely replaced by the following code
    2. When you have copy/pasted the following code into the Run AppleScript action, click the hammer icon in the action to compile the AppleScript.
    3. Save the Quick Action with a brief but unmistable name
    4. In System Preferences > Extensions > Finder, be certain that your named Quick Action is enabled.
    5. Select one or more PDFs in the Finder and from a right-click Finder secondary menu, choose Quick Actions > Your Quick Action name


The original PDFs will be untouched, and you will have the first page and remainder (_split.pdf) filenames for each originally selected PDF. This was tested on Ventura 13.2, instead of Big Sur.


Code:


use framework "Foundation"
use framework "PDFKit"
use AppleScript version "2.4" -- on macOS 10.10 (Yosemite or later)
use scripting additions

property ca : current application
property SUFFIX_1 : "_1"
property SUFFIX_SPLIT : "_split"

on run {input, parameters}
	
	if input = {} then return
	
	repeat with apdf in input
		
		if (apdf as text) ends with ".pdf" then
			
			set thePDF to (POSIX path of apdf) as text
			set pdfPageOne to (ca's NSURL's fileURLWithPath:(my add_PDF_suffix(thePDF, SUFFIX_1)))
			
			set pdf to (ca's PDFDocument's alloc()'s initWithURL:(ca's NSURL's fileURLWithPath:thePDF))
			set page_one to (pdf's pageAtIndex:0)'s dataRepresentation()
			set file_exists to false
			try
				tell application "Finder" to set file_exists to (exists POSIX file pdfPageOne as alias) as boolean
			end try
			if not file_exists then
				set status to (page_one's writeToURL:pdfPageOne atomically:true) as boolean
			end if
			
			set pdfSplitFile to my add_PDF_suffix(thePDF, SUFFIX_SPLIT)
			set pdfSplitURL to (ca's NSURL's fileURLWithPath:pdfSplitFile)
			set file_exists to false
			try
				tell application "Finder" to set file_exists to (exists POSIX file pdfSplitFile as alias) as boolean
			end try
			
			if status and not file_exists then
				(pdf's removePageAtIndex:0)
				(pdf's writeToURL:pdfSplitURL)
			end if
		end if
	end repeat
	return input
end run

on add_PDF_suffix(apdf, suffix)
	-- change filename.pdf to filename_1.pdf
	set ext to (ca's NSString's stringWithString:apdf)'s pathExtension()
	set basename to (ca's NSString's stringWithString:apdf)'s stringByDeletingPathExtension()
	set base_with_suffix to (basename's stringByAppendingString:suffix)'s stringByAppendingPathExtension:ext
	return base_with_suffix as text
end add_PDF_suffix


Feb 18, 2023 10:21 AM in response to xtwxtw

That original linked Automator solution was unique to that request, though it can be adapted to your need without altering the original PDF content.


Are you starting with an original PDF that you have already renamed from filename.pdf to filename_split.pdf? If that is the case, do you want the first page that is split from the PDF to be named original filename with the numeric 1 as a suffix — as filename_1.pdf?

Feb 18, 2023 12:20 PM in response to VikingOSX

Thank you for your quick answer. :-)


No I didn't already renamed the file from filename.pdf to filename_split.pdf.

Ideally, the script will rename the file from filename.pdf to filename_split.pdf (the file without the first page)


Do you want the first page that is split from the PDF to be named original filename with the numeric 1 as a suffix — as filename_1.pdf?

YES exactly :-)


At the end I like to have :

filename.pdf (original file with all page)

filename_1.pdf (first page extract from the file)

filename_split.pdf (pages without the first page)


Thank you.


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.