Skip to main content
Inspiring
September 9, 2024
Answered

Find and update an Index using javascript

  • September 9, 2024
  • 4 replies
  • 1301 views

Hello, i'm trying to make a javascript script to find update an index but i'm stucked.

 

I'm using InDesign CS5.5 and the ExtendScript Toolkit 4.1.28

 

I have an indb file and many indd, I can locate the right file indd and open it with app.open.

I tried to look at indexes but they are always 0, I also tried to check textFrames to find the right one but without success.

 

I looked for a way to inspect the document to identify the structure but i can't find anythin better than randomly checking elements with the debugger.

 

I manually update the index selecting the textbox and then with Edit -> Update index

 

Can you help me to aumate it?

 

Thanks

This topic has been closed for replies.
Correct answer Laubender

Hi @giovanni_4827 ,

you said: "I manually update the index selecting the textbox and then with Edit -> Update index"

Hm. I cannot see a menu command like that.

 

Instead one could generate a new index with the option to update the one that exists. That's a property of the document.indexGenerationOptions.

 

This should work with one single index in the active document:

app.documents[0].indexGenerationOptions.replaceExistingIndex = true;
app.documents[0].indexes[0].generate();

 

Regards,
Uwe Laubender
( Adobe Community Expert )

4 replies

Community Expert
September 9, 2024

Hi @giovanni_4827 ,

there are other options with indexGenerationOptions as well. Did not find the DOM documentation for InDesign CS5.5, but the one for InDesign CS6 version 8.0 should be sufficient; don't think that there were options added or missing in comparison:

https://www.indesignjs.de/extendscriptAPI/indesign8/#IndexOptions.html#d1e112769

 

The other one that you are interested in when updating the index of a book file is:

 

app.documents[0].indexGenerationOptions.includeBookDocuments = true;

 

But I did not test this with a book file…

That's up to you.

 

Regards,
Uwe Laubender
( Adobe Community Expert )

LaubenderCommunity ExpertCorrect answer
Community Expert
September 9, 2024

Hi @giovanni_4827 ,

you said: "I manually update the index selecting the textbox and then with Edit -> Update index"

Hm. I cannot see a menu command like that.

 

Instead one could generate a new index with the option to update the one that exists. That's a property of the document.indexGenerationOptions.

 

This should work with one single index in the active document:

app.documents[0].indexGenerationOptions.replaceExistingIndex = true;
app.documents[0].indexes[0].generate();

 

Regards,
Uwe Laubender
( Adobe Community Expert )

m1b
Community Expert
Community Expert
September 9, 2024

Ah! That's a missing piece I didn't find! Thanks @Laubender. I've never scripted anything to do with indexes before.

- Mark

m1b
Community Expert
Community Expert
September 9, 2024

Update: Please ignore this answer. I didn't know about the `indexGenerationOptions` which makes this process simple. See @Laubender's correct answer. I will leave this here as an embarrassing failure! 🙂

 

Hi @giovanni_4827, I've never done this before, so apologies if this isn't quite right, but it worked in my simple testing. Here is a function "regenerateIndexStory" which should do what you ask. You need to pass it the indexing Story as one of the parameters.

- Mark

 

/**
 * Quick Example of regenerating an Index Story.
 * @author m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/find-and-update-an-index-using-javascript/m-p/14849563
 */
function main() {

    var doc = app.activeDocument,
        story = doc.selection[0];

    if (
        !story
        || !story.isValid
        || (
            'Story' !== story.constructor.name
            && !story.hasOwnProperty('parentStory')
        )
    )
        return alert('Please select an index story to update and try again.');

    // so we can handle any kind of sub-story text or text frame
    if (story.hasOwnProperty('parentStory'))
        story = story.parentStory;

    var result = regenerateIndexStory(doc, story);

    alert(result
        ? 'Index updated successfully.'
        : 'Index was not updated.'
    );

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Update Selected Index');

/**
 * Regenerates an Index Story.
 * @author m1b
 * @version 2024-09-09
 * @param {Document} doc - an Indesign Document.
 * @param {Story} story - a Story of "Indexing" StoryType.
 * @param {Index} [index] - the index to regenerate (default: first index of document).
 * @returns {Boolean} - true if the operation was successful.
 */
function regenerateIndexStory(doc, story, index) {

    var page = story.textContainers[0].parentPage,
        index = index || doc.indexes[0];

    // we need an "indexing" Story
    if (
        !story
        || !story.hasOwnProperty('storyType')
        || !story.isValid
        || StoryTypes.INDEXING_STORY !== story.storyType
        || !index.isValid
    )
        return false;

    // Note: the generate method returns an
    // Array of Stories and I just pick the
    // first one because I don't know when
    // there will be more than one.
    var tempStory = index.generate(page)[0];

    // the Index.generate method creates new text frame(s)
    // so I guess we need to remove these later
    var deleteMe = tempStory.textContainers;

    // update the old index story with the generated story
    story.contents = '';
    tempStory.duplicate(LocationOptions.AT_END, story.insertionPoints[0]);

    // clean up
    for (var i = deleteMe.length - 1; i >= 0; i--)
        deleteMe[i].remove();

    return true;

};

 

 

Robert at ID-Tasker
Legend
September 9, 2024

Can you post a screenshot with this Menu -> Update Index option?

I can't find it in the CC 2024 / 19.3

 

If you want to find Story that holds Index:

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Story.html

 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#StoryTypes.html#d1e75505

 

or you can invoke menu action:

Generate Index... | Panel Menus:Index | 78084

 

but as it's a dialog - you'll have to confirm it manually.

 

Inspiring
September 9, 2024

My bad, it's under layout. In the screenshot above it's italian.

 

If i click update there are no prompt.

If I can find how to check if a TextFrame has a Story, how can I update it?

 

Thanks!

Robert at ID-Tasker
Legend
September 9, 2024

Yes, I've checked Edit and Layout Menus, unfortunately, TOC and Index are two different things:

 

 

There is no option to silently update Index from script.