Swap first and last name in finder files

This is probably posted on here somewhere but I can’t seem to find it. Sorry if so and please point me in the right direction.

I have tiff images that have all been named first and then last name. I need to switch these. Since I have over 100 to do, I wondered if there was a script to help me.

Thanks for any help you can offer.

cindy in indy

Open a Finder window and select the files you want to do this to… then run this…

tell application "Finder"
	set theFiles to the selection
	repeat with aFile in theFiles
		set fName to name of aFile
		set fExt to name extension of aFile
		
		-- get the name without the extension
		if fExt is not "" and fExt is not missing value then
			set fName to text 1 thru ((length of fName) - (length of fExt) - 1) of fName
		end if
		
		-- put the name into a list so we can reverse it
		-- if the count is only 1 then there's nothing to reverse
		set fNameList to words of fName
		if (count of fNameList) is greater than 1 then
			-- reverse the fNameList
			set newFName to ""
			repeat with i from (count of fNameList) to 1 by -1
				set newFName to newFName & item i of fNameList & space
			end repeat
			
			-- remove the ending space character
			set newFName to text 1 thru -2 of newFName
			
			-- add the extension back on to the reversed name
			if fExt is not "" and fExt is not missing value then
				set finalFName to newFName & "." & fExt
			else
				set finalFName to newFName
			end if
			
			-- change the name of the file
			-- make sure there isn't a file already with the new name
			set theFolder to (container of aFile) as text
			set newPath to theFolder & finalFName
			if not (exists item newPath) then
				set name of aFile to finalFName
			end if
		end if
	end repeat
end tell

omg.
i love you.

it works perfectly!

thank you thank you thank you

You’re welcome. :wink: