New scripter needs help - search/replace in "tell" loop - syntax error

I’m new to AppleScript but have some scripting experience. I have the following working script for an app named GarageSale which is an ebay listing program. I’m basically trying to do a search and replace on the “description” filed.

Two scripts are given below. The first script works fine as given. I found the search/replace snippit in these forums. It searches and replaces text in a “description field” on a single selected (highlighted) item. But when I try to place the search/replace within a “tell GarageSale” statement (so I can "loop thru all selected), I get an error. (The repeat statement below the search/replace really doesn’t make sense for a one pass thru - it just there for testing…)

So the working script is shown first.
The modified non-working script is second.
The error given is: Expected “end” but found “to”.
The error is on “to” on this line: to searchReplace(thisText, searchTerm, replacement)

Any help would be appreciated. As I said, I got the search/replace working but I don’t really understand what is happening.


set theReply1 to (display dialog "Enter new replacement text" default answer "text" buttons {"Cancel", "Continue"} default button "Continue")
set origtext to text returned of theReply1

set theReply2 to (display dialog "Enter new replacement text" default answer "text" buttons {"Cancel", "Continue"} default button "Continue")
set replacetext to text returned of theReply2

tell application "GarageSale"
	set origline to get the description of the first selected template
end tell

searchReplace(origline, origtext, replacetext)
to searchReplace(thisText, searchTerm, replacement)
	set AppleScript's text item delimiters to searchTerm
	set thisText to thisText's text items
	set AppleScript's text item delimiters to replacement
	set thisText to "" & thisText
	set AppleScript's text item delimiters to {""}
	
	tell application "GarageSale"
		repeat with aTemplate in first selected template
			set the description of aTemplate to (thisText as text)
		end repeat
	end tell
	
end searchReplace


non-working script

set theReply1 to (display dialog "Enter new replacement text" default answer "text" buttons {"Cancel", "Continue"} default button "Continue")
set origtext to text returned of theReply1

set theReply2 to (display dialog "Enter new replacement text" default answer "text" buttons {"Cancel", "Continue"} default button "Continue")
set replacetext to text returned of theReply2

tell application "GarageSale"
repeat with aTemplate in every selected template
	set origline to get the description of the template

	searchReplace(origline, origtext, replacetext)
	to searchReplace(thisText, searchTerm, replacement)
		set AppleScript's text item delimiters to searchTerm
		set thisText to thisText's text items
		set AppleScript's text item delimiters to replacement
		set thisText to "" & thisText
		set AppleScript's text item delimiters to {""}
	
	set the description of aTemplate to (thisText as text)	
	end searchReplace
	
end repeat
end tell

Hi,

This is a scope confusion

in your second script you have a handler within an application tell block and some terminology of this application inside the handler.
This doesn’t work because the handler can’t see the tell block, because it’s out of the handler’s scope.

I figured something as such… can you offer a suggestion? As being new to AppleScript I need some guidance. Thanks.

First of all it’s always better habit to define the handlers before or after the main code.
A handler can return one or multiple (in a list) values, so use something like this


set theReply1 to (display dialog "Enter new replacement text" default answer "text" buttons {"Cancel", "Continue"} default button "Continue")
set origtext to text returned of theReply1

set theReply2 to (display dialog "Enter new replacement text" default answer "text" buttons {"Cancel", "Continue"} default button "Continue")
set replacetext to text returned of theReply2

tell application "GarageSale"
repeat with aTemplate in every selected template
	set origline to get the description of the template
	set the description of aTemplate to my searchReplace(origline, origtext, replacetext)
end repeat
end tell

to searchReplace(thisText, searchTerm, replacement)
	set AppleScript's text item delimiters to searchTerm
	set thisText to thisText's text items
	set AppleScript's text item delimiters to replacement
	set thisText to "" & thisText
	set AppleScript's text item delimiters to {""}
	return thisText
end searchReplace

Thanks much… I’ll give it a try and I’ll also do some studying… Thanks for the headstart, it’s much appreciatied.

Got it working perfectly… Thanks again. I really appreciate your sharing your knowledge with a new scripter. I’m anxious to do some learning…

Here’s the working script.

set theReply1 to (display dialog "Enter text to replace" default answer "text" buttons {"Cancel", "Continue"} default button "Continue")
set origtext to text returned of theReply1

set theReply2 to (display dialog "Enter new replacement text" default answer "text" buttons {"Cancel", "Continue"} default button "Continue")
set replacetext to text returned of theReply2

to searchReplace(thisText, searchTerm, replacement)
	set AppleScript's text item delimiters to searchTerm
	set thisText to thisText's text items
	set AppleScript's text item delimiters to replacement
	set thisText to "" & thisText
	set AppleScript's text item delimiters to {""}
	return thisText
end searchReplace

tell application "GarageSale"
	repeat with aTemplate in every selected template
		set origline to get the description of aTemplate
		set the description of aTemplate to my searchReplace(origline, origtext, replacetext)
	end repeat
end tell

:slight_smile: as mentioned above, it’s better programming habit to put the handler (to searchReplace . end earchReplace) after the main code,
to make the script better readable

got ya! guess I’m just the “sequential” type

a handler can be called from everywhere. This defeats the “sequential” point :wink: