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.

MacOS 12.3 (Monterey) Automator Combine PDF Pages breaks workflows

I have a few workflows that have stopped working because when the "Combine PDF Pages" action runs, it fails.


I get the error "The action “Combine PDF Pages” encountered an error: “The operation couldn’t be completed. Command line tool returned error 127.: 127”


If anyone has any ideas or how to fix this I would be very appreciative.


Thanks in advance.


Brandon

MacBook Pro 13″, macOS 12.3

Posted on Mar 22, 2022 7:09 AM

Reply
Question marked as Top-ranking reply

Posted on Mar 22, 2022 10:14 AM

As an Automator Quick Action (e.g. QA Merge PDF) where you select the PDF in the Finder in the order you want them merged, and then on the first selected PDF, you control-click (two-finger tap) to access Quick Actions > QA Merge PDF from the secondary menu. The merged PDF will be written to your Desktop as merged.pdf.



And then drag/drop the Utilities Library > Run AppleScript action below it. Select everything in that Run AppleScript window and delete it. Then copy/paste the following back into that Run AppleScript action window:


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

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}
	
	# tilde path to absolute path
	set merged_pdf to (NSString's stringWithString:"~/Desktop/merged.pdf")'s stringByStandardizingPath()
	
	set outurl to NSURL's fileURLWithPath:(POSIX path of (item 1 of input))
	set outpdf to PDFDocument's alloc()'s initWithURL:outurl
	set pdfList to rest of input
	set lastPage to outpdf's pageCount()
	
	repeat with pdfdoc in pdfList
		set thisURL to (NSURL's fileURLWithPath:((POSIX path of pdfdoc) as text))
		set thisPDF to (PDFDocument's alloc()'s initWithURL:thisURL)
		
		# PDF pages are zero-based
		repeat with n from 1 to thisPDF's pageCount()
			set this_page to (thisPDF's pageAtIndex:(n - 1))
			(outpdf's insertPage:this_page atIndex:lastPage)
			set lastPage to outpdf's pageCount()
		end repeat
	end repeat
	outpdf's writeToFile:merged_pdf
	return
end run


Click the hammer icon in that Run AppleScript Action to compile this code, and then Save the Quick Action. I have used an example name for it above.

Similar questions

39 replies

Apr 22, 2022 7:31 AM in response to Duciel

You can revise my script by removing these two lines:


set msg to "Select multiple PDF using the ⌘-key in the order of your merge preference."
set pdfList to (choose file with prompt msg of type {"com.adobe.pdf"} default location (path to desktop) with multiple selections allowed)


and replacing with:


set dropBox to ((path to home folder as text) & "Dropbox:") as alias
tell application "Finder"
	set pdfList to (every item of folder dropBox whose kind is "PDF document") as alias list
	sort pdfList by name
end tell


This will get a sorted list of the PDF documents in your Dropbox folder and the rest of the script remains as it is to combine those PDFs into the Desktop merged.pdf document.

Apr 23, 2022 12:15 AM in response to VikingOSX

It now works exactly the way I wanted it to. Maybe even a little faster than the deprecated Automator function 'Combine PDF Pages'. To make the script work, I just had to replace [whose kind is "PDF document"] with [whose name extension is "pdf"].


You saved my life... :-) And you have made me want to go deeper in Applescript to perform other repetitive tasks.


Thank you.



Apr 25, 2022 6:50 AM in response to VikingOSX

Hi VikingOSX. (Are you Norwegian? If so, I am too...)


I have the same problem with Combine PDF dying because of Python going away, and I really think your script can be my solution too, but I need set the list of known pdf filenames (paths) in my order. (The PDF-files will be in a folder with all kinds of different files...)


I'm not any good in AppleScript, and this is the best I have soo far;


# tilde path to absolute path
set merged_pdf to (NSString's stringWithString:"~/Desktop/merged.pdf")'s stringByStandardizingPath()

# me trying to hardcode the list the 3 PDFs I want to merge. Where the _p1.pdf is page 1
set pdfList to {"~/Desktop/PreAdvise_200436_p1.pdf", "~/Desktop/PreAdvise_200436_a1.pdf", "~/Desktop/PreAdvise_200436_a2.pdf"}


set outurl to NSURL's fileURLWithPath:(POSIX path of (item 1 of pdfList))
set outpdf to PDFDocument's alloc()'s initWithURL:outurl
set pdfList to rest of pdfList
set lastPage to outpdf's pageCount()


but this gives me this error at pageCount:


error "missing value doesn’t understand the “pageCount” message." number -1708 from missing value


So I guess I'm way off in manually setting "pdfList" with my list of paths to get something AppleScripts understands.


If you know how I should format the "set pdfList to"-line to x number of paths to known PDFs - so the rest of your script understand - I would appreciate that very much.

Apr 26, 2022 3:20 PM in response to 3point3

I was able to make Viking OSX's AppleScript run successfully in an Automator Application with Monterey 12.3.1, but then I noticed what I think is a built-in macOS "Quick Action" that does the same thing. If I select multiple PDF files on my desktop, right click, highlight menu item "Quick Actions" and select "Create PDF", a merged PDF file shows up on my desktop with a name that starts with the first filename I selected plus some additional text. So I'm thinking that there is no need for the Automator "Combine PDF Pages" workflow item to be fixed, or to build a replacement using Automator + AppleScript!

Apr 27, 2022 4:14 AM in response to Cardinal1974

Hello,

You are right: the Quick action can be used in most cases.

However, as far as I am concerned, I use Viking OSX's AppleScript almost daily in an Automator workflow. 'Combine PDFs' comes after other automated processes and is launched in turn in different folders.

So it is very convenient for me to be able to launch all these processes in just one click.

Best Regards.

MacOS 12.3 (Monterey) Automator Combine PDF Pages breaks workflows

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