Skip to main content
Inspiring
January 24, 2023
Answered

TOC update only visible layer

  • January 24, 2023
  • 2 replies
  • 929 views

hi, is it possible update TOC('s) only in visible layers with a script?

This topic has been closed for replies.
Correct answer Samuel22307458169h

Hi found the script that updating all TOC in active document and i add the prop .visibile

var myDoc = app.activeDocument;
    // Remember the current view
var myPage = app.activeWindow.activePage;
var myObjectsArray = myDoc.allPageItems; // allPageItems returns an array of all objects in the document, even those in groups
var myNumObjects = myObjectsArray.length;
var myNumTOC = 0;
    for (var i=myNumObjects-1; i>=0; i--) {
        var myObject = myObjectsArray[i];
            if (myObject.constructor.name == "TextFrame") {
                if (myObject.parentStory.storyType == StoryTypes.TOC_STORY&& myObject.visible) {
                myObject.select();
                // Update the TOC
                app.scriptMenuActions.itemByID(71442).invoke();
                myNumTOC++;
            }
        }
    }

2 replies

Community Expert
January 26, 2023

Hi @Samuel22307458169h ,

if I get you right, you want to update all TOCs that are on visible layers.

Then the answer to your initial question is "yes".

 

The algorithm for your script with some ExtendScript properties and values:

 

Loop the array of all stories of the document that contains the TOCs. You get the array like this:

 

var allStoriesArray = app.documents[0].stories.everyItem().getElements();

 

Inspect all of the array's items, the stories, if it is a TOC story. The property for this is storyType. If the value is StoryTypes.TOC_STORY and if the stories first text container is on a visible layer, then update the story.

The first text container of a given story is textContainers[0] , the property of the layer of that container is itemLayer.

When the layer is visible, the value for property itemLayer.visible is true.

 

To update the TOC story you have to select a text frame of that story:

 

app.select( allStoriesArray[n].textContainers[0] );

 

 

Finally run the menu action for updating a story TOC:

 

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

 

 

If you need more help, just ask…

 

Regards,
Uwe Laubender
( Adobe Community Expert )

 

EDITED

Inspiring
January 27, 2023

Hi Laubender, thank you for the reply, i'm not a scripter, i don't know how writing this code for my problem

Samuel22307458169hAuthorCorrect answer
Inspiring
January 27, 2023

Hi found the script that updating all TOC in active document and i add the prop .visibile

var myDoc = app.activeDocument;
    // Remember the current view
var myPage = app.activeWindow.activePage;
var myObjectsArray = myDoc.allPageItems; // allPageItems returns an array of all objects in the document, even those in groups
var myNumObjects = myObjectsArray.length;
var myNumTOC = 0;
    for (var i=myNumObjects-1; i>=0; i--) {
        var myObject = myObjectsArray[i];
            if (myObject.constructor.name == "TextFrame") {
                if (myObject.parentStory.storyType == StoryTypes.TOC_STORY&& myObject.visible) {
                myObject.select();
                // Update the TOC
                app.scriptMenuActions.itemByID(71442).invoke();
                myNumTOC++;
            }
        }
    }
Eric Dumas
Community Expert
Community Expert
January 24, 2023

Hi,

Can you share more details of what you are trying to achieve. Can you explain the structure of your document, and what you are trying to get at the end.

Inspiring
January 24, 2023

hi, thank for the reply, i have a book(user manual) with 15/20 documents, at the second page i have a table of contents of the entire book, every document is a multilingual, every language has a different paragraph style, the TOC read every documents in the book and create a TOC dedicated to the language, also the TOC documents is multilingual.
everytime i create an user manual, i select the languages of entire book with a script(wich make visible or invisible the layers), for example, ENGLISH and SPANISH, i need a script that update the TOC only in the visible layers
(i'm italian, my english isn't sound good)

Community Expert
January 24, 2023

Hi @Samuel22307458169h ,

the best strategy for this is to have dedicated paragraph styles for the different languages. And you already have this into place. So you can do different TOC Styles for different TOCs where the different paragraph styles for the languages are used to fill a new TOC.

 

Regards,
Uwe Laubender
( Adobe Community Expert )