The act of tagging inserts to "characters" into the text. That's what's happening when you iterate forwards.
Since this is the only thing going on in your script you could convert the references on the fly. You'd have to keep track of whether or not you had changed stories to know how many characters the reference is off by.
Alternatively (and perhaps more easily) you could iterate backwards and then reverse the order of the XML elements by moving them.
Thanks for the reply.
I'm not sure what you mean by "convert the references on the fly." You mean find a way to advance two characters forward and tag that? In the script I submitted here I am only searching for "abc," which of course I would not search or at all in real life. The script uses grep to search for about twenty different typographical errors that typically appear and tags them all.
Reiterating backwards seems, at first sight the way to go. But I haven't a clue as to how to reverse the order of the stories. I can get an array of stories, and array.reverse() them, but am not sure how to then make the script put this reversed order into the xml structure. Any hints?
There, of course is bigger problem with the stories. If the document is written with one story created after another then the order of them is top to botom. But if say a text frame edited into the middle of all this, that story is still the last one in the xml structure. At least in one test, that was so. Maybe the way to solve this is to build an array of stories that have as one xml attribute the page it first appears on. Then order the array according to that attribute.
Any advice you give will be appreciated.
Assuming I'm understanding what you want to do, you're looking to:
1. Tag text.
2. Organize the xml structure to match the order of the stories in your document.
The way to do the latter is to use the move method for xmlElements. This is not something I've done much so I'm not sure of the full implications, but as long as you move story tags around relative to each other, this will not affect the layout of the document, just the structure of the xml representation.
Yes you are correct in understanding what I want to do. I'll have to experiment with xmlElement.move(). At the moment I don't quite see how to use it because one has to state as an argument what the story elements are moving in relation to. That of course is the root element. But how does this order the stories? I'll have to play around and find out.
I assume that you have two story elements and you want to move one before the other. So, you'd write:
This would move story element B ahead of story element A in the structure panel, taking all its children elements with it.