Automator Combine PDF script still broken

I'm on macOS Monterey, version 12.5, and the Automator Combine PDF action and underlying python script are still broken ( MacOS 12.3 (Monterey) Automator Combine P… - Apple Community )


I'm using the underlying python script for some of my own shell script, so I've updated it for Python3. No guarantees that this works, but hopefully it's helpful for someone else.


#! /usr/bin/env python
#
# join
#   Joing pages from a a collection of PDF files into a single PDF file.
#
#   join [--output <file>] [--shuffle] [--verbose]"
#
#   Parameter:
#
#   --shuffle
#	Take a page from each PDF input file in turn before taking another from each file.
#	If this option is not specified then all of the pages from a PDF file are appended
#	to the output PDF file before the next input PDF file is processed.
#
#   --verbose
#   Write information about the doings of this tool to stderr.
#
import sys
import os
import getopt
import tempfile
import shutil
from CoreFoundation import *
from Quartz.CoreGraphics import *


verbose = False


def createPDFDocumentWithPath(path):
	global verbose
	if verbose:
		print("Creating PDF document from file %s" % (path))
	return CGPDFDocumentCreateWithURL(CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, path.encode("utf-8"), len(path), False))


def writePageFromDoc(writeContext, doc, pageNum):


	global verbose
	page = CGPDFDocumentGetPage(doc, pageNum)
	if page:
		mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox)
		if CGRectIsEmpty(mediaBox):
			mediaBox = None
			
		CGContextBeginPage(writeContext, mediaBox)
		CGContextDrawPDFPage(writeContext, page)
		CGContextEndPage(writeContext)
		if verbose:
			print("Copied page %d from %s" % (pageNum, doc))


def shufflePages(writeContext, docs, maxPages):
	
	for pageNum in range(1, maxPages + 1):
		for doc in docs:
			writePageFromDoc(writeContext, doc, pageNum)
				
def append(writeContext, docs, maxPages):


	for doc in docs:
		for pageNum in range(1, maxPages + 1) :
			writePageFromDoc(writeContext, doc, pageNum)


def main(argv):


	global verbose


	# The PDF context we will draw into to create a new PDF
	writeContext = None


	# If True then generate more verbose information
	source = None
	shuffle = False
	
	# Parse the command line options
	try:
		options, args = getopt.getopt(argv, "o:sv", ["output=", "shuffle", "verbose"])


	except getopt.GetoptError:
		usage()
		sys.exit(2)


	for option, arg in options:


		if option in ("-o", "--output") :
			if verbose:
				print("Setting %s as the destination." % (arg))
			writeContext = CGPDFContextCreateWithURL(CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, arg.encode("utf-8"), len(arg), False), None, None)


		elif option in ("-s", "--shuffle") :
			if verbose :
				print("Shuffle pages to the output file.")
			shuffle = True


		elif option in ("-v", "--verbose") :
			print("Verbose mode enabled.")
			verbose = True


		else :
			print("Unknown argument: %s" % (option))
	
	if writeContext:
		# create PDFDocuments for all of the files.
		docs = list(map(createPDFDocumentWithPath, args))
		
		# find the maximum number of pages.
		maxPages = 0
		for doc in docs:
			if CGPDFDocumentGetNumberOfPages(doc) > maxPages:
				maxPages = CGPDFDocumentGetNumberOfPages(doc)
	
		if shuffle:
			shufflePages(writeContext, docs, maxPages)
		else:
			append(writeContext, docs, maxPages)
		
		CGPDFContextClose(writeContext)
		del writeContext
		#CGContextRelease(writeContext)
    
def usage():
	print("Usage: join [--output <file>] [--shuffle] [--verbose]")


if __name__ == "__main__":
	main(sys.argv[1:])


It should be possible to replace "/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py" with this script, and have the Automator action work correctly, but I haven't tried it.

MacBook Pro 16″, macOS 12.5

Posted on Jan 9, 2023 8:56 AM

Reply
Question marked as Top-ranking reply

Posted on Jan 9, 2023 12:03 PM

And it will stay broken because it is attempting to call Python 2.7.16 which Apple removed from the operating system at macOS 12.3. It won't work with Python3 from the Apple Command Line tools because that Python does not support the Apple Scripting bridge that binds the CoreFoundation and CoreGraphics libraries to Python.


That said, Apple includes a Quick Action with the operating system where you select the PDF files in the Finder order you wish them to be combined, right-click on the first one, choosing Quick Actions > Create PDF. That will produce a new PDF on the Desktop containing the selected PDFs.

Similar questions

3 replies
Question marked as Top-ranking reply

Jan 9, 2023 12:03 PM in response to namniart

And it will stay broken because it is attempting to call Python 2.7.16 which Apple removed from the operating system at macOS 12.3. It won't work with Python3 from the Apple Command Line tools because that Python does not support the Apple Scripting bridge that binds the CoreFoundation and CoreGraphics libraries to Python.


That said, Apple includes a Quick Action with the operating system where you select the PDF files in the Finder order you wish them to be combined, right-click on the first one, choosing Quick Actions > Create PDF. That will produce a new PDF on the Desktop containing the selected PDFs.

Jan 9, 2023 7:55 PM in response to VikingOSX

I have python 3.9 installed through HomeBrew and the pyobjc module installed through pip for Homebrew, which is probably why this script works for me.


It looks like Apple is no longer shipping the pyobjc module. I'm able to install it with `/usr/bin/pip3 install pyobjc` , but that doesn't seem like a solution that is well suited to the average user.

Jan 10, 2023 4:26 AM in response to namniart

Yes, I too have the current pyobjc module installed with Python 3.11.1, and I didn't mention it here as it is out of scope for the average community user. I also do not wish to explain how it is done as someone will inevitably do it wrong and create a support issue.


Apple's Shortcuts features a Make PDF action that can merge PDFs by appending or shuffling, but no PDF watermarking functionality in Shortcuts. One can frustrate themselves with the macOS print panel's watermarking feature that simply does not impress me.



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.

Automator Combine PDF script still broken

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