Append same pdf to multiple pdfs

Hi I write also here because same post is in Automator but this section I think is better.
I found on this forum this script but I need a modification. This append a single-page pdf to a list of pdfs dragged in the script. I need the same but appending a multi-page pdf to each of the pdf. Can you help me? I tried alone but I am beginner.
Thanks

use AppleScript version “2.4”
use scripting additions
use framework “Foundation”
use framework “Quartz”

on open fileList – drag list of PDFs to be updated
set masterFile to POSIX path of (choose file with prompt “Choose the master file:” of type {“pdf”})
set listOfPosixPaths to {}
repeat with aFile in fileList
set end of listOfPosixPaths to POSIX path of aFile
end repeat
my insertPageOneFrom:masterFile intoDocuments:listOfPosixPaths
display dialog “Finished” buttons {“OK”}
end open

on insertPageOneFrom:posixPath intoDocuments:listOfPosixPaths
set theURL to current application’s class “NSURL”'s fileURLWithPath:posixPath
set thePDFDoc to current application’s PDFDocument’s alloc()'s initWithURL:theURL
set thePage to thePDFDoc’s pageAtIndex:0 – zero-based
repeat with aPath in listOfPosixPaths
set thisURL to (current application’s class “NSURL”'s fileURLWithPath:aPath)
set oldName to (thisURL’s lastPathComponent()'s stringByDeletingPathExtension()'s stringByAppendingString:“+”)
set newURL to ((thisURL’s URLByDeletingLastPathComponent()'s URLByAppendingPathComponent:oldName)'s URLByAppendingPathExtension:“pdf”)
set thisPDFDoc to (current application’s PDFDocument’s alloc()'s initWithURL:thisURL)
(thisPDFDoc’s insertPage:thePage atIndex:0)
(thisPDFDoc’s writeToURL:newURL)
end repeat
end insertPageOneFrom:intoDocuments:

I think, this should perform your task:


use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "Quartz"

on run
	set fileList to (choose file of type "pdf" with prompt "Choose PDF files to append the master PDF to them:" with multiple selections allowed)
	open fileList
end run

on open fileList -- drag list of PDFs to be updated
	set masterFile to POSIX path of (choose file with prompt "Choose the master PDF:" of type {"pdf"})
	set listOfPosixPaths to {}
	repeat with aFile in fileList
		set end of listOfPosixPaths to POSIX path of aFile
	end repeat
	my insertPDFFrom:masterFile intoDocuments:listOfPosixPaths
	display dialog "Finished" buttons {"OK"}
end open

on insertPDFFrom:posixPath intoDocuments:listOfPosixPaths
	
	set masterURL to current application's class "NSURL"'s fileURLWithPath:posixPath
	set masterPDFDoc to current application's PDFDocument's alloc()'s initWithURL:masterURL
	set masterDocCount to masterPDFDoc's pageCount() -- zero-based
	
	repeat with aPath in listOfPosixPaths
		set thisURL to (current application's class "NSURL"'s fileURLWithPath:aPath)
		set thisPDFDoc to (current application's PDFDocument's alloc()'s initWithURL:thisURL)
		set thisDocCount to thisPDFDoc's pageCount()
		set oldName to (thisURL's lastPathComponent()'s stringByDeletingPathExtension()'s stringByAppendingString:"+")
		set newURL to ((thisURL's URLByDeletingLastPathComponent()'s URLByAppendingPathComponent:oldName)'s URLByAppendingPathExtension:"pdf")
		
		repeat with i from 1 to masterDocCount
			set thePDFPage to (masterPDFDoc's pageAtIndex:(i - 1)) -- zero-based indexes
			(thisPDFDoc's insertPage:thePDFPage atIndex:thisDocCount)
			set thisDocCount to thisDocCount + 1
		end repeat
		
		(thisPDFDoc's writeToURL:newURL)
	end repeat
	
end insertPDFFrom:intoDocuments:

OMG is perfect Thank you!!!
Maybe you can tell how to modify that new generated pdf files are saved in a new folder called “new”?

thanks anyway

Other version - with New folder creating:


use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "Quartz"

on run
	set fileList to (choose file of type "pdf" with prompt "Choose PDF files to append the master PDF to them:" with multiple selections allowed)
	open fileList
end run

on open fileList -- drag list of PDFs to be updated
	
	tell application "Finder"
		set aContainerPath to (container of (item 1 of fileList)) as text
		set newFolderPath to aContainerPath & "New"
		if not (folder newFolderPath exists) then make new folder at folder aContainerPath with properties {name:"New"}
	end tell
	
	set masterFile to POSIX path of (choose file with prompt "Choose the master PDF:" of type {"pdf"})
	set listOfPosixPaths to {}
	repeat with aFile in fileList
		set end of listOfPosixPaths to POSIX path of aFile
	end repeat
	
	my insertPDFFrom:masterFile intoDocuments:listOfPosixPaths
	display dialog "Finished" buttons {"OK"}
end open

on insertPDFFrom:posixPath intoDocuments:listOfPosixPaths
	
	set masterURL to current application's class "NSURL"'s fileURLWithPath:posixPath
	set masterPDFDoc to current application's PDFDocument's alloc()'s initWithURL:masterURL
	set masterDocCount to masterPDFDoc's pageCount() -- zero-based
	
	repeat with aPath in listOfPosixPaths
		set thisURL to (current application's class "NSURL"'s fileURLWithPath:aPath)
		set thisPDFDoc to (current application's PDFDocument's alloc()'s initWithURL:thisURL)
		set thisDocCount to thisPDFDoc's pageCount()
		set oldName to thisURL's lastPathComponent()'s stringByDeletingPathExtension()
		set newURL to (((thisURL's URLByDeletingLastPathComponent()'s URLByAppendingPathComponent:"/New")'s URLByAppendingPathComponent:oldName)'s URLByAppendingPathExtension:"pdf")
		
		repeat with i from 1 to masterDocCount
			set thePDFPage to (masterPDFDoc's pageAtIndex:(i - 1)) -- zero-based indexes
			(thisPDFDoc's insertPage:thePDFPage atIndex:thisDocCount)
			set thisDocCount to thisDocCount + 1
		end repeat
		
		(thisPDFDoc's writeToURL:newURL)
	end repeat
	
end insertPDFFrom:intoDocuments:

works great! than you! Really appreciated! :wink: