Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Question:

Question: Automator - Combine PDFs, name result correctly

I am trying to create an automator service that will:


1. Take a selection of PDF's from the finder

2. Combine the PDF's into a single document (got this part)

3. Allow the user to select what the name of the resulting PDF file will be based on the filename of one of the input files (presumably selected by the user from a prompt at this point)

4. add the word "combined" to the filename

5. save to the same location

6. move the original selected files to a new folder (for later manual deletion).


Please can anyone help with this?

MacBook Air, iOS 11.3, null

Posted on

Reply
Question marked as Solved
Answer:
Answer:

Here are the two fixes for item 2 Run AppleScript, and the last action, Run Shell Script. The following actions are removed:

  • Item 4 - Ask for Finder Items

    Prompt for output folder name

  • Item 5 - Set Value of Variable

    Saving Original_delete_folder variable

  • Item 11 - Get Value of Variable

    Retrieving Original_delete_folder variable

  • Original_delete_folder variable itself. Right-click on it to remove it from bottom of Automator variable stack.


Replace content of Item 2 Run AppleScript with the following code. It builds a list of human readable filenames for the list selection, and then passes out the full path to the selected PDF combination file name.


use scripting additions


on run argv

if (count of item 1 of argv) ≤ 1 then

display alert "Either 1 or no input files selected." message "Please select at least two files in Finder before running Automator." as critical

error -128

end if

set this_list to {}

set temp to (item 1 of argv)

tell application "Finder"

repeat with anItem in temp

-- human friendly filenames for the list selection

set the end of this_list to name of anItem as text

end repeat


-- list items will appear in the order they were selected in the Finder

set out_name to (choose from list this_list with title "PDF Outfile Name" with prompt "Select the PDF file to base the joined PDF output upon" without multiple selections allowed and empty selection allowed) as text

if out_name is false then return

-- match list selection to original filename, and return POSIX path of designated, combined PDF filename

repeat with elem in temp

if name of elem is equal to out_name then

set out_file to POSIX path of elem as text

end if

end repeat

end tell

-- last file activity in an action is passed to next action

out_file

end run

Replace the contents of the last Run Shell Script action with the following. It hard-codes the destination folder as ~/Desktop/PDFs for deletion as requested, tests for, and creates this folder if it is missing, and then moves the original files into it. The $HOME variable is equivalent to tilde, and I use it to fully expand the path. I have tested this with and without the output folder and it works as expected.


# Following test statement needs expanded, not relative (e.g. tilde) path

OUT_FOLDER="$HOME/Desktop/PDFs for deletion"


# guard conditional test to make folder if missing

if [ ! -d "$OUT_FOLDER" ]; then

mkdir "${OUT_FOLDER}"

fi


# move original PDF files into the output folder

for f in "$@"

do

mv "${f}" "${OUT_FOLDER}"

done


osascript -e "display alert \"Processing Completed\" as informational"

Posted on

Question marked as Helpful

Apr 22, 2018 3:44 PM in response to mik239 In response to mik239

Ok. I have a tested, 13 action, Automator Service named Combine and Move PDF Files that is tied to a option+command+X keystroke, and successfully achieves your stated goals.


The service receives PDF files from Finder. It then contains the following actions in order:

  1. Set Value of Variable

    Stores the inbound PDF files in pdf_files

  2. Run AppleScript
    1. Gets the PDF Files, makes a list of them
    2. Prompts with a list selection, where selected filename is output PDF name (before _combined).
  3. Set Value of Variable

    Store the output PDF name in pdf_outfile

  4. Ask for Finder Items (get the folder name where original PDF will be moved)
    1. Start at: Desktop
    2. Type: Folder (allow multiple selection unchecked)
    3. Options: √ Ignore this action's input
  5. Set Value of Variable

    Store preceding folder name in original_delete_folder

  6. Get Value of Variable
    1. Retrieve pdf_files variable as input to Combine PDF action
    2. Options: √ Ignore this action's input
  7. Combine PDF Pages
  8. Move Finder Items (move the random-named combined PDF file)

    To: Desktop

  9. Get Value of Variable

    Retrieve pdf_outfile

  10. Run Shell Script (Items 8 and 9 are stacking input variables for this action)
    1. Pass input: as arguments
      1. $1 = item 8's raw combined PDF file
      2. $2 = item 9's chosen output PDF filename
    2. Rename the raw combined PDF to the item 9 variable amended to include "_combined"
  11. Get Value of Variable
    1. Retrieve original_delete_folder name
    2. Options: √ Ignore this action's input
  12. Get Value of Variable

    Retrieve pdf_files

  13. Run Shell Script (items 11 and 12 are stacked input variables for this action)
    1. Pass input: as arguments
      1. $1 = original_delete_folder
      2. rest = pdf_files
    2. Move the pdf_files into the specified original_delete_folder
    3. Issue processing complete message dialog


Code


Replace boilerplate in Item 2 Run AppleScript with the following:


use scripting additions


on run argv

if (count of item 1 of argv) ≤ 1 then

display alert "Either 1 or no input files selected." message "Please select at least two files in Finder before running Automator." as critical

error -128

end if

set this_list to {}

set temp to (item 1 of argv)

repeat with anItem in temp

set the end of this_list to POSIX path of anItem as text

end repeat


set out_file to (choose from list this_list with title "PDF Outfile Name" with prompt "Select the PDF file to base the joined PDF output upon" without multiple selections allowed and empty selection allowed) as text

if out_file is false then return

out_file

end run

Replace boilerplate in item 10 Run Shell Script with the following:

# rename the raw combined pdf to selected output file from list selection

mv "${1}" "${2%.pdf}_combined.pdf"


Replace boilerplate in item 13 Run Shell Script with the following:


outfolder="${1}"

shift

for f in "$@"

do

mv "${f}" "${outfolder}"

done


osascript -e "display alert \"Processing Completed\" as informational"


Once you save the Service from Automator, visit System Preferences : Keyboard panel : Shortcuts : Services, and then scroll down the Services panel until you find this service name. In the right column, you will see a faint gray none. Double-click, and keystroke in your keyboard shortcut that does not conflict with any already set in Finder. Close the System Preferences panel. Your Service's keyboard shortcut is active.

Question marked as Helpful

Apr 23, 2018 7:30 PM in response to mik239 In response to mik239

  1. Delete file destination folder: I would like to be able to set the value of original_delete_folder with a value I input in the process rather than user selected - to make the process faster and less user dependent. For example the trash, or a folder on the desktop "PDFs for deletion" or whatever.
  2. File name selection dialogue - would like to have this appear without the full path - just the file name and extension.


Item 2 is done and tested this morning.


Am I reading item 1 as you want to entirely remove the Ask for Finder Items folder selection entirely? That was the only workable way that I have found to store a destination folder in the original_delete_folder Automator variable at runtime. How do you propose to non-interactively supply the destination folder to the Automator workflow?


I will be out of town this morning and probably into the afternoon, so won't be able to revisit this until later today.

There’s more to the conversation

Read all replies

Page content loaded

Apr 20, 2018 10:26 AM in response to mik239 In response to mik239

I have this working in an Automator workflow. Here is the action flow:

  1. Ask for Finder Items
    1. Start at: Desktop
    2. Type: Files and Multiple selection is selected.
      1. Note 1: Chose not to use Get Selected Finder Items as unreliable.
      2. Note 2: The order of selected PDF files should be the order within the combined PDF
  2. Run AppleScript
    1. This makes a list from the Ask for Finder Items PDF selection.
    2. Copies the PDF filename list to the clipboard as this will be used later in the original file moving process.
    3. Prompt user with a list from which to choose the combined PDF filename
    4. Prompt user for the folder to move the original PDF too.
    5. Set two preexistent Automator variables and set these with the combined PDF name string, and the destination folder respectively.
      1. combined_pdf
      2. PDF_Delete_Candidates
    6. Passes the original PDF selections through to the next action.
  3. Combine PDF Pages
  4. Get Value of Variable - combined_pdf
  5. Run Shell Script
    1. Pass Input : Arguments
      1. The temporary location of the combined PDF
      2. The stored value of the combined PDF filename in the combined_pdf variable
    2. Simple Bash move (mv) statement to rename the temporary combined PDF to the name stored in the combined_pdf Automator variable.
  6. Get Value of Variable - PDF_Delete_Candidates
  7. Run AppleScript
    1. Get the value of the passed in folder name
    2. Perform a Finder repeat loop moving the original filenames from the stored clipboard list into the folder path stored in the Automator variable.


After you set up the actions in your workflow, at the bottom of the workflow, you will see two icons.

User uploaded file

Click the rightmost symbol, and you will see an empty area with the heading Variables. Right-click in this empty area, and choose New Variable from the secondary menu. You will want to do this twice. Just give them the following names, and leave the Value field alone:

  • combined_pdf
  • PDF_Delete_Candidates


