• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Adding index entries via script

Explorer ,
Mar 31, 2023 Mar 31, 2023

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

 

TOPICS
Bug , Scripting

Views

434

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 31, 2023 Mar 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]
...

Votes

Translate

Translate
Community Expert ,
Mar 31, 2023 Mar 31, 2023

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

 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Mar 31, 2023 Mar 31, 2023

Copy link to clipboard

Copied

Hi Peter,

 

Nice!

Just a small error:

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 03, 2023 Apr 03, 2023

Copy link to clipboard

Copied

thank you.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 31, 2023 Mar 31, 2023

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 03, 2023 Apr 03, 2023

Copy link to clipboard

Copied

LATEST

Thank you very much Peter.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines