1
votes

I am trying to get automator to merge PDF pages from two files with similar names.

Right now I already have a service workflow in automator that is able to merge two selected files:

enter image description here

While this workflow is certainly useful, I still have to manually select the files to be merged one by one. Also, the resulting file name is being renamed with an apparently random string of letters after the set words.

I will mainly use this script for getting numbered files (ex: 10989.pdf) merged with files containing those numbers, with names like "lst009010989.pdf". Those files are all in a folder with various other files with names following the same system, so what I need is for the workflow to get the numbered files and join them to their respective lst file, copy it to another folder while maintaining the numbered file name (in this case, 10989) and then doing the same for the other files in the folder.

How am I able to do this?

Additional information: The first page of the merged file should always be the one of the numbered file (10989 in the exemple). The second one should be the one of the LST file. The resulting file should be named NF_LST xxxxx.pdf (xxxx being the name of the numbered file, 10989 in this case).

The lst filename always starts with lst and has 14 characters, the last of them always being the same as the numbered file name. The numbered filename changes from 3 characters to 6.

The following image shows what a typical folder contains:

folder contents

1
You've left out some very important information! 1. When combining pages which file's pages are first and which file's pages are second in the combined document? 2. Which filename is to be used when renaming, will it be e.g. FN_LST 10989.pdf or FN_LST lst009010989.pdf 3. Are the number of digits always 9 with lst….pdf files and 5 with the corresponding numeric filename files? 4. Do the non-numeric filenames all start with lst? Frankly, you should include an appropriate partial directory listing so we know what you're dealing with.user3439894
5. Are there files other then the ones to combine in the same directory, or just corresponding files to be combined?user3439894
Sorry for that, I will update de question. So, the first page should be the one of file 10989.pdf, and the second should be the one of the lst file. The edited filename should be FN_LST 10989.pdf. All the non numeric filenames start with LST. The numbers following LST may change but the last numbers are always the same as the numbered file name (in this case, 10989).Ricardo Milhomem
The LST filenames are always 14 characters, but the numbered files vary from 3 characters to 6. I added a print screen of a folder containing the files. Thanks in advance for you patience! @user3439894Ricardo Milhomem
What version of macOS are you running? E.g. macOS Catalina 10.15.4user3439894

1 Answers

1
votes

The following has been tested under macOS Catalina 10.15.4 and 10.15.5 and it did not work for me under 10.15.5 as a Quick Action. It appears there is a bug in the Run Shell Script action when using the find command as formed. It also works under macOS High Sierra, and also as shell script in the three test environments. This further bolsters the bug assumption in 10.15.5.

This requires the use of cpdf from the third-party, free, Coherent PDF Command Line Tools Community Release -- Direct download link: Download pre-built cpdf tool

Combine PDF File Sets Automator Quick Action

Example bash script code:

for i in "$@"; do
    [[ ${i} =~ .*/[0-9]{3,6}.*\.[pP][dD][fF] ]] || continue
    j="$(find "${i%/*}" -type f -iname "lst*${i##*/}")"
    [[ ${j} =~ .*/lst.*[0-9]{3,6}\.[pP][dD][fF] ]] || continue
    l=${j##*/}
    [[ ${#l} -eq 18  ]] || continue
    /usr/local/bin/cpdf -merge "${i}" "${j}" -o "${i%/*}/FN_LST ${i##*/}"
done

As coded, it performs the following:

  • Checks the files passed are a 3 to 6 digit numeric named PDF file.
  • Finds the corresponding PDF file, in the same folder, starting with lst and ending with the same 3 to 6 digit numeric named PDF file.
  • If a corresponding PDF file is not found it moves to the next file.
  • Validates the length of the found corresponding PDF file to be 18 characters, including the extension.
  • Combines the matching sets with the 3 to 6 digit numeric named PDF file first in the new combined file.
  • Creates the combined file as e.g. FN_LST [0-9]{3,6}.pdf, (FN_LST 10989.pdf), in the same folder, which can be changed.
  • Does not overwrite an existing file.

To change the location of where the new combined files are created, in the e.g. /usr/local/bin/cpdf ... line, change ${i%/*} in:

-o "${i%/*}/FN_LST ${i##*/}"

To:

-o "/path/to/FN_LST ${i##*/}"

Where /path/to is the fully qualified directory pathname, e.g.:

-o "$HOME/Documents/Combined PDF Files/FN_LST ${i##*/}"

Note: The /path/to directory must already exist as there is no error handling for it, although that too can be added, e.g.:

d="HOME/Documents/Combined PDF Files"
[[ -d ${d} ]] || mkdir -p "${d}"

for i in "$@"; do
    [[ ${i} =~ .*/[0-9]{3,6}.*\.[pP][dD][fF] ]] || continue
    j="$(find "${i%/*}" -type f -iname "lst*${i##*/}")"
    [[ ${j} =~ .*/lst.*[0-9]{3,6}\.[pP][dD][fF] ]] || continue
    l=${j##*/}
    [[ ${#l} -eq 18  ]] || continue
    /usr/local/bin/cpdf -merge "${i}" "${j}" -o "${d}/FN_LST ${i##*/}"
done