Another newbie post: why can't I call my handler?

This code is generating an error. Can someone help me fix it? Thanks! :slight_smile:

to findAndReplaceInText(theText, theSearchString, theReplacementString)
	set AppleScript's text item delimiters to theSearchString
	set theTextItems to every text item of theText
	set AppleScript's text item delimiters to theReplacementString
	set theText to theTextItems as string
	set AppleScript's text item delimiters to ""
	return theText
end findAndReplaceInText

tell application "Finder"
	set fold to folder "current_projects:macjournal_export:subset" of home
	set filenames to the name of every document file of fold
	set filedates to the creation date of every document file of fold -- set filenames to the name of every document file of files
	repeat with filename in filenames
		set fn to filename as string
		fn = findAndReplaceInText(fn, ".rtfd", "")
		findAndReplaceInText(fn, ".rtf", "")
		
		--set s to 	
	end repeat
end tell

When I run it, I get this mysterious error

on this line:

findAndReplaceInText("\"The art of being wise is the art of knowing what to overlook.\".rtf", ".rtfd", "")

Help!? :slight_smile:
Not sure how to post an actual screen shot here but I can if it will help.

I figured it out. It’s the old thing:

This is illegal:

 fn = findAndReplaceInText(fn, ".rtfd", "")

Ooops!

Here is a version which works:

repeat with filename in filenames
		set fn to filename -- as string -- EDITED, the coercion is useless
		set fn to my findAndReplaceInText(fn, ".rtfd", "") -- EDITED
		my findAndReplaceInText(fn, ".rtf", "") -- EDITED
		log result -- ADDED to see the result
		--set s to 	
	end repeat

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 15 mai 2020 10:33:10

Hi.

It’s because you’re calling the handler from within a ‘tell’ statement. The Finder doesn’t understand the ‘findAndReplaceInText’ command. In such cases, you have to use ‘my’, as in Yvan’s reply, to identify the call as belonging to the script rather than to the thing being ‘told’.