Skip to main content
Inspiring
October 10, 2024
Question

Script Not Working to Add Table of Contents Style from Paragraph Style Group

  • October 10, 2024
  • 1 reply
  • 336 views

Hi,

The blwo script is not working to add Table of contents style. Can advise to me.

 

var doc = app.activeDocument;
var pGroups=doc.paragraphStyleGroups.item("Group_Style");
var tocStyle = doc.tocStyles.add({
name: "TOC"
});
var tocEntry = tocStyle.tocStyleEntries.add(pGroups.paragraphStyles[0]);

 

 

<Title renamed by MOD>

This topic has been closed for replies.

1 reply

Community Expert
October 10, 2024

Not entirely sure - I'll throw my hat at it - usually putting through some alerts helps me narrow down where I'm going wrong

 

var doc = app.activeDocument;

// Check if the paragraph style group exists and is valid
var pGroups = doc.paragraphStyleGroups.itemByName("Group_Style");

if (pGroups.isValid) {
    var tocStyle;
    
    // Check if the TOC style already exists, to avoid duplication
    try {
        tocStyle = doc.tocStyles.itemByName("TOC");
        if (!tocStyle.isValid) {
            throw new Error("TOC style does not exist.");
        }
    } catch (e) {
        tocStyle = doc.tocStyles.add({
            name: "TOC"
        });
    }

    // Ensure that there is at least one paragraph style in the group
    if (pGroups.paragraphStyles.length > 0) {
        // Add a TOC entry if the paragraph style is valid
        var paragraphStyle = pGroups.paragraphStyles[0];
        if (paragraphStyle.isValid) {
            tocStyle.tocStyleEntries.add(paragraphStyle);
            alert("TOC style added successfully with paragraph style: " + paragraphStyle.name);
        } else {
            alert("Invalid paragraph style.");
        }
    } else {
        alert("No paragraph styles found in the group.");
    }
} else {
    alert("Paragraph style group 'Group_Style' does not exist.");
}
$rikanthAuthor
Inspiring
October 10, 2024

Hi Eugene

Kinldy check below

 

Community Expert
October 10, 2024

ok

maybe it's

tocStyle.tocStyleEntries.add(paragraphStyle.name);