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
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.
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
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
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