Here is the contents of item 2 above (Run AppleScript). Just copy and replace the content of your Run AppleScript action. The choose statements are long and wrap. The last set value of variable statement wraps.


property aDesktop : (path to desktop)


on run {input, parameters}

set {TID, AppleScript'stext item delimiters} to {AppleScript'stext item delimiters, ","}

set seLst to text items of (input as text)

set the clipboard toseLst

set AppleScript'stext item delimiters to TID

set thePDF to (choose from listseLstwith title "Combined PDF Path and Name" with prompt "Pick Combined PDF Output Name:" default itemsnone without multiple selections allowed and empty selection allowed)

if the result is false then error -128

set destination_folder to (choose folderwith prompt "Folder to receive original files after PDF combine:" default locationaDesktop without invisibles, multiple selections allowed and showing package contents)


set {TID, AppleScript'stext item delimiters} to {AppleScript'stext item delimiters, "."}

set path_ext to text items of (thePDF as text)

set AppleScript'stext item delimiters to TID

-- the Automator workflow variables must exist beforehand as part of the workflow setup

set value of variable "PDF_Delete_Candidates" of front workflow to destination_folder

set value of variable "combined_pdf" of front workflow to POSIX path of ((item 1 of path_ext) & "_combined." & "pdf") as text

return input

end run


Here is the content of item 5 (Run Shell Script). Just copy and replace the boilerplate content of your Run Shell Script action:


# move and rename the combined PDF file from its temporary folder location to the

# original path stored in the Automator variable "combined_pdf".

mv "$1" "$2"


And finally, the contents of item 7 (Run AppleScript) that moves your original files to the designated folder. Again, copy and paste this to replace the boilerplate Run AppleScript content.


-- get the list of original selected PDF items from the clipboard and

-- move them into the designated folder for subsequent manual deletion

on run argv

if length of argv = 0 then return


set move_folder to (item 1 of argv) as text

tell application "Finder"

repeat with anItem in (get the clipboard as list)

moveitem (anItem as alias) tomove_folder with replacing

end repeat

end tell

return

end run

Apr 20, 2018 10:26 AM

Reply Helpful

Apr 20, 2018 11:20 AM in response to VikingOSX In response to VikingOSX

My post was premature, as the workflow performs as expected when run from within Automator, or if right-clicked, and opened with Automator and then run. Otherwise, AppleScript blows up because I believe it cannot set the Automator variables because it is Automator Runner, and not Automator that is in control.


So another approach is necessary to solve your sequence of events.

Apr 20, 2018 11:20 AM

Reply Helpful
Question marked as Helpful

Apr 22, 2018 3:44 PM in response to mik239 In response to mik239

Ok. I have a tested, 13 action, Automator Service named Combine and Move PDF Files that is tied to a option+command+X keystroke, and successfully achieves your stated goals.


The service receives PDF files from Finder. It then contains the following actions in order:

  1. Set Value of Variable

    Stores the inbound PDF files in pdf_files

  2. Run AppleScript
    1. Gets the PDF Files, makes a list of them
    2. Prompts with a list selection, where selected filename is output PDF name (before _combined).
  3. Set Value of Variable

    Store the output PDF name in pdf_outfile

  4. Ask for Finder Items (get the folder name where original PDF will be moved)
    1. Start at: Desktop
    2. Type: Folder (allow multiple selection unchecked)
    3. Options: √ Ignore this action's input
  5. Set Value of Variable

    Store preceding folder name in original_delete_folder

  6. Get Value of Variable
    1. Retrieve pdf_files variable as input to Combine PDF action
    2. Options: √ Ignore this action's input
  7. Combine PDF Pages
  8. Move Finder Items (move the random-named combined PDF file)

    To: Desktop

  9. Get Value of Variable

    Retrieve pdf_outfile

  10. Run Shell Script (Items 8 and 9 are stacking input variables for this action)
    1. Pass input: as arguments
      1. $1 = item 8's raw combined PDF file
      2. $2 = item 9's chosen output PDF filename
    2. Rename the raw combined PDF to the item 9 variable amended to include "_combined"
  11. Get Value of Variable
    1. Retrieve original_delete_folder name
    2. Options: √ Ignore this action's input
  12. Get Value of Variable

    Retrieve pdf_files

  13. Run Shell Script (items 11 and 12 are stacked input variables for this action)
    1. Pass input: as arguments
      1. $1 = original_delete_folder
      2. rest = pdf_files
    2. Move the pdf_files into the specified original_delete_folder
    3. Issue processing complete message dialog


Code


Replace boilerplate in Item 2 Run AppleScript with the following:


use scripting additions


on run argv

if (count of item 1 of argv) ≤ 1 then

display alert "Either 1 or no input files selected." message "Please select at least two files in Finder before running Automator." as critical

error -128

end if

set this_list to {}

set temp to (item 1 of argv)

repeat with anItem in temp

set the end of this_list to POSIX path of anItem as text

end repeat


set out_file to (choose from list this_list with title "PDF Outfile Name" with prompt "Select the PDF file to base the joined PDF output upon" without multiple selections allowed and empty selection allowed) as text

if out_file is false then return

out_file

end run

Replace boilerplate in item 10 Run Shell Script with the following:

# rename the raw combined pdf to selected output file from list selection

mv "${1}" "${2%.pdf}_combined.pdf"


Replace boilerplate in item 13 Run Shell Script with the following:


outfolder="${1}"

shift

for f in "$@"

do

mv "${f}" "${outfolder}"

done


osascript -e "display alert \"Processing Completed\" as informational"


Once you save the Service from Automator, visit System Preferences : Keyboard panel : Shortcuts : Services, and then scroll down the Services panel until you find this service name. In the right column, you will see a faint gray none. Double-click, and keystroke in your keyboard shortcut that does not conflict with any already set in Finder. Close the System Preferences panel. Your Service's keyboard shortcut is active.

Apr 22, 2018 3:44 PM

Reply Helpful (1)

Apr 22, 2018 4:12 PM in response to VikingOSX In response to VikingOSX

Ok this worked a treat - thank you so much - Champion - may the light of Valhalla shine upon you always!


A couple of changes however:


  1. Delete file destination folder: I would like to be able to set the value of original_delete_folder with a value I input in the process rather than user selected - to make the process faster and less user dependent. For example the trash, or a folder on the desktop "PDFs for deletion" or whatever.
  2. File name selection dialogue - would like to have this appear without the full path - just the file name and extension.


Thank you once again for your help.

Apr 22, 2018 4:12 PM

Reply Helpful
Question marked as Helpful

Apr 23, 2018 7:30 PM in response to mik239 In response to mik239

  1. Delete file destination folder: I would like to be able to set the value of original_delete_folder with a value I input in the process rather than user selected - to make the process faster and less user dependent. For example the trash, or a folder on the desktop "PDFs for deletion" or whatever.
  2. File name selection dialogue - would like to have this appear without the full path - just the file name and extension.


Item 2 is done and tested this morning.


Am I reading item 1 as you want to entirely remove the Ask for Finder Items folder selection entirely? That was the only workable way that I have found to store a destination folder in the original_delete_folder Automator variable at runtime. How do you propose to non-interactively supply the destination folder to the Automator workflow?


I will be out of town this morning and probably into the afternoon, so won't be able to revisit this until later today.

Apr 23, 2018 7:30 PM

Reply Helpful (1)

Apr 23, 2018 3:15 PM in response to VikingOSX In response to VikingOSX

The destination folder for deleted files could either be a fixed folder created on the desktop or even better, checked for, and if not already existing, created by the process. I don't know if either of those two scenarios would be possible - the latter certainly more elegant and fool proof.


Thank you very much for your help!

Apr 23, 2018 3:15 PM

Reply Helpful

Apr 23, 2018 6:02 PM in response to mik239 In response to mik239

Steps 4, 5, and 11 can be eliminated if you want to hardcode a specific folder name in step 13. There, you can set a shell variable to the full path to the static folder location, the script can check for, and create it if necessary, and then the move of the original files would be as shown already.


If you have a specific destination folder and location in mind, I can post the two revisions that you asked for without the preceding actions that are unnecessary. This requires minimal code changes in the last action for implementation.

Apr 23, 2018 6:02 PM

Reply Helpful

Apr 23, 2018 8:05 PM in response to VikingOSX In response to VikingOSX

Oh sorry I just realized you are asking me about two different options there.


I think the best option would be the first one you mentioned as it is failsafe - The folder does not need to be there, it will check whether it is there, and if it is not, create it. In this case it can be installed on any system.


It should default to "~/Desktop/PDFs for deletion" where ~ is the users home folder. That should work universally if I'm not mistaken?


