Hi,
I need a script that can erase specific groups of characters from file names.
For example, I have a file name that looks like this: ‘C19326.jobSg01SdA-11.eps’. I want to erase the ‘.jobSg0’, the ‘Sd’, and the ‘-11’, so that the file name looks like this: ‘C193261A.eps’.
I’ve tried to modify the ‘replace text in item names’ script, and another script I found on the forum, but I don’t know enough about writing scripts to make it work. Thanks for your help.
Something like this:
set fname to "C19326.jobSg01SdA-11.eps"
--I want to erase the '.jobSg0', the 'Sd', and the '-11', so that the file name looks like this: 'C193261A.eps'.
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set FN to text items of fname
set AppleScript's text item delimiters to "-"
set L to (text item 1 of fname)
set AppleScript's text item delimiters to tid
set newName to item 1 of FN & character -1 of L & "." & item -1 of FN
--> C19326A.eps
The alternative but less general approach is to use offsets:
set fname to "C19326.jobSg01SdA-11.eps"
--I want to erase the '.jobSg0', the 'Sd', and the '-11', so that the file name looks like this: 'C193261A.eps'.
set x to (offset of ".jobSg01Sd" in fname) - 1 -- but this must not change, obviously
set y to (offset of "-" in fname) - 1 -- position of character before the dash
set z to offset of ".eps" in fname -- must also remain constant
tell fname to set newName to (text 1 thru x) & character y & (text z thru -1)
--> C19326A.eps
Thanks, but I see I didn’t clearly communicate what I need. I need to change the names of several files in a specific folder. Also, the first seven characters of the file names are never the same. I modified a script I found in the forum post “Replace text in filenames” posted by grouchyguy. It erases the ‘.jobSg0’ and the ‘Sd’, but I can’t get it to erase the ‘-11’.
tell application “Finder”
activate
try
get (folder of front Finder window) as alias
on error
get (path to desktop)
end try
set fileList to every file of folder (result) as alias list
set ASTID to AppleScript's text item delimiters
try
repeat with thisFile in fileList
set newName to (thisFile's name)
set AppleScript's text item delimiters to ".jobSg0"
set newName to every text item of newName
set AppleScript's text item delimiters to ""
set newName to newName as Unicode text
set AppleScript's text item delimiters to "Sd"
set theExtension to (last text item of newName) as Unicode text
set newName to text items 1 through -2 of newName
set AppleScript's text item delimiters to ""
set newName to newName as Unicode text
set AppleScript's text item delimiters to ""
set name of thisFile to (newName & "" & theExtension)
end repeat
beep
on error errorMsg number errorNum
display dialog "Error (" & errorNum & "):" & return & return & errorMsg buttons "OK" default button 1 with icon caution
end try
set AppleScript's text item delimiters to ASTID
end tell
Using what I proposed earlier, except I removed the bit in your script about choosing the desktop (not a safe move) and instead put in the selected folder. To test this, make a copy of a typical folder, select it in the Finder, and run this script:
tell application "Finder"
set tFolder to (get selection) as alias -- select a valid folder here; there is no checking that the contents are what you want to make changes to.
try
set fileList to files of folder tFolder as alias list
on error -- the line above doesn't work if only one file
set fileList to files of tFolder as alias as list
end try
set ASTID to AppleScript's text item delimiters
try
repeat with thisFile in fileList
set fName to (thisFile's name)
set AppleScript's text item delimiters to "."
set FN to text items of fName
set AppleScript's text item delimiters to "-"
set L to (text item 1 of fName)
set AppleScript's text item delimiters to ASTID
set newName to item 1 of FN & character -1 of L & "." & item -1 of FN
set name of thisFile to newName
end repeat
on error e
display dialog e
end try
set AppleScript's text item delimiters to ASTID
end tell
I tried the latest script you posted. I get this error message: Can’t make {} into type alias.
Disregard the previous post. The script works fine when I follow the instructions. But it still needs a little tweaking.
My original file name was something like: ‘C19326.jobSgO1SdA-11.eps’
I wanted it to be re-named like so: ‘C193261A.eps’
The script re-named the file: ‘C19326A.eps’. I need it to save the numeral between the ‘.jobSg0’ and the ‘Sd’. This numeral varies with each file name. Thanks for your help.
One other thing, the numeral between the ‘.jobSgO’ and the ‘Sd’ is sometimes two digits.
To avoid a lot of back and forth tuning, why not post a representative sample of the names (to which you keep adding "oh, by the way"s). Otherwise I have no way to test a solution.
Here’s a typical list of file names.
P19525.jobSg01SdA-11.eps
P19525.jobSg01SdB-11.eps
P19525.jobSg02SdA-11.eps
P19525.jobSg02SdB-11.eps
P19525.jobSg03SdA-11.eps
P19525.jobSg03SdB-11.eps
P19525.jobSg04SdA-11.eps
P19525.jobSg04SdB-11.eps
P19525.jobSg05SdA-11.eps
P19525.jobSg05SdB-11.eps
P19525.jobSg06SdA-11.eps
P19525.jobSg06SdB-11.eps
P19525.jobSg07SdA-11.eps
P19525.jobSg07SdB-11.eps
P19525.jobSg08SdA-11.eps
P19525.jobSg08SdB-11.eps
P19525.jobSg09SdA-11.eps
P19525.jobSg09SdB-11.eps
P19525.jobSg10SdA-11.eps
P19525.jobSg10SdB-11.eps
I assume “P19525” is not a constant because doesn’t match your earlier examples. Is it always exactly 6 characters followed by a period? Is it always a letter and 5 numbers?
What we’re doing here is pattern matching, and to do that you have to know what’s “pattern” and what’s constant. For other examples, is the name extension always “eps”? Does “Sd” always occur and is the prefix for the middle number always “jobSg” followed by exactly two numerals, one of which can be zero, but never three digits? Are the digits before the final period and after the dash always “11”?
I assume you’ve seen lots of these, so give the “rules” some thought. The job can be done without all that info, but it’s more likely to fail on a special case.
This works for my guesses at the rules. In your first example, you eliminated the zero following “jobSg” if it was a single digit file, but I haven’t done so - I’ve left it as a two-digit number whether it was “01” or “12”
tell application "Finder"
set tFolder to (get selection) as alias
if (kind of tFolder) is not "folder" then
display dialog "Not a Folder"
return
end if
try
set fileList to files of folder tFolder as alias list
on error -- the line above doesn't work if only one file
set fileList to files of tFolder as alias as list
end try
if fileList is {} then
display dialog "An Empty Folder Chosen"
return
end if
set ASTID to AppleScript's text item delimiters
try
repeat with thisFile in fileList
set fName to thisFile's name
set AppleScript's text item delimiters to "." -- first 'chunker'
set {Head, Mid, Tail} to text items of fName -- a list of three chunks given names
set AppleScript's text item delimiters to "Sg"
set MidStart to text item 2 of Mid
set AppleScript's text item delimiters to "Sd"
set Mid to text item 1 of MidStart
set AppleScript's text item delimiters to "-"
set Ltr to last character of text item 1 of MidStart
set newName to Head & Mid & Ltr & "." & Tail
set AppleScript's text item delimiters to ASTID
set name of thisFile to newName
end repeat
on error e
display dialog "Something wrong with a file name" & return & e
end try
set AppleScript's text item delimiters to ASTID
end tell
Thank you so much. This script works perfectly. This will save us a lot of keystrokes.
You’re welcome. If you have to do much of that kind of thing, you should read a tutorial about text item delimiters.