Noob question

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!:stuck_out_tongue:

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.

Hi,

try this


set lcString to text returned of (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 ucString to do shell script "echo " & quoted form of 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
repeat with oneFile in myFiles
	set {name:Nm, name extension:Ex} to info for alias ((myFolder as text) & oneFile)
	if Ex is "doc" and Nm contains lcString then
		set {TID, text item delimiters} to {text item delimiters, lcString}
		set Nm to text items of Nm
		set text item delimiters to ucString
		set Nm to Nm as text
		set text item delimiters to TID
		tell application "Finder" to set name of file ((myFolder as text) & oneFile) to Nm
	end if
end repeat

Thanks, Stefan. That is much cleaner and simpler.

Someday, I will be able to call myself a scripter.