Append (Concatenate) multipage PDFs from any number of containers or records in any oder with AppleScript

Version 1

    Source: Loop the Concatenation of Multiple PDFs from Container Fields of Multiple Records with AppleScript | Samuel Giles


    When I discovered there was no easy way to export the contents of a container (or multiple containers) and combine/append/concatenate them with/to a single PDF in a single FileMaker script, I was VERY disappointed. This was a core function of a database I was working on for client managing health records. Most forums indicate that the only option is to create separate PDFs and combine these afterward using Preview or Acrobat. Or, at best, append records to an exported container. Considering my client would be printing PDFs potentially hundreds of pages long with countless supporting documents, neither option would have been viable. I refused to give up.

    The solution is using AppleScript and including a shell command in the AppleScript to combine (concatenate) the PDFs -- OS X has a Python script installed by default (in /System/Library/Automator). It goes without saying, this only works on an Apple computer (OS X). Unlike other solutions, this makes possible the concatenation of PDFs from any number/combination of containers or records, and they can be combined in any order. In summary, it:

    1. Loops through the records and print each to append to File1,
    2. Exports the container PDF to File2 if one exists, and
    3. Concatenates the PDFs before moving on to the next record.


    The AppleScript is simple. The only difficulty is wrapping your head around writing a shell command (Python) using the parameters of AppleScript, and that all from the perspective of FileMaker. My AppleScript below uses the two files placed on the desktop in the FileMaker script above, moves them to a new folder, combines them, saves the file to the desktop, and then deletes the folder with the old files. A few important things to note:

    • I create the folder first because you can't use the same name for input and output files in the same location, and, without the same name, the loop in the FileMaker script wouldn't work.
    • The AppleScript takes a minute to complete, which seems like a delay, and can make the entire script drag on depending on how many and what size the PDF files are. Therefore, a custom dialogue in your FileMaker script would be a good idea to let the user know when the script is finished and where the file was saved. Don't worry, the FileMaker script will in no case continue before the AppleScript is finished executing. If it seems that your FileMaker script is continuing prematurely, then there is actually an error in the AppleScript.


    My AppleScript text:


    global outputFile

    set outputFile to (path to desktop) & "WhateverNameYouDefinedIn$$DesktopPath.pdf" as string

    set fileOne to (path to desktop) & "WhateverNameYouDefinedIn$$DesktopPath.pdf" as string

    set fileTwo to (path to desktop) & "WhateverNameYouDefinedIn$$DesktopPath2.pdf" as string


    global TempFolderPath

    tell application "Finder"

    make new folder at (path to desktop) with properties {name:"FileMakerTemp"}

    set TempFolderPath to (path to desktop as string) & "FileMakerTemp:"

    move file fileOne to TempFolderPath

    move file fileTwo to TempFolderPath

    end tell


    set inputOne to TempFolderPath & "WhateverNameYouDefinedIn$$DesktopPath.pdf" as string

    set inputTwo to TempFolderPath & "WhateverNameYouDefinedIn$$DesktopPath2.pdf" as string


    do shell script "/System/Library/Automator/Combine\\ PDF\\ Pages.action/Contents/Resources/ " & "-o " & quoted form of POSIX path of outputFile & " " & quoted form of POSIX path of inputOne & " " & quoted form of POSIX path of inputTwo


    tell application "Finder"

    delete folder TempFolderPath

    end tell


    That's it! See source blog for more detailed explanation as well as sample text for the FileMaker script. German translation also available.