Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

TBast

macrumors newbie
Original poster
Jul 23, 2019
5
1
United Kingdom
Hi all

I'm looking for a way to combine a number of PDF's into 1 Merged document from a folder via a txt list. I need the running order of the merged document follow the order to the list, rather than being in numerical order.

I currently have a script that moves PDFs from one folder to another based on a txt list, wondered if it could be adapted

"set path2source to choose folder with prompt "Select the source folder" default location (path to desktop)
set path2dest to choose folder with prompt "Select the destination folder" default location (path to desktop)
set path2list to choose file with prompt "Select your textList" of type {"txt"}

set theList to paragraphs of (read path2list)

tell application "Finder"
set theNames to name of files of path2source
repeat with aName in theNames
if (aName as text) is in theList then
move file aName of path2source to path2dest
end if
end repeat
end tell"


Can anyone offer some insight or help out, would be hugely appreciated :)
 
You can apparently use the Automator.app to merge PDF's, but I haven't looked into this myself.
On this one best Google "Merge PDF's with Automator"

The code you posted above is simply for moving PDF Documents from a source folder to a destination folder.
It does not do any document combining or merging.

Personaly I would use the PDFKit Framework in Mac OS for PDF Document manipulation.
On my old Mac OS the PDFKit Framework is part of the Quartz Framework.
But my understanding is on later Mac OS systems the PDFKit Framework is separate to the Quartz Framework.
You can check this out by using "Finder" and navigating to "/System/Library/Frameworks/" and checking if the PDFKit Framework exists in there.
If Not then there should be the Quartz Framework in there instead.

Depending on the MAC OS version your on, you could use something like this code posted below.
But it would very much be OS version dependant.

AppleScript:
use scripting additions
use framework "Foundation"
use framework "Quartz"

try
    set sourcePDFDocList to choose file with prompt "Choose the PDF document files you want to combine." of type {"pdf"} default location (path to desktop folder) with multiple selections allowed
on error
    return -- User canceled the PDF Document selection
end try

if (count of sourcePDFDocList) is less than 2 then -- User selected only one PDF document so no merging possible
    return
end if

try
    set destPDFDocPath to choose file name with prompt "Select the PDF document folder and file name." default name "Combined.pdf" default location (path to desktop folder)
on error
    return -- User canceled the PDF destination folder and file selection
end try

set combinedPDFDocCreated to my combinePDFDocsInList:sourcePDFDocList savingPDFDocToPath:destPDFDocPath
if combinedPDFDocCreated then
    return "Success"
else
    return "Failure"
end if

on combinePDFDocsInList:sourcePDFDocList savingPDFDocToPath:destPDFDocPath
    set theCombinedPDFDoc to current application's PDFDocument's alloc()'s init()
    set theCombinedPDFDocPageCount to theCombinedPDFDoc's pageCount()
    repeat with thePDFDoc in sourcePDFDocList
        set thePDFDocPath to POSIX path of thePDFDoc as text
        set thePDFDocURL to (current application's NSURL's fileURLWithPath:thePDFDocPath)
        set thePDFDocFile to (current application's PDFDocument's alloc()'s initWithURL:thePDFDocURL)
        set thePDFDocFilePageCount to thePDFDocFile's pageCount()
        repeat with i from 1 to thePDFDocFilePageCount
            set thePDFDocPage to (thePDFDocFile's pageAtIndex:(i - 1))
            (theCombinedPDFDoc's insertPage:thePDFDocPage atIndex:theCombinedPDFDocPageCount)
            set theCombinedPDFDocPageCount to theCombinedPDFDocPageCount + 1
        end repeat
    end repeat
    set theCombinedPDFDocPath to POSIX path of destPDFDocPath
    set theCombinedPDFDocURL to current application's NSURL's fileURLWithPath:theCombinedPDFDocPath
    set combinedPDFDocCreated to theCombinedPDFDoc's writeToURL:theCombinedPDFDocURL
    return combinedPDFDocCreated
end combinePDFDocsInList:savingPDFDocToPath:

This code would allow you to select multiple PDF Documents, and then combine them into one merged PDF Document.
But only tested on Mac OS 10.13.

If your on Mac OS 10.13, then Copy and Paste the above code into the "Script Editor.app", and you can see the possabilities for PDF Document manipulation with the PDFKit Framework.

I dont know what is in your "path2list" text file, so I can't incorporate this data into the above code.
So you would have to let us know what is in this file, before we can help using this data in the above code.

Regards Mark
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.