String replace.

What is wrong with my code. Why does it not replace the string?

display dialog "Divider?" buttons {"\\", ">", ":"} default button 1
set divider to the button returned of the result

tell application "Finder"
	set filepath to (get selection as text)
	set filepath to replace_chars(filepath, ":", divider)
	display dialog filepath
	set the clipboard to filepath
end tell

on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

Your code is VERY close to working. You only need to replace this line:

set filepath to replace_chars(filepath, “:”, divider)

with:

set filepath to my replace_chars(filepath, “:”, divider)

The problem is that the Finder doesn’t know what the “replace_chars” command/handler is. By adding “my” before “replace_chars” you are letting the Finder know that the script will handle this.

Oh, duh. I can’t believe I did that. Thanks!

Don’t feel bad, I spent the better part of a week trying to figure out why an Obj-C method I wrote wasn’t being called from Applescript. I was referring to the method’s class as:

“AB_Handler”

instead of

“AB_handler”

That’s right, the letter “h” was the cause of my grief. :lol:

Brad Bumgarner, CTA