replaciing characters in file and folder names

Howdy–

I offered up this script to someone yesterday and it seems that they
have found a bug that I never encountered before (but I can duplicate
it) and i can’t seem to figure out why. The code is below my
signature, but here’s what it does and what the problem is:

Basically, it replaces characters of my choosing out of the names of
files and folders. I give it a list of characters to look for and what
to replace them with. It also spits out a couple of reports based upon
what it finds and does. The problem is that when it comes across a
name with an accented character, it tells me the file already exists,
even though all other punctuation works fine.

Any ideas? TIA!

Philip Regan


property searchStrings : {"é", "+", "#", "!", "@", "$", "%", "^", "*", "(", ")", "-", "=", "[", "]", "<", ">", "?", " ", ";", "/", "\", """, ",", "{", "}", "'", "."}
property replacementStrings : {"eE", "p", "s", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "."}

global search_string
global replacement_string
global periodFlag

set periodFlag to false

tell application "Finder"
	set source_folder to choose folder with prompt "FindReplaceIllegalCharacters: Please select directory."
	
	set startTime to (current date)
	
	repeat with x from 1 to (get count of items in searchStrings)
		if x = 28 then
			set search_string to (item x of searchStrings)
			my countPeriods(source_folder)
		else
			set search_string to (item x of searchStrings)
			set replacement_string to (item x of replacementStrings)
			my createList(source_folder)
		end if
	end repeat
	
	set endTime to (current date)
	set runTime to (endTime - startTime)
	set runTimeText to ("RUNTIME: " & (runTime as text) & " seconds.")
	set changedFileLog to (((path to desktop folder) as text) & "ChangedFileLog.txt")
	my write_to_file(runTimeText, changedFileLog, true)
	
	if periodFlag is true then
		display dialog "Some files had extra periods in them. Please check the log and fix the filenames."
	end if
end tell

on countPeriods(item_list) --currently lists all files with a period. Need to coerce filename into a list, and/or view each *character* in turn and see if it's a period.
	set periodCount to 0
	set the the_items to list folder item_list without invisibles
	set item_list to item_list as string
	repeat with i from 1 to number of items in the the_items
		set the_item to item i of the the_items
		set the_item to (item_list & the_item) as alias
		set this_info to info for the_item
		set item_name to (name of this_info) as string
		if item_name contains search_string then
			set text item delimiters to search_string
			set periodCount to (count text items of item_name) - 1
			if periodCount > 1 then
				set periodFlag to true
				set periodFilename to ((the_item as string) & return)
				set periodFilenameLog to (((path to desktop folder) as text) & "FilenamesWithExtraPeriods.txt")
				my write_to_file(periodFilename, periodFilenameLog, true)
			end if
		end if
		if folder of this_info is true then
			my countPeriods(the_item)
		end if
	end repeat
end countPeriods

on createList(item_list)
	set the the_items to list folder item_list without invisibles
	set item_list to item_list as string
	repeat with i from 1 to number of items in the the_items
		set the_item to item i of the the_items
		set the_item to (item_list & the_item) as alias
		set this_info to info for the_item
		set the item_name to the name of this_info
		if item_name contains search_string then
			set changedFile to ((the_item as string) & return)
			set changedFileLog to (((path to desktop folder) as text) & "ChangedFileLog.txt")
			my write_to_file(changedFile, changedFileLog, true)
			my replaceString(item_name, the_item, search_string, replacement_string)
		end if
		if folder of this_info is true then
			my createList(the_item)
		end if
	end repeat
end createList

on replaceString(current_name, the_item, search_string, replacement_string)
	set AppleScript's text item delimiters to search_string
	set the text_item_list to every text item of the current_name
	set AppleScript's text item delimiters to replacement_string
	set the new_item_name to the text_item_list as string
	set AppleScript's text item delimiters to ""
	my set_item_name(the_item, new_item_name)
end replaceString

on set_item_name(this_item, new_item_name)
	tell application "Finder"
		--activate
		set the parent_container_path to (the container of this_item) as text
		if not (exists item (the parent_container_path & new_item_name)) then
			try
				set the name of this_item to new_item_name
			on error the error_message number the error_number
				if the error_number is -59 then
					set the error_message to "This name contains improper characters, such as a colon (:)."
				else --the suggested name is too long
					set the error_message to error_message -- "The name is more than 31 characters long."
				end if
				--beep
				tell me to display dialog the error_message default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
				copy the result as list to {new_item_name, button_pressed}
				if the button_pressed is "Skip" then return 0
				my set_item_name(this_item, new_item_name)
			end try
		else --the name already exists
			--beep
			tell me to display dialog "This name is already taken, please rename." default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
			copy the result as list to {new_item_name, button_pressed}
			if the button_pressed is "Skip" then return 0
			my set_item_name(this_item, new_item_name)
		end if
	end tell
end set_item_name

on write_to_file(this_data, target_file, append_data)
	try
		set the target_file to the target_file as text
		set the open_target_file to open for access file target_file with write permission
		if append_data is false then set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof
		close access the open_target_file
		return true
	on error
		try
			close access file target_file
		end try
		return false
	end try
end write_to_file