Copy link to clipboard
Copied
Hi,
I need to delete unused master spreads from document. If there is any unused master spreads then its removed. But when there is no unused master spreads, its pop up the below alert. Its removing pages. I'm using the below code.
Whats my in the below code.
function removeUnusedMaster()
{
app.selection=1851876449;
if(app.menuActions.itemByID(6145).checked == true)
{
try{
app.menuActions.itemByID(6151).invoke();
app.menuActions.itemByID(6150).invoke();
}catch(e){ // Remove all spread except 1st one, when all spreads are unused except none.
app.activeDocument.masterSpreads[1].select()
app.activeDocument.masterSpreads[app.activeDocument.masterSpreads.length-1].select(SelectionOptions.addTo);
app.menuActions.itemByID(6176).invoke();
}
}else
{
//try{
app.menuActions.itemByID(6145).invoke();
app.menuActions.itemByID(6151).invoke();
app.menuActions.itemByID(6150).invoke();
//}catch(e){}
}
}
Thanks in advance,
Sudha K
The alert that you see pops up not when there are no unused master spreads, but when you try to delete a master that's applied to another master or a page/spread. You can't use menuActions for this I don't think because you need to be able to tell whether a master is applied. With menuActions the test is to check whether that alert pops up, but you can't script that.
But it's not too hard to check whether masters are used: create a list of masters applied to the document's pages, another list of
...Copy link to clipboard
Copied
The alert that you see pops up not when there are no unused master spreads, but when you try to delete a master that's applied to another master or a page/spread. You can't use menuActions for this I don't think because you need to be able to tell whether a master is applied. With menuActions the test is to check whether that alert pops up, but you can't script that.
But it's not too hard to check whether masters are used: create a list of masters applied to the document's pages, another list of masters applied to the document's masters (i.i. a list of masters based on other masters), then remove the masters that don't occur in either list.
(function(){
var pages;
var masters;
var i;
var appliedToPages = {};
var appliedToMasters = {};
// Get the names of the masters applied to document pages
pages = app.documents[0].pages.everyItem().getElements();
for (i = pages.length-1; i >= 0; i--) {
if (pages.appliedMaster !== null) {
appliedToPages[pages.appliedMaster.name] = true;
}
}
// Get the names of masters applied to masters (i.e. masters based on other masters)
masters = app.documents[0].masterSpreads.everyItem().getElements();
for (i = masters.length-1; i > 0; i--) {
if (masters.appliedMaster !== null) {
appliedToMasters[masters.appliedMaster.name] = true;
}
}
// Delete masters that are not applied to any document pages or other masters
for (i = masters.length-1; i > 0; i--) {
if (!appliedToPages[masters.name] && !appliedToMasters[masters.name]) {
// $.writeln (masters.name);
masters.remove();
}
}
}());
Peter
Copy link to clipboard
Copied
Hi,
Thank you... Its working fine... Sorry for the late reply...
Thanks,
Sudha K
Copy link to clipboard
Copied
Almost what I was looking for.
I added a do/while loop because masters not used in the document with unused masters based on them weren't being deleted. I'm sure there is a faster way of doing it, but this is working for me.
var myDocument = app.activeDocument;
var masterPagesHaveBeenDeleted = false;
var pages;
var masters;
var i;
var appliedToPages = {};
mastersAppliedToPages ();
do {
removeUnusedMasters ();
}
while (masterPagesHaveBeenDeleted === true);
function mastersAppliedToPages (){
// Get the names of the masters applied to document pages
pages = myDocument.pages.everyItem().getElements();
for (i = pages.length-1; i >= 0; i--) {
if (pages.appliedMaster !== null) {
appliedToPages[pages.appliedMaster.name] = true;
}
}
}
function removeUnusedMasters (){
masterPagesHaveBeenDeleted = false;
var appliedToMasters = {};
// Get the names of masters applied to masters (i.e. masters based on other masters)
masters = myDocument.masterSpreads.everyItem().getElements();
for (i = masters.length-1; i > 0; i--) {
if (masters.appliedMaster !== null) {
appliedToMasters[masters.appliedMaster.name] = true;
}
}
// Delete masters that are not applied to any document pages or other masters
for (i = masters.length-1; i > 0; i--) {
//$.writeln ("checking " + masters.name);
if (!appliedToPages[masters.name] && !appliedToMasters[masters.name]) {
//$.writeln (" deleting " + masters.name);
masters.remove();
masterPagesHaveBeenDeleted = true;
}
}
}
Copy link to clipboard
Copied
I have just tested this script with a scenario as set out by Uwe Laubender from this post (12th post in) https://forums.adobe.com/message/1112390#1112390‌
The script has an unwanted result of removing a master page that is in use.
Thought it best to warn you.
Colin
Copy link to clipboard
Copied
Thanks, Colin. I had completely forgotten about that thread. The script in comment 1, above, can be changed to handle Uwe's case by looking at master pages rather than master spreads in the second for-loop:
(function(){
var pages;
var masterPages;
var masterSpreads;
var i;
var appliedToPages = {};
var appliedToMasters = {};
// Get the names of the masters applied to document pages
pages = app.documents[0].pages.everyItem().getElements();
for (i = pages.length-1; i >= 0; i--) {
if (pages.appliedMaster !== null) {
appliedToPages[pages.appliedMaster.name] = true;
}
}
// Get the names of masters applied to masters (i.e. masters based on other masters)
masterPages = app.documents[0].masterSpreads.everyItem().pages.everyItem().getElements();
for (i = masterPages.length-1; i > 0; i--) {
if (masterPages.appliedMaster !== null) {
appliedToMasters[masterPages.appliedMaster.name] = true;
}
}
// Delete masters that are not applied to any document pages or other masters
masterSpreads = app.documents[0].masterSpreads.everyItem().getElements();
for (i = masterSpreads.length-1; i > 0; i--) {
if (!appliedToPages[masterSpreads.name] && !appliedToMasters[masterSpreads.name]) {
masterSpreads.remove();
}
}
}());
Nice catch, Naomi. I didn't put your fix in yet.
Peter
Copy link to clipboard
Copied
Peter scripts works fine. Fixed a little glitch in the second for loop (>= instead of >)
function remmoveMasterSpreads(dok) {
// Peter Kahrel https://community.adobe.com/t5/indesign/delete-unused-master-spreads/td-p/7433938
var appliedToPages = {};
var appliedToMasters = {};
// Get the names of the masters applied to document pages
var pages = dok.pages.everyItem().getElements();
for (var i = pages.length - 1; i >= 0; i--) {
var page = pages[i];
if (page.appliedMaster !== null) {
appliedToPages[page.appliedMaster.name] = true;
}
}
// Get the names of masters applied to masters (i.e. masters based on other masters)
var masterPages = dok.masterSpreads.everyItem().pages.everyItem().getElements();
for (i = masterPages.length - 1; i >= 0; i--) {
var masterPage = masterPages[i]
if (masterPage.appliedMaster !== null) {
appliedToMasters[masterPage.appliedMaster.name] = true;
}
}
// Delete masters that are not applied to any document pages or other masters
var masterSpreads = dok.masterSpreads.everyItem().getElements();
for (i = masterSpreads.length - 1; i > 0; i--) {
var masterSpread = masterSpreads[i];
if (!appliedToPages[masterSpread.name] && !appliedToMasters[masterSpread.name]) {
masterSpread.remove();
}
}
}
remmoveMasterSpreads(app.activeDocument)
Copy link to clipboard
Copied
Hi Colin,
the link you posted is not working anymore, because that thread was moved from the old InDesign Scripting forum to the new InDesign forum by the end of 2019. Searched a bit in the forum. I think, you refer to this post by me:
Can I build a script to delete unused masters in CS2?
Laubender, Feb 24, 2015
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Hi gang,
Which of the 4 scripts on this thread is the most reliable version of script to remove master pages/spreads? I see 4 versions here?
Copy link to clipboard
Copied
What if you just copy normal pages to a new document? 😉 it should copy ONLY used Masters... but it will also strip all unused *Styles and colors - so may not be exactly what you are looking for...
Copy link to clipboard
Copied
Hi @Mike Witherell ,
test the code Gregor Fellenz ( @grefel ) did out of Peter's post.
Regards,
Uwe Laubender
( Adobe Community Expert )
Copy link to clipboard
Copied
Thanks! 🙂