Copy link to clipboard
Copied
I use a simple script to add selected text to Index, however if I select two words and run this script the index marker won't be at the start of the selected text IF there are two multiple row tables before selected text in the story, but if I add the same selection to index via index panel it works as expected.
here is my code to add selected text to index, I also attached a file if anyone wants to try:
var selectedText = app.selection[0].contents;
var index = app.activeDocument.indexes[0];
var topic = index.topics.add(selectedText);
topic.pageReferences.add(app.selection[0].insertionPoints[0], PageReferenceType.CURRENT_PAGE);
Copy link to clipboard
Copied
This is a known problem, has been around forever. But since Adobe seem to have lost all interest in the index feature, it will probably never be fixed.
What you need to do is record where the cursor is, create the page reference, and check where the page reference landed. If it isn't as the recorded spot, move it there:
// Record the cursor position
var ip = app.selection[0].insertionPoints[-1].index;
var selectedText = app.selection[0].contents;
var index = app.activeDocument.indexes[0];
var topic = index.topics.add(selectedText);
// Add a pageReference
var pRef = topic.pageReferences.add (
app.selection[0].insertionPoints[0],
PageReferenceType.CURRENT_PAGE
);
// Check whether the pageReference landed at the recorder cursor position
if (Math.abs (pRef.sourceText.index - ip) > 0) {
// The page reference did not land at the correct place: move it
try {
app.selection[0].parentStory
.characters [pRef.sourceText.index]
.move (LocationOptions.AFTER, app.selection[0].insertionPoints[0]);
} catch (_) {
}
}
Copy link to clipboard
Copied
Hi Peter,
Nice!
Just a small error:
Copy link to clipboard
Copied
thank you.
Copy link to clipboard
Copied
Thanks, Michel. I corrected it in the posted code.
Copy link to clipboard
Copied
Thank you very much Peter.