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
Question marked as Top-ranking reply

Jun 7, 2022 9:51 AM in response to toddfromgloucester

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:



Jun 15, 2022 4:57 AM in response to VikingOSX

Hi Todd and VikingOSX.


Todd, may you find this useful.


VikingOSX, you can run your AppleScript unmodified in Shortcuts on macOS Monterey. You just need to set up inputs 'the Shortcuts way.' The shortcut uses the following actions sequence:


1. Quick Actions input - appears when Use as Quick Action is selected from the Shortcut Details menu

2. File

3. Set Variable

4. Add to Variable

5. Run AppleScript


You can download the shortcut from this link: Prepend Assessment PDF to Scans AS

The workflow:



You can also accomplish the same result without scripting using the following actions sequence:


1. Quick Actions input - appears when Use as Quick Action is selected from the Shortcut Details menu.

2. Repeat with Each

3. Get Name

4. File

5. Set Variable

6. Add to Variable

7. Make PDF

8. Save File

9. End Repeat (which is the automatic bookend to the Repeat with Each action)


You can download the shortcut from this link: Prepend Assessment PDF to Scans SC


The workflow:



For testing, I used a PDF version of Pages’ Academic Report template for the assessment form, and PDF versions of Pages’ School Report and Term Paper templates for the two student scans. The test documents used and the identical results for both shortcuts:



Regards, Roote.


Mac mini (2018), macOS Monterey 12.4, Shortcuts 5.0


Shortcuts User Guide for Mac

Jun 7, 2022 5:50 AM in response to toddfromgloucester

Once Apple removed the Python 2.7.18 distribution in macOS 12.3.1, it broke the Combine PDF Automator action whose work horse is a Python script.


A Quick Action can still work on multiple Finder selected student scanned PDF by using a Run AppleScript action with the specific code to perform the work. The student PDF pages would be appended to the coversheet, and then that content written out with the name of the student scan document and the suffix of your choice appended to it. One can also use a single Cover sheet like a template, make a copy of it, append the student scan to that copy, and then overwrite. Clarify what gets overwritten if that is your preference.


This is the workflow I envision:


Jun 7, 2022 3:52 AM in response to toddfromgloucester

What is the easiest workflow for you:

  1. Click on a student scanned PDF in the Finder and the solution takes that selection, prepends the assessment prologue to it, and then writes out a new PDF with "_assessed" as suffix.?
  2. Drag and drop a student's scanned PDF onto an application and then have it do the same processing as [1]?
  3. Double-click the application on your Desktop (or single-click in the Dock), and be prompted for the student's scanned PDF, then previous processing applied [1]?
  4. Place the result from the above into a designated folder?


The suffix can be anything you want, and I believe this is beneficial so that you know what has been processed and what has not.


Right now, I have item 3 working where it appends the student's scanned PDF to the end of the assessment PDF and then writes out the result with the "_assessed" suffix on the original student scan PDF's filename. I place a specific path in the solution to the assessment PDF to avoid a second prompt for input from you.

Jun 7, 2022 4:29 AM in response to VikingOSX

Hi VikingOSX!

Thanks for getting back to me.

My initial thought was to be able to right click the PDF and have a Quick Action to 'Attach Cover Sheet'. I tried doing this with Automator but it appears that the combine PDF function doesn't work in Monterey. So I suppose that's what you suggest with option 1 but without duplicating the file, simply overwriting it (I don't need the original file in addition)

Ideally I'd be able to do this in bulk so I can grab a group of PDFs and process them all in one go but this isn't absolutely necessary if it's more challenging.

Option 2 or 3 would also work fine although I do to try and keep my dock to a minimum.


Thanks so much for your help.

Jun 12, 2022 10:26 AM in response to toddfromgloucester

There is a couple reasons that I haven't posted the Monterey Shortcuts version of this Quick Action.

  1. Difficult to set up as one cannot just choose to open the Automator Quick Action in Shortcuts… it fails to convert it.
  2. Difficult to set up due to Shortcuts workflow and settings. In fact, I had it working, accidentally deleted that QA, and now cannot seem to recreate it, nor find it to restore from Time Maochine backup over several days.
  3. Different AppleScript code to accommodate Shortcuts input quirks.


So, the Shortcuts version of this QA is too much of a PITA to invest further time on it.

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.