Skip to main content
ali u
Inspiring
March 31, 2023
Answered

Adding index entries via script

  • March 31, 2023
  • 2 replies
  • 874 views

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);

 

This topic has been closed for replies.
Correct answer Peter Kahrel

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 (_) {
  }
}

 

 

 

2 replies

Peter Kahrel
Community Expert
Community Expert
March 31, 2023

Thanks, Michel. I corrected it in the posted code.

ali u
ali uAuthor
Inspiring
April 3, 2023

Thank you very much Peter.

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
March 31, 2023

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 (_) {
  }
}

 

 

 

FRIdNGE
March 31, 2023

Hi Peter,

 

Nice!

Just a small error:

.characters [p_ref.sourceText.index]
instead of:
.characters [pRef.sourceText.index]
 
(^/)  The Jedi