Rename home directories

I’ve been tasked with fixing a problem someone created with folder naming conventions on sever home directories. Within the home directory sharepoint, student user directories should be named following the convention of XXYZ, where XX is the 2-digit graduation year of the student, Y* is the student’s full last name, and Z* their full first name. The server’s former administrator used the convention X_Z*_Y*, where X is the student’s current grade. I wanted to write a script that would operate on a list of all the folders within the sharepoint, and rename them following our standard.

If I comment out the TRY statements, the script fails with the message that it cannot rename the first folder it finds, though the new folder name it displays in the error message matches the convention I need to use. I figure there’s some boneheaded thing I’ve overlooked here, and would appreciate some more experienced eyeballs. Why isn’t Finder cooperating?

set oldDelims to AppleScript's text item delimiters
try
	set AppleScript's text item delimiters to {"_"}
	tell application "Finder"
		set sourceFolder to "server_hd:students" as alias
		set folderList to list folder sourceFolder without invisibles
		set sourceFolder to sourceFolder as string
		repeat with i from 1 to number of items in folderList
			set thisFolder to item i of folderList
			set stuGrade to the first text item of thisFolder
			if stuGrade = "0" then
				set stuGrade to "11"
			else if stuGrade = "1" then
				set stuGrade to "10"
			else if stuGrade = "2" then
				set stuGrade to "09"
			else if stuGrade = "3" then
				set stuGrade to "08"
			else if stuGrade = "4" then
				set stuGrade to "07"
			else if stuGrade = "5" then
				set stuGrade to "06"
			end if
			set stuFirst to the last text item of thisFolder
			set stuLast to the second text item of thisFolder
			set newName to stuGrade & stuLast & stuFirst as string
			set name of thisFolder to newName
		end repeat
	end tell
on error
	set AppleScript's text item delimiters to oldDelims
end try
set AppleScript's text item delimiters to oldDelims

I believe the actual error is occurring here:

set name of thisFolder to newName

The problem is that thisFolder is just a string. This happens because “list folder” just returns the name of items (strings).

Try something like this instead:

try
	tell application "Finder"
		set sourceFolder to "server_hd:students"
		set folderList to folders of (result as alias) as alias list
		
		set oldDelims to AppleScript's text item delimiters
		set AppleScript's text item delimiters to {"_"}
		
		repeat with thisFolder in folderList
			set oldName to name of thisFolder
			set newName to (last text item of oldName) & (second text item of result)			
			get first text item of oldName
			if result = "0" then
				get "11"
			else if result = "1" then
				get "10"
			else if result = "2" then
				get "09"
			else if result = "3" then
				get "08"
			else if result = "4" then
				get "07"
			else if result = "5" then
				get "06"
			end if
			
			set name of thisFolder to (result & newName)
		end repeat
	end tell
end try
set AppleScript's text item delimiters to oldDelims

Thank you! I made an error in assuming that a list could be an array of anything, but I guess it’s only a list of strings?

A list can contain anything, but the list folder command only returns strings.