Sebastien,
If script writers aren't stubborn they'll never get their script problems solved! A note on terminology: ExtendScript, by the way is just JavaScript. What you mean is InDesign's object model.
It's true that when you select an index marker in a text and you have the Index panel open, the referenced topic is highlighted in the panel. But that's not exposed to scripting, that is, the Character object doesn't have a property 'topic'. However, you can determine the selected marker's topic, even if it's a pretty laborious method. Index markers are characters in the text, so they have a parent story and an index. pageReferences have a property sourceText, which is an insertion point. To find an index marker's topic, you need to compare the marker's insertion point with the insertion points of all pageReferences.
The functionality that you miss -- selecting an index marker so that its parent topic is selected in the Index panel -- can be scripted as follows:
$.writeln (findTopic (app.selection[0]).name);
function findTopic (marker) {
var topics = app.documents[0].indexes[0].allTopics;
var mStory = marker.parentStory;
var mIndex = marker.insertionPoints[0].index;
var pRefs;
for (var i = topics.length-1; i >= 0; i--) {
pRefs = topics.pageReferences.everyItem().getElements();
for (var j = pRefs.length-1; j >= 0; j--){
if (pRefs.sourceText.parentStory == mStory && pRefs.sourceText.index == mIndex) {
return pRefs.parent;
}
}
}
return "";
}
Select an index marker in a text and run the script (this doesn't select anything in the panel, of course). Now, if you've many index markers, then looping through them to find their topic names could in principle be done as follows:
app.findGrepPreferences = null;
app.findGrepPreferences.findWhat = '~I';
markers = app.documents[0].findGrep();
for (var i = markers.length-1; i >= 0; i--) {
$.writeln (findTopic (markers).name);
}
But that would be horribly inefficient (though accurate). Better first to build a lookup table of the pageReferences and their story IDs and sourceText indexes so that you can find topic names quickly.
Your particular approach -- looping through all characters -- needs a bit of a modification in that you need to compare every character with the index.
Something like this:
for (var i = 0; i < myChars.length; i++) {
if (myChars.contents == '\uFEFF') { // myChar is probably an index marker
var topic = (findTopic (myChars).name);
if (topic != "") {
$.writeln (topic);
}
}
}
Peter