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

Find and update an Index using javascript

Community Beginner ,
Sep 09, 2024 Sep 09, 2024

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

TOPICS
Scripting
862
Translate
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 , Sep 09, 2024 Sep 09, 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 Lauben

...
Translate
Community Expert ,
Sep 09, 2024 Sep 09, 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

RobertatIDTasker_0-1725880634372.pngexpand image

 

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.

 

Translate
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 Beginner ,
Sep 09, 2024 Sep 09, 2024

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

social_Dream98A2_0-1725880844980.pngexpand image

 

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!

Translate
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 ,
Sep 09, 2024 Sep 09, 2024

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

 

RobertatIDTasker_0-1725881388847.pngexpand image

 

There is no option to silently update Index from script.

 

Translate
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 ,
Sep 09, 2024 Sep 09, 2024

There is update() for Index:

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

 

but:

 

undefined update ()

Updates the index preview pane. Note: Does not update the index.

 

 

But there is Generate():

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

 

RobertatIDTasker_0-1725881907610.pngexpand image

 

Translate
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 ,
Sep 09, 2024 Sep 09, 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;

};

 

 

Translate
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 ,
Sep 09, 2024 Sep 09, 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 )

Translate
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 ,
Sep 09, 2024 Sep 09, 2024

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

- Mark

Translate
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 ,
Sep 09, 2024 Sep 09, 2024
LATEST

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 )

Translate
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