简体   繁体   中英

Merge pdf by file name using Mac automator

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 "lst0090 10989 .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:

文件夹内容

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

合并 PDF 文件集 Automator 快速操作

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 eg 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 eg /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 , eg:

-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, eg:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM