Skip to main content
Participant
April 13, 2008
Question

Can I build a script to delete unused masters in CS2?

  • April 13, 2008
  • 4 replies
  • 3788 views
Can it be done? I've tried to build a script to select unused masters and delete them, but so far have been unsuccessful. Whats more is when I googled it I didn't really get anything... is it even possible?

Thanks in advance for any/all assistance!
This topic has been closed for replies.

4 replies

Community Expert
May 10, 2021

FWIW: A working script by Peter Kahrel can be found in this other thread, also tested it against my sample document from 2015 that is discussed above. All tests done with InDesign 2021 on Windows 10. Should also work with previous versions of InDesign:

 

https://community.adobe.com/t5/indesign/delete-unused-master-spreads/m-p/12026724#M425553

 

Thank you Peter Kahrel, thank you Gregor Fellenz for posting the code again and for removing a little glitch in the code from September 2015.

 

Regards,
Uwe Laubender

( ACP )

Peter Kahrel
Community Expert
Community Expert
April 13, 2008
Sure. But checking a string is easier than checking an array.

Peter
Colin Flashman
Community Expert
Community Expert
December 7, 2013

Sorry to bump an old thread, but both of these scripts have an issue where a master is based upon another master. I've uploaded a testfile: https://dl.dropboxusercontent.com/u/55743036/remove%20unused%20master%20pages%20test.idml

In the test file, masters A,B & C should remain - master B is based on the A master, and C master is a standalone, and there are three other master pages that should be deleted when the script runs. However, when either of the above scripts are tested, only B & C remain, despite the fact that the B master is based on the A master.

I did spy upon another script: http://extendscript.blogspot.com.au/2009/10/delete-unused-master-pages.html, but that doesn't appear to do anything to this testfile at all.

Any thoughts?

Colly

If the answer wasn't in my post, perhaps it might be on my blog at colecandoo!
Jump_Over
Legend
December 7, 2013

Hi,

This issue can be serious...

I suggest to use something managed by Adobe already:

var

     mMenu = app.menus.item("$ID/PagesPanelPopup"),

     aDoc = app.activeDocument;

if(aDoc) {

     mMenu.menuItems[5].associatedMenuAction.invoke();

     mMenu.menuItems[4].associatedMenuAction.invoke();

     }

Jarek

Message was edited by: Jump_Over In case of CS2 usefullness..., can't check it but be awared of.

Inspiring
April 13, 2008
How about:

Array.prototype.contains = function(myObj){

for (var i=0; this.length > i; i++) {
if(myObj == this){
return true;
}
}
return false;
}

aDoc = app.documents[0];
aMasters = aDoc.pages.everyItem().appliedMaster;
masters = aDoc.masterSpreads;
for (var j = masters.length - 1; j >= 0; j--) {
if (aMasters.contains(masters)) {continue}
masters.remove();
}


Dave
Peter Kahrel
Community Expert
Community Expert
April 13, 2008
This one is very basic:

doc = app.activeDocument;

applied_master_names = "|";
for (i = 0; i < doc.pages.length; i++)
applied_master_names += doc.pages.appliedMaster.name + "|";

masters = doc.masterSpreads;
for (i = masters.length-1; i >= 0; i--)
if (applied_master_names.indexOf ("|"+masters.name+"|") < 0)
masters.remove();


The first loop goes through all pages, collecting the names of the applied master frames. It creates a string with all these names separated by |. The second loop goes through the master spreads: if the name of a master does not occur in the string with applied master names, the master spread is deleted.

The script is basic and will probably run slowly on long documents, but it could be optimised in several places.

Peter