Checking name length and removing illegal characters

I’ve been able to use various pieces of scripts that I found on this site to develop a script that I use to:

  1. check a file name to a specified length, and if it exceeds 25 characters, the user is asked to rename the file… and
  2. replace all illegal characters in the file names with an underscore

I’m still a beginner on applescript, and like I said…this is built together from various scripts that other people have made

My question to more advanded applescripters is how I can now turn this script into a folder action…as I would like these 2 checks to take place every time I drop a new file into a folder.

The script I now have is:

global listFiles
set m to choose folder
set srcPath to (m as string)
set listFiles to {}
global delim

set AppleScript’s text item delimiters to “_”

–Direct User To Choose A Folder–
tell me to AddFolder(srcPath)
set s to “”

–Count Files In Selection, And Display # To Users–
set cnt to display dialog “Modify the names of " & (count of listFiles) & " files?” & return & return & �
“This Script will help you to constrain your file names to the 25 character maximum” & return & �
“As well as clean out any illegal characters”

if the button returned of cnt is “OK” then

--Call To Check Name Length For Max Allowed, And If Too Long Ask User To Rename--
tell application "Finder"
	my setName(listFiles)
end tell

--Clean Any Illegal Characters Out Of Name--
tell application "Finder"
	repeat with i from 1 to count of listFiles
		set elem to (item i of listFiles) as string
		set fileItem to alias elem
		set strName to name of fileItem
		tell me to set strName to FixName(strName, " ")
		tell me to set strName to FixName(strName, "� �")
		tell me to set strName to FixName(strName, "-")
		tell me to set strName to FixName(strName, "_")
		tell me to set strName to FixName(strName, "__")
		tell me to set strName to FixName(strName, "~")
		tell me to set strName to FixName(strName, "`")
		tell me to set strName to FixName(strName, "!")
		tell me to set strName to FixName(strName, "@")
		tell me to set strName to FixName(strName, "#")
		tell me to set strName to FixName(strName, "$")
		tell me to set strName to FixName(strName, "%")
		tell me to set strName to FixName(strName, "^")
		tell me to set strName to FixName(strName, "&")
		tell me to set strName to FixName(strName, "*")
		tell me to set strName to FixName(strName, "(")
		tell me to set strName to FixName(strName, ")")
		tell me to set strName to FixName(strName, "=")
		tell me to set strName to FixName(strName, "+")
		tell me to set strName to FixName(strName, "{")
		tell me to set strName to FixName(strName, "}")
		tell me to set strName to FixName(strName, "[")
		tell me to set strName to FixName(strName, "]")
		tell me to set strName to FixName(strName, "|")
		tell me to set strName to FixName(strName, "\")
		tell me to set strName to FixName(strName, "/")
		tell me to set strName to FixName(strName, "'")
		tell me to set strName to FixName(strName, ",")
		tell me to set strName to FixName(strName, """)
		set name of fileItem to strName
	end repeat
end tell

end if

on FixName(currentName, fixString)
set AppleScript’s text item delimiters to fixString
set listName to every text item of currentName
set AppleScript’s text item delimiters to “_”
return (listName as string)
end FixName

on AddFolder(pt)
tell application “Finder”
set topFolder to alias pt
set srcContents to every item of topFolder
repeat with i from 1 to count of srcContents
set elem to item i of srcContents
if last character of (elem as string) is “:” then
copy elem to the end of listFiles
tell me to AddFolder(elem as string)
else
tell me to AddFile(elem as string)
end if
end repeat
end tell
end AddFolder

on AddFile(pt)
set theFile to alias pt
copy theFile to the end of listFiles
end AddFile

–Direct Controller for Name Length–
on setName(fileList)
tell application “Finder”
repeat with thisItem in fileList
set item_name to name of thisItem
set name_length to count item_name
if kind of thisItem is not “Folder” then
if name_length > 25 then
set answerOne to text returned of (display dialog �
“This file’s name has too many characters. It has " & name_length �
& " characters, but the limit is 25 total. Please enter a new name�
or trim this name down.” buttons {“Proceed”} default button 1 default answer item_name)
set name of thisItem to answerOne
end if
end if
my setName(thisItem)
end repeat
end tell
end setName

You’re adding much more code than you need. Try adding a repeat loop. With a loop, you could change:

to

set replace_strings to {" ", "    ", "-", "_", "__", "~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "=", "+", "{", "}", "[", "]", "|", "\", "/", "'", ",", """}
repeat with this_replace_string in replace_strings
    set strName to my FixName(strName, (contents of this_replace_string))
end repeat

Jon