简体   繁体   中英

Mac Automator - Combine PDF files, save in same folder

I'm using Automator to combine PDF files, but can't figure out how to automate saving the resulting file to the same folder. (Instead, it asks me where to put the file.)

I'm using it as a service - here's my existing workflow:

1) Service receives selected -PDF files- in -any application-
2) Combine PDF Pages [appending pages]
3) Rename Finder Items: Name Single Item [Name Single Item, Basename only, to "TestResults"]
4) Move Finder Items [to a folder - show action when workflow runs]

Ideally, I'd love the resulting file:

-- to be named based on (the folder where it's located) + (an appended static string)
-- saved in the folder where the files are


I've seen other solutions where people are Getting and Setting Variable Values, but when I've tried, it only seems that I'm naming the variables, not actually telling the program where to get the values.

(In my case, I was creating a variable called "folderPath" and trying to pass that along to the Move Finder Items action.)

Thanks to jweaks for his script. Super helpful and elegant.

I found and fixed a small bug:

I got his workflow to put the file into the same folder as the original PDFs rather than on the desktop by tweaking the Applescripts to talk to the Finder (instead of System Events) and return an alias, which seems to be what Move Finder Items is expecting:

So…

  1. Service receives PDF files in Finder
  2. Set Value of Variable : originalPDFs
  3. Run Applescript:

Revised Applescript:

on run {input}
    tell application "Finder" to return (container of item 1 of input) as alias
end run
  1. Set Value of Variable : containerAlias
  2. Run Applescript:

Revised Applescript 2

on run {input, parameters}
    -- the input is the enclosing folder which was output from the previous Applescript
    tell application "Finder" to return name of (item 1 of input) 
end run
  1. Set Value of Variable : containerName

  2. Get Value of Variable : originalPDFs ; select Options > "ignore this action's input"

  3. Combine PDF Pages
  4. Rename Finder Items: Name Single Item, Basename only, containerName "(combined)”
  5. Move Finder Items : container Alias

Hope this helps… I'm sure I'll look up this answer again in a few years :)

Here's a screenshot:

修订工作流程

There is no automator action to get the name of a file's container, nor is there an action to get the path of a file's container, so that you can set those to variables. So, you'll have to use an Applescript action to get each of those. Here is an example of a working automator workflow accomplishing what you have asked, assuming you place it in your service that accepts PDFs.

动作1行动2acitons3

You can do it straight from the CLI if your comfortable with that, with the code: "/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py" -o <output-file-name>.pdf <file-one>.pdf <file-two>.pdf . Would combine file-one.pdf and file-two.pdf.

Furthermore, i suggest creating a shell script, and putting it in the /usr/local/bin folder. Fx. create a file called combine_pdfs.sh and write the following in it:

read -p "Name of output file: "  name

"/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py" -o $name.pdf *.pdf 

This would prompt the user for an output name, and merge all the pdfs in the current path.

Note that this doesn't answer precisely the OPs task of selection multiple files and combining them. It merely applies a Quartz Filter to a single (or mutliple) PDF file(s) independently and renames them.

The presented solutions (now in 2022) seem overly complex. After trying them out first for a similar task, I switched to the simpler solution (on macOS Big Sur 11.6.4) of creating a "Quick Action" containing:

  • Duplicate Finder Items
  • Rename Finder Items: Replace Text with the options: Replace Text , find copy (notice the space) in basename only , replace: _reduced .
  • Apply Quartz Filter to PDF Documents

The only assumption here is that there is not yet a copy of that file so that step 1 effectively generates a file with filename copy.pdf instead of filename copy 2.pdf .

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