Finder Rename Script

I’m having trouble with a Finder script I’m trying to write, and I thought it would be simple. We’re getting XML files that are named incorrectly, and they have to be formatted correctly before we run them through a second script. Basically, characters 3 & 4 need to replace characters 20 & 21 (or insert character 4 as a new character when there is no character 21).

Before
TX014EE_Gr04WRIT_Fm1.xml
TX024EE_Gr04WRIT_Fm4.xml
TX034EE_Gr04WRIT_Fm6.xml
TX044EE_Gr04WRIT_Fm8.xml

After
TX014EE_Gr04WRIT_Fm01.xml
TX024EE_Gr04WRIT_Fm02.xml
TX034EE_Gr04WRIT_Fm03.xml
TX044EE_Gr04WRIT_Fm04.xml

Script so far:

tell application "Finder"
	activate
	set theFolder to choose folder with prompt "Select a folder of XML files to rename"
	set theFiles to every file of theFolder
	repeat with oneFile in theFiles
		set fileName to name of oneFile
		activate
		
		set oldFileName to name of oneFile as string
		
		set char3 to character 3 of oldFileName as string
		set char4 to character 4 of oldFileName as string
		set extension to name extension of oneFile
		set newName to ((characters 1 thru 19 of oldFileName) as string) & (char3 as text) & (char4 as text) & ("." as text) & (extension) as string
		set name of file (oneFile) to newName --fails here
		
	end repeat
end tell

tell application "Finder"
	activate
	display dialog "Done." buttons {"OK"} default button 1 with icon 1
end tell

My predecessor pointed out that I’d forgotten to specify the path along with the file name… :rolleyes:

set thePath to (theFolder & fileName) as string
set name of file (thePath) to newName

Hello.

It’s a stupid thing to write of course, but if nobody haven’t helped you when I wake up, I’ll give you a hand with it. :wink:

Hi.

oneFile is already a file, so you shouldn’t put ˜file’ in front of it.

An easy way to derive newName from oldFileName, assuming the name extension’s always a dot and three other characters, is:

tell oldFileName to set newName to text 1 thru 19 & text 3 thru 4 & text -4 thru -1

Hello.

Please check if this works for you. :slight_smile:

on run
	global lastLoc
	local appName, theFolder
	testForDefaultLocation()
	-- We choose the folder
	tell application (path to frontmost application as text)
		set appName to name of it
		tell me to set theFolder to (choose folder with prompt "Select a folder of XML files to rename" default location lastLoc)
	end tell
	
	tell application "Finder"
		set lastLoc to container of theFolder as alias
		-- we check to see if there where any files that 
		-- are eligible for renaming... and renames them.
		local theFiles
		try
			set theFiles to (every file of theFolder) as alias list
		on error
			display alert "There were no files in " & (theFolder as text) & "."
			tell me to tell application appName to activate
			error number -128
		end try
		local i
		set i to 0
		repeat with oneFile in theFiles
			-- You'll change the contents of the handler
			-- to make it do something else.
			set i to i + (my renameFile(oneFile))
		end repeat
	end tell
	-- okay: Time to sum it up:
	tell application appName
		local btnChoice
		set btnChoice to button returned of (display dialog "Done. Renamed " & i & " files" buttons {"OK", "Show"} default button 1 with icon 1)
		if btnChoice = "Show" then tell application "Finder"
			open theFolder
			activate
		end tell
	end tell
end run

on renameFile(oneFile)
	-- This is the handler to rewrite to make to fit your needs
	local i
	set i to 0
	tell application "Finder" to tell file oneFile
		local oldNm, nmExt, eligible
		
		set {oldNm, nmExt} to {(name of it), (name extension of it)}
		if (length of oldNm) ≥ 19 and nmExt is "xml" then
			-- something may be eligible for renaming here.
			tell oldNm
				if (length of it) ≥ 21 and (text 3 thru 4 of it) = (text 20 thru 21 of it) then
					-- unless of course it follows the naming convention.
					set eligible to false
				else
					set eligible to true
				end if
			end tell
			if eligible then
				set name of it to (text 1 thru 19 of oldNm & text 3 thru 4 of oldNm & "." & nmExt)
				set i to 1
			end if
		end if
	end tell
	return i
end renameFile


on testForDefaultLocation()
	-- This handler tests and sets a default location 
	-- if one is missing.
	global lastLoc
	local a
	try
		set a to lastLoc
	on error
		set lastLoc to (path to desktop)
	end try
	tell application "System Events" to if not (exists folder (lastLoc as text)) then set lastLoc to (path to desktop)
	
end testForDefaultLocation

Hello.

I realized I could use one for myself now and then, so I rewrote it so it be easy to change the namirng rule, having factored that out in a handler. It also nags me if I have to drill down, searching for another folder, so now it saves the parent folder of the last folder you visited as the new default location. It sets back that folder to the desktop folder, should the last location folder have ceased to exist in between runs.