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
....
Here is the solutions:
Here you can get the files, fotos, and code:
...//_____________________________________________________________________________________
// 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
Copy link to clipboard
Copied
Here is the solutions:
Here you can get the files, fotos, and code:
//_____________________________________________________________________________________
// 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
//_____________________________________________________________________________________
// 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!");
Thanks so much to Boris.