Re: applescript to rename files
Re: applescript to rename files
- Subject: Re: applescript to rename files
- From: Graff <email@hidden>
- Date: Fri, 11 Jun 2004 00:28:12 -0400
Yep, my bad. I think my head was screwed on wrong for the past couple
of days, I created a huge bug in there. The gist of the bug is that I
was copying a list to the end of a list when I should have been
concatenating them with the "&" operator.
This line:
copy (every item of theItem) to the end of theList
Should be this:
set theList to theList & (every item of theItem)
I also cleaned up a problem where I was incorrectly checking if theItem
was a folder. Here's what should be a corrected version:
----
property allowedCharacters :
"1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ$%'-_@~`!{}()#&+,;=[]. "
property replacementCharacter : "!"
tell application "Finder"
try
set theFolder to choose folder with prompt "Select the folder to
clean up illegal Windows file names."
set theList to every item of theFolder
repeat while (length of theList is greater than 0)
set theItem to item 1 of theList
if class of theItem is folder then
set theList to theList & (every item of theItem)
end if
set theList to the rest of theList
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 & "\" can not have its
name cleaned because the cleaned name is already in use."
end if
end if
end repeat
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 in allowedCharacters) then
set newName to newName & character i of theName
else
set newName to newName & replacementCharacter
end if
end repeat
return newName
end CleanName
----
- Ken
On Jun 10, 2004, at 11:07 PM, bob bader wrote:
Thanks for the script. I tested it and it did not go into sub
folders. any ideas?
Thanks
Bob
_______________________________________________
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.