I am newer than new to this, i needed to see if someone could help me with a droplet
to take a predetermined prefix and place it as a suffix before the file extension.
FC_BC_1234.pdf to become 1234_FC_BC.pdf
it seems so easy but like i stated, newer than new…
The following script should do what you want. To test, save it to the desktop as an application and then drag one or more files from the Finder to the desktop icon. For regular use, with recent versions of macOS, you will need to set permissions in Security & Privacy in System Preferences.
This script assumes the file prefix is the first five characters of the current file name. If that’s not the case, the script needs to be revised. Also, it’s probably a good idea to add some error correction just to insure the names of the dropped have the correct name format. This could be as simple as verifying that text 6 of fileName is equal to “_”.
on open droppedFiles
set ATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":", ".pdf"}
repeat with aFile in droppedFiles
set aFile to aFile as text
set fileName to text item -2 of aFile
set {filePrefix, fileBase} to {text 1 thru 5, text 7 thru -1} of fileName
set newFileName to fileBase & "_" & filePrefix & ".pdf"
tell application "Finder" to set name of file aFile to newFileName
end repeat
set AppleScript's text item delimiters to ATID
end open
It works Perfectly…
Thank you so much, you guys make this look so easy. I love that it is easy to read, this will help me learn and hopefully get things done on my own. I really appreciate it.
how would one go about making options either with a dialog box of choices or automatically…
examples of files would be
FC_BC_1234.pdf to 1234_FC_BC.pdf
Case_1234.pdf to 1234_Case.pdf
Jacket_1234.pdf to 1234_Jacket.pdf
I understand the if else then, but i am missing something important.
on open droppedFiles
set ATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":", ".pdf"}
tell application "Finder"
display dialog "Is this a... " buttons {"Cover", "Case", "Jacket"} default button "Cover"
repeat with aFile in droppedFiles
set aFile to aFile as text
set fileName to text item -2 of aFile
if result = {button returned:"Cover"} then
set {filePrefix, fileBase} to {text 1 thru 5, text 7 thru -1} of fileName
else if result = {button returned:"Case"} then
set {filePrefix, fileBase} to {text 1 thru 4, text 6 thru -1} of fileName
else if result = {button returned:"Jacket"} then
set {filePrefix, fileBase} to {text 1 thru 6, text 8 thru -1} of fileName
end if
set newFileName to fileBase & "_" & filePrefix & ".pdf"
tell application "Finder" to set name of file aFile to newFileName
end repeat
set AppleScript's text item delimiters to ATID
end tell
end open
did i place something incorrectly?
the messed up part is that the FC_BC_ has 2 “_” in the file name.
there is always a 13 digit number #.pdf, would that be easier to use as a fileBase?
kempederson, The following should work with file names with both single and multiple underscores. I tested it with your examples without issue but let me know if something doesn’t work:
on open droppedFiles
set ATID to AppleScript's text item delimiters
repeat with aFile in droppedFiles
set aFile to aFile as text
set AppleScript's text item delimiters to {":", ".pdf"}
set fileName to text item -2 of aFile
set AppleScript's text item delimiters to {"_"}
set filePrefix to text items 1 thru -2 of fileName as text
set fileBase to text item -1 of fileName
set newFileName to fileBase & "_" & filePrefix & ".pdf"
tell application "Finder" to set name of file aFile to newFileName
end repeat
set AppleScript's text item delimiters to ATID
end open
If you prefer the approach taken in your script, you could use the following. I omitted the dialog because I saw no reason to use one.
on open droppedFiles
set ATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":", ".pdf"}
repeat with aFile in droppedFiles
set aFile to aFile as text
set fileName to text item -2 of aFile
if fileName begins with "FC_BC_" then
set {filePrefix, fileBase} to {text 1 thru 5, text 7 thru -1} of fileName
else if fileName begins with "Case_" then
set {filePrefix, fileBase} to {text 1 thru 4, text 6 thru -1} of fileName
else if fileName begins with "Jacket_" then
set {filePrefix, fileBase} to {text 1 thru 6, text 8 thru -1} of fileName
else
display alert quote & fileName & quote & " cannot be renamed."
error number -128
end if
set newFileName to fileBase & "_" & filePrefix & ".pdf"
tell application "Finder" to set name of file aFile to newFileName
end repeat
set AppleScript's text item delimiters to ATID
end open
BTW, I use the Finder in my scripts to rename the files. If a large number of files are being renamed, you may want to use System Events instead. To try this out, just change “Finder” in my scripts to “System Events”.
AWWHAA, i see, this is an incredible help.
i thank you greatly. it is just what i was looking for.