Does Applescript have a property "index" for your found text? In JS, I can get a list of *all* "ZZZZ"s and only change the one with an index as close as (but not less) to the "AAAA" index I found.
I think simply use replace function can do it easily. e.g.
story.replace("AAAA ZZZ", "AAAA TEST1");
story.replace("BBBB ZZZ", "BBBB TEST2");
you can combine with regular expression for advance search.
The problem magic3000_chris, unless I'm missing something, is that there's an indeterminate amount of text between the key strings ("AAAA" etc.) and the find/replace strings ("ZZZ"). And I can't use a regular expression at least not in the way I could imagine it with a positive lookbehind (find "ZZZ" that follows an indeterminate amount of text and "AAAA") because the positive look behind expression won't allow for a variable-length wildcard.
It does, though, look like Jongware's approach will work. Trying it now.