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

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

Explorer ,
Oct 09, 2024 Oct 09, 2024

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>

TOPICS
Scripting , SDK
228
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 ,
Oct 09, 2024 Oct 09, 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.");
}
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
Explorer ,
Oct 10, 2024 Oct 10, 2024

Hi Eugene

Kinldy check below

rikanth_0-1728545188386.png

 

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 ,
Oct 10, 2024 Oct 10, 2024
LATEST

ok

maybe it's

tocStyle.tocStyleEntries.add(paragraphStyle.name);
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