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

Apply Master Pages to specific Group Styles and Paragraph Styles with ONE or TWO dimension names

Engaged ,
Jun 08, 2018 Jun 08, 2018

Copy link to clipboard

Copied

Hello!

I have paragraph styles with names in ONE and TWO dimensions, and I would apply automatically Master Pages based on their paragraph styles.

The number of the paragraph styles and master pages can increase and decrease, according to your needs.

My styles ONE dimension paragraph Styles are:

Chapter 1

Chapter 2

Chapter 3

...

My styles TWO dimension paragraph Styles are:

Chapter 1.1

Chapter 1.2

Chapter 1.3

Chapter 2.1

Chapter 2.2

Chapter 2.3

Chapter 3.1

Chapter 3.2

Chapter 3.3

...

My Master pages are:

C1-Chapter

C2-Chapter

C3-Chapter

....

TOPICS
Scripting

Views

412

Translate

Translate

Report

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

Engaged , Jun 08, 2018 Jun 08, 2018

Here is the solutions:

Here you can get the files, fotos, and code:

SOLUTION FOR ONE DIMENSION PARAGRAPH

//_____________________________________________________________________________________

// COUNT ALL PARAGRAPH STYLES IN A PARAGRAPH STYLES GROUP WITH THE SAME NAME            

var myDoc = app.documents[0];

    

var paragraphStyle1 = [];    

var counter = 0;    

var paragraphStyleGro

...

Votes

Translate

Translate
Engaged ,
Jun 08, 2018 Jun 08, 2018

Copy link to clipboard

Copied

LATEST

Here is the solutions:

Here you can get the files, fotos, and code:

SOLUTION FOR ONE DIMENSION PARAGRAPH

//_____________________________________________________________________________________

// COUNT ALL PARAGRAPH STYLES IN A PARAGRAPH STYLES GROUP WITH THE SAME NAME            

var myDoc = app.documents[0];

    

var paragraphStyle1 = [];    

var counter = 0;    

var paragraphStyleGroup1 = myDoc.paragraphStyleGroups.itemByName("CHAPTERS"); 

while(counter < paragraphStyleGroup1.paragraphStyles.length){     

    if(paragraphStyleGroup1.paragraphStyles[counter].name.toString().indexOf("Chapter ") != -1){   

paragraphStyle1.push(paragraphStyleGroup1.paragraphStyles[counter]);}     

  counter++;}  

totalParagraphStyle1=paragraphStyle1.length;  

alert("Total Paragraph Styles: " + totalParagraphStyle1); 

//____________________________________________________________________________________

//      END COUNT ALL PARAGRAPH STYLES IN A PARAGRAPH STYLES GROUP WITH THE SAME NAME

//_____________________________________________________________________________________

//                   START Apply Master Page based on Paragraph Style

var myDocument = app.activeDocument;

var myParas = myDocument.stories.everyItem().paragraphs.everyItem().getElements();

var myPage = myDocument.pages;

totalParagraphStyle1++;

for(i=0; i<myParas.length; i++){

for(j=0;j<totalParagraphStyle1;j++){

if(myParas.appliedParagraphStyle.name == "Chapter "+){

myParas.parentTextFrames[0].parentPage.appliedMaster = myDocument.masterSpreads.item("C"++"-Chapter");}}

}

alert("Done!");

//_____________________________________________________________________________________

//                     END Apply Master Page based on Paragraph Style

SOLUTION FOR TWO DIMENSION PARAGRAPH

//_____________________________________________________________________________________

// COUNT ALL PARAGRAPH STYLES IN A PARAGRAPH STYLES GROUP WITH THE SAME NAME   

var myName = "CHAPTERS"

var myArr =[];

var myParaStyleList = myGetParagraphStyleNames();

for (var i=0; i< myParaStyleList.length; i++)

{

    if(myParaStyleList.indexOf(myName) !=-1)

    {

        myArr.push(myParaStyleList);

    }

}

alert("Total Paragraph Styles FIRST and SECOND dimension: " + myArr.length); // TOTAL OF PARAGRAPH STYLES

var myArr1=[], myArr2 = [];

for(var i =0; i< myArr.length; i++)

{

    myArr1=myArr.split(".")[0];

}

for(var i =0; i< myArr.length; i++)

{

    myArr2=myArr.split(".")[1];

}

myArr3= Unique(myArr1);

alert("Total Paragraph Styles FIRST dimension: " + myArr3.length);   // TOTAL OF FIRST DIMENSION

myArr4 = Unique(myArr2);

alert("Total Paragraph Styles SECOND dimension: " + myArr4.length);   // TOTAL OF SECOND DIMENSION

function myGetParagraphStyleNames()

{

var result = [];   

var myParagraphStyles = app.activeDocument.allParagraphStyles;

var myParagraphStyleName, obj;

for(var i=0; i < myParagraphStyles.length ; i++)

{

    myParagraphStyleName = myParagraphStyles.name;

    obj = myParagraphStyles;

    while(obj.parent instanceof ParagraphStyleGroup)

    {

        myParagraphStyleName = obj.parent.name + ":" + myParagraphStyleName;

        obj = obj.parent;       

    }

    result.push(myParagraphStyleName);

} // for

result.shift(); // удаление стиля No Paragraph Style

return result;

} // fnc

function Unique(A)

{

    var n = A.length, k = 0, B = [];

    for (var i = 0; i < n; i++)

     { var j = 0;

       while (j < k && B !== A) j++;

       if (j == k) B[k++] = A;

     }

    return B;

}

//_____________________________________________________________________________________

//                     END Apply Master Page based on Paragraph Style

//_____________________________________________________________________________________

//                   START Apply Master Page based on Paragraph Style

var myDocument = app.activeDocument;

var myParas = myDocument.stories.everyItem().paragraphs.everyItem().getElements();

var myPage = myDocument.pages;

myArr3.length++;

myArr4.length++;

for(i=0; i<myParas.length; i++){

for(AppendixA=0;AppendixA<myArr3.length;AppendixA++){

for(AppendixB=0;AppendixB<myArr4.length;AppendixB++){

if(myParas.appliedParagraphStyle.name == "Chapter "+[AppendixA]+"."+[AppendixB]){

myParas.parentTextFrames[0].parentPage.appliedMaster = myDocument.masterSpreads.item("C"+[AppendixA]+"-Chapter");}}}

}

//_____________________________________________________________________________________

//                     END Apply Master Page based on Paragraph Style

alert("Done!");

QUICK VIEW

01.png

02.png

03.png

04.png

06.png

06.png

07.png

08.png

09.png

10.png

11.png

12.png

13.png

14.png

15.png

17.png

Thanks so much to Boris.

Votes

Translate

Translate

Report

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