Automating merging PDFs with specific file each time

Hi everyone,


I'm a school teacher and I am looking for a way to automate a very repetitive task.


I receive PDF scans of student work and I need to insert an assessment pro forma cover sheet at the start of the work so I can give feedback once the work is marked.


I'd like to automate this but all the help I can find about merging PDFs only seems to work for the currently selected files, not for a specified file.


So to be clear, my current workflow is:

Open student work PDF.

Open cover sheet PDF.

Copy pages from cover sheet PDF.

Paste into student work PDF.

Move these pages to start of document.

Save.


Is there a way I can create a service that will do this for me quickly please? Ideally for a batch of files also.


Many thanks!


Todd

MacBook Air 13″, macOS 12.3

Posted on Jun 7, 2022 12:32 AM

Reply
Question marked as Top-ranking reply

Posted on Jun 7, 2022 9:51 AM

Ok. I am satisfied with the results from the Quick Action prepending the assessment PDF form to the beginning of the student scans, whether they are one or multiple pages in length. A scan named JC_scan.pdf is output as JC_scan_assessed.pdf.


Tested on macOS 11.6.6 and 12.4.


Though strategically Apple's Shortcuts are the way to go, I was developing this on Big Sur, so used Automator to create a Quick Action. If I have time, I will submit a Shortcut solution later.


  1. From /Applications folder, double-click Automator to launch it.
    1. New Docum,ent > Quick Action > Choose.
    2. Workflow receives current [ PDF files ] in [ Finder ]
  2. From Library > Files & Folders > drag/drop Get Specified Finder Items to the larger workflow window.
    1. Add your Assessment PDF form
  3. From Library > Utilities > drag/drop Run AppleScript action below the preceding action in the work flow window
    1. Remove all content in the Run AppleScript action window.
    2. Copy/paste AppleScript code below into the Run AppleScript window
    3. Click the hammer icon to compile the AppleScript
  4. Save the Automator Quick Action with a readily identifiable name (e.g. Prepend Assessment PDF to Scans)
  5. The Quick Action will automatically be added to the secondary Finder menu > Quick Actions when scanned PDF/s are selected in the Finder.
  6. Quit Automator


Run AppleScript code:


use framework "Foundation"
use framework "PDFKit"
use AppleScript version "2.4" -- Yosemite (10.10) or later
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}
	
	-- this is the assessment form from the Get Specified Finder Items action
	set PDFForm to POSIX path of (first item of input) as text
	
	-- now process the selected student scan PDFs
	repeat with studentScan in (rest of input) as list
		
		set assessPDF to (PDFDocument's alloc()'s initWithURL:(NSURL's fileURLWithPath:PDFForm))
		
		set studentPDFPath to (NSString's stringWithString:(POSIX path of studentScan as text))
		set studentPDFAssessed to (studentPDFPath's stringByDeletingPathExtension()'s stringByAppendingString:"_assessed.pdf")
		set studentPDF to (PDFDocument's alloc()'s initWithURL:(NSURL's fileURLWithPath:studentPDFPath))
		
		repeat with n from 0 to studentPDF's pageCount()
			(assessPDF's insertPage:(studentPDF's pageAtIndex:n) atIndex:(assessPDF's pageCount()))
		end repeat
		(assessPDF's writeToFile:studentPDFAssessed)
		
	end repeat
	
	return input
end run


Finished workflow:



Similar questions

17 replies

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.

Automating merging PDFs with specific file each time

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