property illegalCharacters : {"\\", "/", ":", "*", "?", "\"", "<", ">", "|", "#", "%", "$", ";", "?"} ¬ -- this is the list of characters to substitute from inside the filename property substituteCharacter : "_" -- this is the character to substitute for the illegal characters property illegalEnds : {" ", "_", "."} -- this is the list of characters to remove from the start or end of the file property renameCount : 0 -- count of how many files and folders were renamed property checkCount : 0 -- count of how many files and folders were checked on ProcessAFolder(aFolder) tell application "Finder" set fileList to every file of aFolder -- grab the files from inside the folder set folderList to every folder of aFolder -- grab the folders from inside the folder end tell my ProcessFiles(fileList) -- process the files repeat with bFolder in folderList -- loop through the folders my ProcessAFolder(bFolder) -- recursively go through the folders my RenameItem(bFolder) -- after checking the folder contents, rename it as well end repeat end ProcessAFolder on open of dropList -- operate on files and folders dropped onto the application repeat with theItem in dropList -- iterate through the list and call the processing routine tell application "Finder" if last character of (theItem as string) is ":" then -- if the item is a folder my ProcessAFolder(theItem) -- process the folder my RenameItem(theItem) -- then rename the folder else my RenameItem(theItem) -- otherwise rename the file end if end tell end repeat tell application "Finder" -- make sure the dialog is visible if renameCount is 0 then -- if nothing was renamed display dialog "" & checkCount & " items were checked. Nothing was renamed." buttons ("Ok") default button ¬ "Ok" -- alert the user that nothing was renamed else display dialog "" & checkCount & " items were checked. " & renameCount & ¬ " item(s) were renamed." buttons ("Ok") default button "Ok" -- otherwise alert the user how many items were renamed end if end tell end open on run display dialog "Drag and Drop folders and/or files to rename onto this application" buttons ("Ok") default button ¬ "Ok" -- this is a drag drop, not run end run on ProcessFiles(fileList) repeat with aFile in fileList -- loop through the file list RenameItem(aFile) -- rename the files end repeat end ProcessFiles on RenameItem(theItem) set checkCount to checkCount + 1 -- increment the check counter tell application "Finder" set theName to name of theItem -- grab just the name of the item set newName1 to my fixCharacters(theName) -- check for illegal characters throughout the item set newname2 to my fixEnds(newName1) -- check for illegal characters at the start and end of the item if theName ? newname2 then -- if the name has been changed if exists item (((container of theItem) as string) & newname2) then -- check if the new item name already exists if text -5 thru -1 of newname2 contains "." then -- check for suffix on item name if text -4 thru -4 of newname2 is "." then -- if is of the form image.jpg set suffix to text -4 thru -1 of newname2 -- grab the suffix with the period set newname2 to text 1 thru -5 of newname2 -- grab the first part of the item name else if text -5 thru -5 of newname2 is "." then -- if is of the form image.jpeg set suffix to text -5 thru -1 of newname2 -- grab the suffix with the period set newname2 to text 1 thru -6 of newname2 -- grab the first part of the item name else if text -3 thru -3 of newname2 is "." then -- if is of the form image.ps set suffix to text -3 thru -1 of newname2 -- grab the suffix with the period set newname2 to text 1 thru -4 of newname2 -- grab the first part of the item name else -- if is of the form image.1 set suffix to text -2 thru -1 of newname2 --grab the suffix with the period set newname2 to text 1 thru -3 of newname2 -- grab the first part of the item name end if else set suffix to "" -- otherwise there is no suffix end if set done to false -- set up our loop exit set x to 1 -- start our counter repeat until done if not (exists item (((container of theItem) as string) & newname2 & x & suffix)) then -- check if the renamed file exists already set newname2 to newname2 & x & suffix -- add the counter to the filename and add the suffix back on set done to true -- exit the loop else set x to x + 1 -- otherwise increment our counter end if if x = 10 then -- if we have reached 10, time to bail anyway set newname2 to newname2 & x & suffix -- add the counter to the filename and add the suffix back on set done to true -- exit the loop end if end repeat end if try -- setup error handling set name of theItem to newname2 -- try and rename the file to the new name set renameCount to renameCount + 1 -- increment the rename counter if the rename was successful on error errMsg number errnum -- if there is an error set dialogReturn to display dialog "An Error occurred renaming the following item: " & return & (theItem as string) ¬ & return & "to " & newname2 & "." & return & return & "Please go to that file and manually rename it!" & return ¬ & return & errMsg & return & errnum buttons {"Continue?"} default button "Continue?" -- throw an error dialog and continue end try end if end tell end RenameItem on fixEnds(theName) set theName to my fixFirst(theName) -- fix the start of the name set theName to my fixLast(theName) -- fix the end of the name return theName -- pass back the (un)changed name end fixEnds on fixCharacters(theName) repeat with x from 1 to (count illegalCharacters) -- loop through the list of illegal characters if theName contains (item x of illegalCharacters) then -- if this character is in the filename set oldDelims to AppleScript's text item delimiters -- store the current delimiters set AppleScript's text item delimiters to (item x of illegalCharacters) -- set the delimiter to the illegal character set theTextItems to text items of theName -- extract the text without the illegal character as a list of strings set AppleScript's text item delimiters to substituteCharacter -- set the delimiter to be the substitute character set theName to theTextItems as text -- change the list of strings back into a single string set AppleScript's text item delimiters to oldDelims -- change the delimiters back to what they were originally end if end repeat return theName -- pass back the (un)changed name end fixCharacters on fixFirst(theName) set theFirst to first character of theName -- check the first character of the item name if theFirst is in illegalEnds then -- if the first character is in the illegal list set theName to text 2 thru -1 of theName -- remove the first character of the name set theName to my fixFirst(theName) -- make sure we haven't uncovered any more illegals end if return theName -- pass back the (un)changed name end fixFirst on fixLast(theName) set theLast to last character of theName -- check the last character of the item name if theLast is in illegalEnds then -- if the last character is in the illegal list set theName to text 1 thru -2 of theName -- remove the last character of the name set theName to my fixLast(theName) -- make sure we haven't uncovered any more illegals end if return theName -- pass back the (un)changed name end fixLast