Find and Random Replace

Hello all,
I was wondering if there is an applescript way to find each instance of a text string in a larger string and then replace those instances with one random item from a list of possible replacers.

As in:
Original String: “Hello my name is firstame. My friend is firstname who is also the friend of firstname.”
Search String: “firstname”
Replace Items: {“Zach”,“John”,“Jim”,“Laurie”,“Kendra”}

I do not want for the result to be “Hello my name is Zach. My friend is Zach who is also the friend of Zach.” but rather for the each replacement string to be some random item from the list as in “Hello my name is John. My friend is Laurie who is also the friend of Kendra.” Repetition of the random items does not bother me at all: “Hello my name is Jim. My friend is Jim who is also the friend of Laurie.”

Thus the goal would be for the subroutine to take out “firstname” from the original string and replace it with one random item from the Replace Items list. I’ve tried working with all of the Text Item Delimiter-based find and replace subroutines and have been unable to make anything work.

Many thanks for the time anyone might grant my silly question.

Model: MacBook Pro 15’’
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hi,

here is one possibility

set origString to "Hello my name is firstname. My friend is firstname who is also the friend of firstname."
set searchString to "firstname"
set replaceItems to {"Zach", "John", "Jim", "Laurie", "Kendra"}

set oldDelims to text item delimiters
set text item delimiters to searchString
set origItems to text items of origString
set text item delimiters to oldDelims
set replaceString to ""
repeat with i from 1 to (count origItems) - 1
	set replaceString to replaceString & text item i of origItems & item (random number from 1 to count replaceItems) of replaceItems
end repeat
set replaceString to replaceString & last text item of origItems

Thank you so much StefanK. This is perfect, you are a genius.

A slight variation if you never want any of the firstNames to be the same:


set origString to "Hello my name is _*_. My friend is _*_ who is also the friend of _*_, who knows _*_ well."
set searchString to "_*_"
set replaceItems to {"Zach", "John", "Jim", "Laurie", "Kendra", "Mary", "Peter", "Kathy", "Jane"}
set oldDelims to text item delimiters
set text item delimiters to searchString
set origItems to text items of origString
set text item delimiters to oldDelims
set replaceString to ""
-- If you want them to all be different, then make list of necessary items
set R to {}
set c to (count origItems) - 1
repeat while (count R) ≠ c
	tell some item of replaceItems to if it is not in R then set end of R to it
end repeat
-- then add the pieces back together with the items of the all different name set.
repeat with k from 1 to c
	set replaceString to replaceString & text item k of origItems & item k of R
end repeat
set replaceString to replaceString & last text item of origItems