Although AppleScript can be coded and saved as a drag and drop solution, its reliability to detect all dropped files is inconsistent. For that reason, I have decided to use an Automator application that does work correctly when one or more files are dropped on it.
The Automator application uses just one action, Run AppleScript and the AppleScript code has changed according to your last functionality request. Now it processes all dropped files on it. If it detects a PDF document, and that PDF document contains a "_common" string, the PDF will receive the current file's parent folder as a prefix, and the common string is replaced by FINAL.
Example for a PDF on the Desktop dropped on the application:
22-M1FTV5-1_common.pdf => Desktop_22-M1FTV5-1_FINAL.pdf
Example for any other file containing "_common":
22-M1FTV5-1_common.txt => 22-M1FTV5-1.txt
Tested on macOS Sonoma 14.0.
- Launch Automator from your Applications folder.
- New Document
- Application
- Choose
- Enter Run AppleScript in the search field.
- Drag and drop the Run AppleScript action to the right onto the larger workflow window
- Select all text in it and hit backspace to remove its content.
- Copy and paste the following AppleScript below into that Run AppleScript action, and click the hammer icon to compile it. The text will change from purple to multiple colors to signify success.
- Save the Automator application to your Desktop with some meaningful name (.e.g. Fix_DocumentNames).
- Quit Automator
Code to copy/paste into the Run AppleScript action:
(*
Drag and drop files onto this application.
PDFs will be renamed to foldername_filename_FINAL.pdf
Other files will have no prefix and any occurrence of "_common"
in the filename will be removed
*)
use scripting additions
property suffix : "FINAL"
on run {input, parameters}
tell application "Finder"
repeat with anItem in input
set nameStr to anItem's name
set nameExt to "." & anItem's name extension
-- no file extension because - 1 removes "." and extension
set baseName to text 1 thru ((offset of "." in nameStr) - 1) of nameStr
ignoring case
if baseName contains "_common" and (name extension of anItem = "pdf") = true then
-- PDF only
set prefix to (container of anItem as text)'s last word
set newName to prefix & "_" & text 1 thru (offset of "_" in baseName) of baseName & suffix & nameExt
else
-- other file types where we strip out any "_common" presence
set newName to text 1 thru ¬
((offset of "_" in baseName) - 1) of baseName & nameExt
end if
end ignoring
log (newName) as text
set name of anItem to newName
end repeat
end tell
return newName
end run
Now that you have an Automator application on your Desktop, you can drag and drop one or more files onto it and it will process them. It is not configured for you to drop folders on it.
The first time you run this, it will throw a dialog asking to control Finder. Click OK (that is important). My name for the Automator application that I tested was Fix_Filenames.

Here is what the Automator appliication contents should look like when you save it.
