16 C
United States of America
Saturday, November 23, 2024

applescript – Script / automator motion that mixes pdfs with the identical title from completely different folders


I might use the third-party command line software, cpdf, and script the method of merging the PDF paperwork.

Take a look at: Coherent PDF Command Line Instruments Neighborhood Launch

  • Obtain pre-built cpdf software (This can be a direct obtain hyperlink. Constructing from supply is offered from hyperlink on most important web page linked above.)
  • A complete person guide for the PDF instruments will be discovered as a PDF doc, or browsable on-line.
  • Observe: the Neighborhood Launch model of Coherent PDF Command Line Instruments is free fo non-commercial use.

Copy the suitable cpdf binary executable from the extracted cpdf-binaries-master.zip file to a listing that’s within the PATH handed to the shell.


To make use of the instance shell script code:

In Terminal, run the next compound command:

f="mergepdf"; contact "$f"; open -e "$f"; chmod 0755 "$f"

Within the opened mergepdf doc, copy and paste the instance shell script code, proven beneath, into the doc, after which save and shut the doc.

  • Observe: For those who did do not copy the cpdf binary executable to a listing that’s within the PATH handed to the shell, then use the absolutely certified pathname of the cpdf binary executable within the shell script.

Again in Terminal:

./mergepdf

Outputs:

Parameter lacking!...
Instance: mergepdf '/path/to/PDF Recordsdata 1' '/path/to/PDF Recordsdata 2' '/path/to/Merged PDF Recordsdata'
  • Observe: I might transfer the mergepdf file to a location that is inside the PATH handed to shell scripts, then you definately’d not should preface the command with ./, or embody it is absolutely certified pathname.

Instance shell script code:

#!/bin/bash

if [ -z "${3}" ]; then
    echo "Parameter lacking!..."
    echo "Instance: mergepdf '/path/to/PDF Recordsdata 1' '/path/to/PDF Recordsdata 2' '/path/to/Merged PDF Recordsdata'"
    exit 1
fi  

oridir="${1}"
dupdir="${2}"
merdir="${3}"

if [ ! -d  "${oridir}" ]; then
    echo "The listing "${oridir}" doesn't exist!..."
    exit 1
fi

if [ ! -d  "${dupdir}" ]; then
    echo "The listing "${dupdir}" doesn't exist!..."
    exit 1
fi

if [ ! -d  "${merdir}" ]; then
    echo "The listing "${merdir}" doesn't exist!..."
    exit 1
fi

whereas IFS= learn -r orifile; do

    whereas IFS= learn -r dupfile; do

        if [ -e "${dupfile}" ]; then
            if [ ! -e "${merdir}/${orifile##*/}" ]; then
                cpdf "${orifile}" "${dupfile}" -o "${merdir}/${orifile##*/}" 2>/dev/null
            else
                echo ""${orifile##*/}" already exists in "${merdir}"."
            fi
        else
            echo "No match discovered for "${orifile}" in "${dupdir}/"."
        fi

    completed <<< "$(discover "${dupdir}" -type f -name "${orifile##*/}")"

completed < <(discover "${oridir}" -type f -iname '*.pdf')

What the shell script does:

Finds of all recordsdata with a pdf extension in '/path/to/PDF Recordsdata 1' after which makes an attempt to search out the file of the identical title in '/path/to/PDF Recordsdata 2'.

If it finds the file of the identical title in '/path/to/PDF Recordsdata 2' it merges the 2 recordsdata with a pdf extension having the identical title to '/path/to/Merged PDF Recordsdata', merging the second file to the top of the primary file. Observe that different command line choices for the cpdf binary executable can be found.

If a duplicate .pfd doc of the identical title just isn’t present in '/path/to/PDF Recordsdata 2', then a message it output.

As coded, present recordsdata in '/path/to/Merged PDF Recordsdata' should not overwritten. If a file exists a message is output.


Notes:

As a result of the cpdf binary executable just isn’t signed, you will have to permit it to run by clicking the [Allow Anyway] button in: System Preferences > Safety & Privateness > Basic

The instance shell script code, proven above, was examined as an executable shell script in Terminal below macOS Catalina with Language & Area settings in System Preferences set to English (US) — Major and labored for me with out concern1.

  • 1 Assumes obligatory and acceptable setting in System Preferences > Safety & Privateness have been set/addressed as wanted.

Testing below macOS Massive Sur 11.4, I didn’t have any concern with the cpdf binary executable regarding its signing and it merely labored by copying the OSX-Intel model of cpdf to a location that is inside the PATH handed to shell scripts.


Observe: The instance shell script code is simply that and sans any included error dealing with doesn’t comprise any further error dealing with as could also be acceptable. The onus is upon the person so as to add any error dealing with as could also be acceptable, wanted or needed.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles