2

I would like a script to update all tables of content in my document. I tried

var t = app.activeDocument.tocStyles;  
for (var i=0; i<t.length; i++) {   
  app.activeDocument.createTOC(t[i],true);  
}

as suggested here, but for those TOC styles I do not uses it creates a new story. So, how can I figure out whether a TOC style is actually used? I know I can loop over all stories and check whether their storyType is StoryTypes.TOC_STORY, but I haven't figured out how to determine which TOC style that story then uses or how to refresh specifically that story. I even considered using

app.menuActions.item("$ID/UpdateTableOfContentsCmd").invoke();

which would just use the menu action (which sounds like something to avoid), but in that case I fail due to the action being inactive - I must change the selection somehow first, but app.select(story) fails due to an invalid data type...

5
  • This might be off topic for graphic design as its a script question. You might have better luck getting an answer on stackoverflow.com
    – Lex
    Commented May 24, 2016 at 12:05
  • @Lex I've seen more helpful scripting answers here than on SO. I'd have asked there if the question were more about JavaScript itself, but this is more a question of the InDesign API Commented May 24, 2016 at 12:10
  • Still, in questions not to ask "Application Troubleshooting questions or other forms of technical support". I think it would fall under off topic (other scripting questions too, not this one specifically)
    – Lex
    Commented May 24, 2016 at 12:43
  • @Lex Then what's the point of the indesign-scripting tag? I'm not asking for troubleshooting or technical support. Commented May 24, 2016 at 14:04
  • But it is another form of technical support :p Its good point about the indesign-scripting tag... Anyway I'm not flagging it off topic, I hope you get an answer.
    – Lex
    Commented May 24, 2016 at 14:09

1 Answer 1

2

Ok, here's my "solution" so far, unfortunately relying on using menuActions:

function updateTOCs() {
    var stories = app.activeDocument.stories;
    for(var i=0; i < stories.length; i++) {
        var story = stories[i]
        if(story.storyType != StoryTypes.TOC_STORY)
            continue;
        story.textContainers[0].select()
        app.menuActions.item("$ID/UpdateTableOfContentsCmd").invoke();
    }
}
2
  • I think that, at this date (almost 3 years passed!) this is the only way to do that. Thank you for sharing your solution.If only Adobe updated its scripting engine... :/ Commented Mar 18, 2019 at 8:40
  • 1
    @GabrielRambaud Thanks, it's comforting to hear I'm not alone with this 😅 Commented Mar 18, 2019 at 9:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.