Reputation: 169
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:
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:
Upvotes: 1
Views: 929
Reputation: 7555
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
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:
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
Upvotes: 1