Re: Replace characters in filename
Re: Replace characters in filename
- Subject: Re: Replace characters in filename
- From: Graff <email@hidden>
- Date: Sat, 12 Jun 2004 16:51:30 -0400
On Jun 12, 2004, at 3:49 PM, Rick Davis wrote:
How would I find certain characters (any of a list of 13) in a
filename and replace them with one common character?
Heh, I just posted this yesterday. It's the same solution for cleaning
up files so they don't have any non-Windows characters, I just flipped
the logic in the CleanNames handler. Just change the
disallowedCharacters property to contain the characters you want to
strip and the replacementCharacter property to the character you want
to replace them with. If you set the replacementCharacter property to
"" then the script will just strip out the disallowed characters.
----
property disallowedCharacters : "1234567890ABCD"
property replacementCharacter : "!"
tell application "Finder"
try
set theItem to choose file with prompt "Select the file to clean up
its name."
set oldName to name of theItem
set newName to my CleanName(oldName)
if (newName is not equal to oldName) then
set containerPath to container of theItem as string
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
if (length of text items of newName > 1) then
set theExt to "." & text item -1 of newName
else
set theExt to ""
end if
set AppleScript's text item delimiters to oldDelims
set strippedName to text 1 thru (-1 * ((length of theExt) + 1))
of newName
set i to 0
repeat while ((item (containerPath & newName) exists) and (i <
1000))
set i to i + 1
set newName to strippedName & i & theExt
end repeat
if (i < 1000) then
set (name of theItem) to newName
else
display dialog "The file \"" & theItem & "\"'s cleaned name is
already in use."
end if
end if
on error
display dialog "An error occured."
end try
end tell
on CleanName(theName)
set newName to ""
repeat with i from 1 to length of theName
if ((character i of theName) is not in disallowedCharacters) then
set newName to newName & character i of theName
else
set newName to newName & replacementCharacter
end if
end repeat
return newName
end CleanName
----
Mind the line wraps, some long lines might get wrapped. If you want I
can mail this script directly to you so that the line wraps don't trip
you up.
I made the replacement character for any illegal characters to be an
exclamation point (!). If you want it to be something different then
change the string of the property "replacementCharacter" to whatever
you want it to be. It can even be blank so that the illegal characters
will just be stripped and not replaced.
- Ken
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.