I don't see what you're trying to do. You mention that you change the sort order of the topics but it's not clear where you do that. And you create strings of a topic name followed by its subtopics names, but it's not clear what you do with those strings. If you want to change sort orders, you'd look for topics, not page references, so you don't need the pageReference for-loop. Could you clarify what you want a bit more?
But if you do want to change the sort order, what you describe will always be a problem. Since InDesign immediately sorts the topic list whenever you change the Sort By field, the references to your array (myTopics) are likely to become invalid.
Sorry, my original post wasn't very clear. I'll try to explain more clearly:
Here is the big picture: I'm scripting a solution that takes a completed index, harvests the index references, and inserts the references into document structure XML that we eventually output.
So we have a completed index, with 2 or 3 levels of topics. Some of the topics have a "Sort by" entry in Page Reference Options, so that the entry sorts differently than it is spelled. See "and rituals" in the screen shot below. That entry would normally appear in the "a" position, but is sorted by "rituals" so it appears in the "r" position in the alphabetical list.
So, the script code that I posted in the original post iterates through each index topic from Z to A, and within each topic iterates through the page references from last to first. For each page reference, the script tries to resolve an insertion point for the page reference, and then goes off and adds code to the XML for the index entry.
The script works perfect, except when it hits a page reference that is displayed lower in the list than it would if it didn't have a "Sort by" entry. In this case, the script is unable to get the insertion point for the page reference, for some reason.
It's the adding XML code that messes your document insertion points up. I fell for that one too, these supposedly invisible codes take up *two* character positions.
I could solve it for my case by careful counting :)
Perhaps you could sort your topic references by their actual insertionpoint order before adding XML into your running text.
Jongware, thanks for the help, but I don't think that is what is going on in this case. I've removed all the code that is adding XML elements, and I still get an "Object is invalid" error on the "var myInsertionPoint = myTopics[i].pageReferences[j].sourceText;" line in my original post.
In other words, in my original code, in the "Do something at the insertion point" line, I'm not currently doing ANYTHING. I'm just trying to get the insertion point. I'm not altering or manipulating the original text, the index, or the XML at all.
Keith -- Apparently any values in the Sort By field interact in mysterious ways with the rest of the world. Maybe you could try something like this:
1. cycle through all topics and subtopics, adding the value of each (sub)topic's Sort By field as a script label to the (sub)topic -- then blank the Sort By field;
2. // Do something at the insertion point...
3. cycle through all topics again, restoring the Sort By field from what you stored in the topic's script label.
This shouldn't be necessary, but it looks as if it is.
Peter, I was thinking that I'd need to do some sort of workaround like this. But how can I add the value of each (sub)topic's Sort By field as a script label to the (sub)topic? It appears that page references can have labels, but not topics. Or am I missing something obvious here?
No, you're not missing anything! Sorry, I had just assumed that since you can add script labels to virtually anything, you could add them to topics as well. But now that I checked it turns out that you can't. Two ways out of this could be
(1) temporarily add the Sort By value to the topic's name separated by a unique string, blank the Sort By field. Later restore the Sort By field and the topic's name;
(2) if for some reason you can't add the Sort By value to the name, you'll have to build a table with topic names and Sort By values, blank the Sort By fields, then restore them later using the table.
Pretty convoluted, but I can't think of anything else.
Thanks Peter. This was really helpful. I was able to append the Sort By value to the topic name separated by #####, delete the sort value, do what I needed to do, and then restore the sort value. Works great! Thanks for the help and advice!