Replace text in filenames

Hi folks… I would love a little help here if you all can give it…

I want to create a script that will simply do a batch of very specific text replacements inside mp3 filenames in the front Finder window; in fact these are the actions I want the script to do:

Change “.01.” to “-01-”
Change “.02.” to “-02-”
Change “.03.” to “-03-”
Change “.04.” to “-04-”
Change “.05.” to “-05-”
Change “.06.” to “-06-”
Change “.07.” to “-07-”
Change “.08.” to “-08-”
Change “.09.” to “-09-”
Change “.10.” to “-10-”
Change “.11.” to “-11-”
Change “.12.” to “-12-”
and
Change “_” to " "

In short, I’m replacing dots with hyphens, and then replace underscores with spaces.

I could just have the script replace all dots with hyphens but then “.mp3” gets changed to “-mp3”, which I don’t want to happen.

Result: all the files in a folder that were named like:

“1946.12.13_The_File_Name.mp3”

become named

“1946-12-13 The File Name.mp3”

I don’t need any user input dialogs or anything, just a simple script that will do these operations on a batch of files and nothing else.

I’m trying to modify Apple’s “Replace Text In Item Names” in the Script Editor, but I’m not experienced in scripting, so I’m having a lot of trouble…I apologize for my greenness and my (likely) inefficiency in how I’m going about it.

Any help you all can give would be greatly appreciated…

thanks!
geoff

Try this:

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 "_"
			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 "."
			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