Re: replacing text - solved
Re: replacing text - solved
- Subject: Re: replacing text - solved
- From: Michael Glasser <email@hidden>
- Date: Wed, 5 Mar 2003 09:13:41 -0700
Perhaps I should have been more specific... I am having a script create
a log in HTML...I want some words to be bold and underline, so I want
"word" to be replaced with "<B><U>" & word & </U></B>" - at first I was
doing it as a universal replace, later decided just to have it bold.
With a few mods it could easily be made to replace. Anyway, here is my
completed solution. changeCase does not need to be able to convert to
upper or lower, but I grabbed that directly from Apple's web site and
MIGHT use the other functionality of it... if not, then I will simplify
it at some point.
thanks for the help...
on boldWords(fullText, searchString)
set capText to changeCase(fullText, 1)
set capString to changeCase(searchString, 1)
set startWord to offset of capString in capText
if startWord is not 0 then
set textSize to count of characters in fullText
set endWord to startWord + ((count of characters in searchString) - 1)
if startWord is not equal to 1 then
set textBeg to characters 1 through (startWord - 1) of fullText
else
set textBeg to ""
end if
set textMid to characters startWord through (endWord) of fullText
if endWord is not equal to textSize then
set textEnd to characters (endWord + 1) through textSize of fullText
else
set textEnd to ""
end if
set fullText to "" & textBeg & "<B><U>" & textMid & "</U></B>" &
textEnd
end if
return fullText
end boldWords
on changeCase(thisText, thisCase)
set thisText to "" & thisText
if thisCase is 0 then
set the comparison_string to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set the source_string to "abcdefghijklmnopqrstuvwxyz"
else
set the comparison_string to "abcdefghijklmnopqrstuvwxyz"
set the source_string to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
end if
set the newText to ""
repeat with thisChar in thisText
set x to the offset of thisChar in the comparison_string
if x is not 0 then
set the newText to ,
(the newText & character x of the source_string) as string
else
set the newText to (the newText & thisChar) as string
end if
end repeat
return the newText
end changeCase
On Monday, March 3, 2003, at 09: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 on a working example of
----------------------------------------------
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)
do nifty magic that just works
return magic_text
end
_______________________________________________
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.
_______________________________________________
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.