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

Count pages with applied master

Explorer ,
Jul 27, 2021 Jul 27, 2021

Hi All.

Can anyone tell me how can I count doc pages eith certain master applied?

Thanks in advance.

TOPICS
Scripting
800
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

correct answers 3 Correct answers

Community Expert , Jul 27, 2021 Jul 27, 2021

Hi,

Try following

 

//Counting pages with master page applied with name 'A-Master'. Use name of your master page instead of 'A-Master'
var doc = app.activeDocument;
var _pages = doc.pages;
var totalCountForMasterPages = 0;
for (var i = 0; i < _pages.length; i++) {
    if (_pages[i].appliedMaster && _pages[i].appliedMaster.name == "A-Master")
        totalCountForMasterPages++;
}
alert("Pages with with 'A-Master' applied on it is : " + totalCountForMasterPages);

 

 

 

Translate
Community Expert , Jul 27, 2021 Jul 27, 2021

It'd be good to add a check for no master page, too, just before testing the name.

if (_pages[i].appliedMaster)

 

Translate
Community Expert , Jul 27, 2021 Jul 27, 2021

Hi,

Try following that will count from active page

var doc = app.activeDocument;
var _pages = doc.pages;
var _pageName = 'B-Content'
var activePageIndex = doc.layoutWindows[0].activePage.documentOffset;
var totalCountForMasterPages = 0;
for (var i = activePageIndex; i < _pages.length; i++) {
    if (_pages[i].appliedMaster && _pages[i].appliedMaster.name == _pageName)
        totalCountForMasterPages++;
}
alert("Pages with with '" + _pageName + "'  applied on it is : " + totalCountForMasterPages
...
Translate
Community Expert ,
Jul 27, 2021 Jul 27, 2021

Hi,

Try following

 

//Counting pages with master page applied with name 'A-Master'. Use name of your master page instead of 'A-Master'
var doc = app.activeDocument;
var _pages = doc.pages;
var totalCountForMasterPages = 0;
for (var i = 0; i < _pages.length; i++) {
    if (_pages[i].appliedMaster && _pages[i].appliedMaster.name == "A-Master")
        totalCountForMasterPages++;
}
alert("Pages with with 'A-Master' applied on it is : " + totalCountForMasterPages);

 

 

 

Best regards
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 ,
Jul 27, 2021 Jul 27, 2021

It'd be good to add a check for no master page, too, just before testing the name.

if (_pages[i].appliedMaster)

 

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 ,
Jul 27, 2021 Jul 27, 2021

@m1b 

Updated with your feedback 🙂

Best regards
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 ,
Jul 27, 2021 Jul 27, 2021

Try this jsx: 

 

var nameToFind = "A-Master";

var pgs = app.documents[0].pages.everyItem().getElements();

var count = 0;

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

    try { if (pgs[i].appliedMaster.name === nameToFind) { count++; } } catch(e) {}

}

alert(count);

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 ,
Jul 27, 2021 Jul 27, 2021

You're awesome! Thank you! 

I have another question.

How can I count pages with applied master from active page to the end of document?

In this example i want to count "B-Content" pages and the result should be 4

p.1 -> A-Master
p.2 -> B-Content
p.3 -> B-Content
p.4 -> B-Content //active Page
p.5 -> B-Content
p.6 -> B-Content
p.7 -> B-Content
p.8 -> A-Master
p.9 -> A-Master
p.10 -> A-Master

 

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 ,
Jul 27, 2021 Jul 27, 2021

Hi,

Try following that will count from active page

var doc = app.activeDocument;
var _pages = doc.pages;
var _pageName = 'B-Content'
var activePageIndex = doc.layoutWindows[0].activePage.documentOffset;
var totalCountForMasterPages = 0;
for (var i = activePageIndex; i < _pages.length; i++) {
    if (_pages[i].appliedMaster && _pages[i].appliedMaster.name == _pageName)
        totalCountForMasterPages++;
}
alert("Pages with with '" + _pageName + "'  applied on it is : " + totalCountForMasterPages);
Best regards
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 ,
Jul 27, 2021 Jul 27, 2021

Great! Awesome! Thank you very much! 🙂

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
Engaged ,
Jul 27, 2021 Jul 27, 2021
LATEST

You can count multiple master pages applied document pages...

 

var doc = app.documents[0];
var pages = document.pages.everyItem().getElements();
var re = /(A-Master|B-Master)/i; //here you can add more master pages...
for(var rs = 0, counter=-1; ++counter<pages.length;){
	pages[counter].appliedMaster && re.test(pages[counter].appliedMaster.name) && rs++;
}
alert("Total pages with 'A-Master' and 'B-Master' are: " + rs);

- Sumit

-Sumit
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