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 9:04 AM

The following AppleScript will allow you to select n-tuple PDFs from a file chooser, and then merge them in selection order to a user-designated output pdf (in this case ~/Desktop/merged.pdf). With little effort, this AppleScript can be adopted for use in a Run AppleScript action in an Automator Application, Quick Action, or Shortcut.


Copy and paste into Apple's Script Editor, compile, and run to test. Does not change any existing PDF.


(*
  merge_pdf.applescript
  
  Prompt user for PDF filenames to merge using the ⌘-key for multiple selection.
  PDF will be merged in the order of their selection and written to an arbitrary
  PDF filename on the Desktop (in this example, merged.pdf)
  
  Tested: macOS 11.4, 11.6.5, 12.3
  VikingOSX, 2021-06-24, Apple Support Communities, No warranty/support implied.
*)

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

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

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)

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()

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


I also have this written in Swift for those that want a compiled tool.

Similar questions

39 replies

Apr 19, 2022 9:28 AM in response to VikingOSX

Ok. I sat down with Apple's join.py and looked at the code involved with the shuffle feature. Neither AppleScript/Objective-C nor JavaScript for Automation are being cooperative in the shuffle functionality migration and I have given up as it is consuming too much time. It might be possible in Objective-C or Swift, but that limits the user adaptation here.

Apr 22, 2022 4:30 AM in response to VikingOSX

Hello,


I used VikingOSX’s script to automate the combining of all pdf's in a folder into a single file. It works perfectly.


How would you replace the "choose file" in the line below with the hardcoded path? Folder is "Macintosh HD/myusername/Dropbox/…".


set pdfList to (choose file with prompt msg of type {"com.adobe.pdf"} default location (path to desktop) with multiple selections allowed)


Please note that the list must remain sorted.


Thank you.

Apr 22, 2022 6:16 AM in response to VikingOSX

Thank you very much for your quick response.


What I would like is indeed to omit the choose file clause. (Since the path is known in my case, it is not necessary for me to select it every time I run the script).


But, as I have never written Applescript before, I don't know how to assign pdfList its hardcoded value. Besides, the list must be sorted alphabetically.


Please excuse my English which is not my native language.


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

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

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.