Re: replacing text
Re: replacing text
- Subject: Re: replacing text
- From: David Hood <email@hidden>
- Date: Tue, 4 Mar 2003 20:41:34 +1300
On Tuesday, March 4, 2003, at 05:30 PM, Michael Glasser wrote:
The apple web site has a script for replacing a word in text, for
example "I like bananas" could become "I like apples"... but their
example only works if you know the case of the text in question. I
have been struggling with making a function that would allow me to
check a set of words and change the text... for example see the
following code... I want it to display "I WANT TO SEE A big PENGUIN"...
but I can not get the function replace_words to work...
any ideas
This seems to work (drawing on lists vs. strings) regardless of case,
and doesn't need additions or the command line, which was actually my
first thought. Note the answer is ...big penguin since you are
substituting the_word (penguin) for the original form (PENGUIN).
_____________
set the_list to {"penguin", "turtle", "owl"}
set the_text to "I WANT TO SEE A PENGUIN"
repeat with loop from 1 to number of items in the_list
set the_word to item loop of the_list
set the_text to replace_words(the_text, the_word, "big " & the_word)
end repeat
display dialog the_text -- answer should be "I WANT TO SEE A big
PENGUIN" (I do not expect the replacement text to guess the case)
on replace_words(full_text, search_string, replacement_string)
set saveddelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {" "}
set magic_list to {}
set list_of_words to every word in full_text
repeat with individual_word in list_of_words
if (individual_word as text = search_string) then set
individual_word to replacement_string
set magic_list to magic_list & individual_word
end repeat
set magic_text to magic_list as string
set AppleScript's text item delimiters to saveddelimiters
return magic_text
end replace_words
--------------------------
Regards,
David Hood
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.