This way also, if they are not using this function they can delete the folder and it will only reappear on their Desktop if they run the function again.

Apr 23, 2018 8:05 PM

Reply Helpful
Question marked as Solved

Apr 24, 2018 4:19 AM in response to mik239 In response to mik239

Here are the two fixes for item 2 Run AppleScript, and the last action, Run Shell Script. The following actions are removed:

  • Item 4 - Ask for Finder Items

    Prompt for output folder name

  • Item 5 - Set Value of Variable

    Saving Original_delete_folder variable

  • Item 11 - Get Value of Variable

    Retrieving Original_delete_folder variable

  • Original_delete_folder variable itself. Right-click on it to remove it from bottom of Automator variable stack.


Replace content of Item 2 Run AppleScript with the following code. It builds a list of human readable filenames for the list selection, and then passes out the full path to the selected PDF combination file name.


use scripting additions


on run argv

if (count of item 1 of argv) ≤ 1 then

display alert "Either 1 or no input files selected." message "Please select at least two files in Finder before running Automator." as critical

error -128

end if

set this_list to {}

set temp to (item 1 of argv)

tell application "Finder"

repeat with anItem in temp

-- human friendly filenames for the list selection

set the end of this_list to name of anItem as text

end repeat


-- list items will appear in the order they were selected in the Finder

set out_name to (choose from list this_list with title "PDF Outfile Name" with prompt "Select the PDF file to base the joined PDF output upon" without multiple selections allowed and empty selection allowed) as text

if out_name is false then return

-- match list selection to original filename, and return POSIX path of designated, combined PDF filename

repeat with elem in temp

if name of elem is equal to out_name then

set out_file to POSIX path of elem as text

end if

end repeat

end tell

-- last file activity in an action is passed to next action

out_file

end run

Replace the contents of the last Run Shell Script action with the following. It hard-codes the destination folder as ~/Desktop/PDFs for deletion as requested, tests for, and creates this folder if it is missing, and then moves the original files into it. The $HOME variable is equivalent to tilde, and I use it to fully expand the path. I have tested this with and without the output folder and it works as expected.


# Following test statement needs expanded, not relative (e.g. tilde) path

OUT_FOLDER="$HOME/Desktop/PDFs for deletion"


# guard conditional test to make folder if missing

if [ ! -d "$OUT_FOLDER" ]; then

mkdir "${OUT_FOLDER}"

fi


# move original PDF files into the output folder

for f in "$@"

do

mv "${f}" "${OUT_FOLDER}"

done


osascript -e "display alert \"Processing Completed\" as informational"

Apr 24, 2018 4:19 AM

Reply Helpful

Apr 25, 2018 3:11 PM in response to VikingOSX In response to VikingOSX

Thank you - amazing! Where can I learn this knowledge for myself? Anyway thank you very much it works brilliantly. 😎


I wonder as well if it would be possible to have it so that the file selection determined the first file to be ordered in the combined PDF so it was the first page(s) in the new document. Either way this works fantastically and I'm very grateful - thanks a million!

Apr 25, 2018 3:11 PM

Reply Helpful

Apr 25, 2018 4:53 PM in response to mik239 In response to mik239

Automator is by default, passing the sort ascending order of Finder selected files into the workflow. You cannot alter this Automator behavior. AppleScript's Choose files command, and Automator's Ask for Finder Items also behave this way. Again, no override. This circumvents any controlled selection order over sort order into the workflow. This effects the presentation order in the list selection too.


Yesterday, I finished another automator workflow that required selection order for correct results. This required an initial, Run Shell Script action with Python/Objective-C to open a custom NSOpenPanel that permitted selection order without sort. This would allow the first selected filename to be first in the list selection.


If you want to dip your toe into learning Automator and AppleScript, you can start at macosxautomation. MacScripter is a good source of, “I have this problem, how do I solve it” questions. Some of the coded solutions on MacScripter are sufficiently old and do not work correctly with current releases of AppleScript. Just keep that in mind. There is also stack overflow, and Apple StackExchange.


The default Terminal shell is Bash, and Apple has frozen it at v3.2.5 while the current release is at 4.4. There are differences in syntax between the old and new versions. Here is a list of rated online Bash tutorials. Any of the first three should get you grounded in Bash.

Apr 25, 2018 4:53 PM

Reply Helpful
User profile for user: mik239

Question: Automator - Combine PDFs, name result correctly