Copy link to clipboard
Copied
Hi All.
Can anyone tell me how can I count doc pages eith certain master applied?
Thanks in advance.
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);
It'd be good to add a check for no master page, too, just before testing the name.
if (_pages[i].appliedMaster)
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
...
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
It'd be good to add a check for no master page, too, just before testing the name.
if (_pages[i].appliedMaster)
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
Great! Awesome! Thank you very much! 🙂
Copy link to clipboard
Copied
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