Hi,
I have a folder of files with names like “tennessee_08_sci_firstpart.doc”, “tennessee_09_sci_secondpart.doc”. What I need to do, (client request) is set the chapter code “sci” to uppercase “SCI” but leave the rest of the name alone. I needed to write an applescript that would do that. (script library had a change case script, but it only worked on the whole name, while the replace text in filenames script wouldn’t change accept “SCI” as a replacement for “sci”.
I looked around and adapted some script samples (Rosenthal, google) and came up with this:
display dialog "Please enter the lower case chapter code you want to set to upper case:" default answer "" buttons {"Cancel", "OK"} default button 2
set lcString to text returned of result
do shell script "echo " & lcString & " | tr A-Z a-z "
set lcString to result
set ucString to do shell script "echo " & lcString & " | tr a-z A-Z "
set myFolder to (choose folder with prompt "Pick folder of files you want to change:")
set myFiles to list folder myFolder without invisibles
set myFolderString to myFolder as string
set myRealFiles to {}
repeat with i from 1 to (count myFiles)
set myItem to item i of myFiles as string
set myItemInfo to get info for file (myFolderString & myItem)
set myFileNameExt to name extension of myItemInfo
if myFileNameExt = "doc" then
copy myItem to end of myRealFiles
end if
end repeat
repeat with i from 1 to (count of myRealFiles)
set myFile to item i of myRealFiles
set myFilePath to (myFolderString & myFile)
set myFileInfo to info for alias myFilePath
set currentName to name of myFileInfo
if currentName contains lcString then
set AppleScript's text item delimiters to the lcString
set the text_item_list to every text item of the currentName
set AppleScript's text item delimiters to the ucString
set the new_item_name to the text_item_list as string
set AppleScript's text item delimiters to ""
end if
tell application "Finder"
set myFolder to myFolder as text
set originalFile to (myFolder & myFile)
set name of file originalFile to new_item_name
end tell
end repeat
Yes, I can’t believe it worked either!
I’d just like to know if there is a simpler or elegant way to do this. I think I’m probably wasting code lines here. Any advice is greatly appreciated.