I decided to add some elaborate tests in the Run Shell Script phase of the revised workflow. Here is what it does:
- I have incorporated the customer reference number Finder prompt in the Run Shell Script action.
- Will only accept 4-digit numbers and will continue to prompt until this condition is met.
- Uses an array containing the CAMA, CAMB, and CAMC folder names
- If one just clicks the Choose button without explicit folder selection, or a first folder name that is not in that array, the script throws an error dialog and quits.
- One can select any or all of the CAMA, CAMB, or CAMC folders and those selected will be processed.
- The array of these folder names may need to be changed if different named folders are expected.
- I have added a validation so that if one attempts to process a folder more than once, it will check if the filenames in that folder have been processed and if not it renames them.
- There is no logic to rename added files after initial folder processing and resume the numeric suffix sequence.
- The Run Shell Script uses Zsh and its Pass input selector is set to as arguments so the selected folders are passed into the action on its virtual command line which uses the "${@}" to represent the array of those folder paths.
- When processing is complete, the Run Shell Script will use AppleScript to throw a dialog to that effect.
The Automator actions used in order are:
- Files & Folders Library: Ask for Finder Items
- Utilities Library: Run Shell Script
When you set the Run Shell Script action to Shell: /bin/zsh and Pass input: as arguments, the contents of the empty action will change to show a simple for loop. Select and remove that as you will want to paste the following code instead:
: <<"COMMENT"
Prompt the user for the 4-digit customer reference number using an AppleScript function. Then check if between 1 and 3 folders were
selected from the preceding Ask for Finder Items action. Fail if user cancels, or more than 3 are selected.
The folder names are on the arguments list ($@) provided in the first for loop, and we process each file in that folder, incrementing
its zero-padded generation number with each file. We then rename the existing file to the CFCXXXX_FOLDERNAME_nnnn.ext.
The Zsh shell's modifiers (e.g. h:r:t:e) are documented in the zshexpn man page, or in the Terminal by running the following command sequence to list just the modifier section:
mandoc -Tutf8 -man $(man -w zshexpn) | col -b | awk '/^[ \t]+Modifier/,/^PROCESS SUBSTITUTION/ {print}' | more
Tested: macOS Sequoia v15.0.1, Zsh 5.9
VikingOSX, 2024-10-20, Apple Support Communities, No warranties expressed or implied.
COMMENT
# initialize a 4-digit, zero-padded generation counter (e.g. 0001)
typeset -Z4 gen=1
typeset -a allowed_folders=( CAMA CAMB CAMC )
# prompt for customer reference number
function cust_prompt () {
/usr/bin/osascript <<AS
use scripting additions
-- continue repeat loop until 4-digit number string is entered.
repeat
set ans to text returned of (display dialog "Enter 4-digit string of customer number: " default answer "")
try
set foo to ans as number
if ((count of ans) = 4) then exit repeat
end try
end repeat
return ans
AS
}
function fatal_error () {
/usr/bin/osascript <<AS
use scripting additions
display dialog "${1}" with title "Automator Processing Error"
return
AS
}
# Check if the first folder is one of the approved folders or if the user just pressed choose
# for instance, avoid changing every file on the Desktop.
(( ${allowed_folders[(Ie)${1:a:t}]} )) || { $(fatal_error "Not one of the approved folders."); exit 0}
[[ $# -ge 1 && $# -le 3 ]] || { $(fatal_error "Must pick between 1 and 3 folders for input… quitting."); exit 0}
# set customer reference number from its prompt
CUSTREF="$(cust_prompt)"
# process folders in order of Finder selection
for f in "${@}";
do
# process files in each folder
for g in ${f}/*(.N);
do
# rename files in folder to new format while borrowing ${f:r:t} the folder name string
# and add on the current file extension ${g:e}.
# if renamed file exists then skip file
[[ -f "${g:h:r}/CFC${CUSTREF}_${f:r:t}_${gen}.${g:e}" ]] && continue
/bin/mv "${g}" "${g:h:r}/CFC${CUSTREF}_${f:r:t}_${gen}.${g:e}"
(( gen++ ))
done
# reset generation counter for next folder
gen=1
done
/usr/bin/osascript -e 'display dialog "Processing complete." with title "Automator Completion Status"'
exit 0
The entire Automator application workflow looks like this: