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