Can I build a script to delete unused masters in CS2?
Copy link to clipboard
Copied
Thanks in advance for any/all assistance!
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Peter
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
g'day there.
I should have mentioned that I'm working in CS6 and the script above had an error.
I used Peter Kahrel's menuactions.jsx script to reveal the $ID item and came up with the following mod:
var
aDoc = app.activeDocument;
if(aDoc) {
app.menuActions.itemByID(6151).invoke();
app.menuActions.itemByID(6150).invoke();
}
seemed to work even if there were no master pages to delete, nor if the pages panel was not displayed.
did notice the second time it ran, it deleted a spread on the pages themselves... back to the drawing board... 😞
Colly
Copy link to clipboard
Copied
After more research I did find a script that should do the job but it appears to do nothing to the test file. A link to that script is:
http://extendscript.blogspot.com.au/2009/10/delete-unused-master-pages.html
Not sure if it is just my machine or not. Can anyone else confirm if this script works or not?
Colin
Copy link to clipboard
Copied
Hi,
It works on my side.
...not exactly THIS, since this is a function()..., I used:
xUnusedMasters(app.activeDocument);
Jarek
Copy link to clipboard
Copied
@Colin – Jarek is right. Your link is showing just a general function you can feed a document to. Scripters say the function takes an argument. The brackets after the name suggest a document: docRef
Jarek was showing how to handle this:
//First the function call (the order of the two units are not important):
xUnusedMasters(app.activeDocument);
//Then the function itself:
function xUnusedMasters( docRef ) {
//All code of function goes here:
};
You could call it after the definition of the function and feed it like this (a variant of the above):
function xUnusedMasters( docRef ){//All code of function goes here:
};
//variable d is defined as
//the first document in the open document collection
//= the active documentvar d = app.documents[0];
//call of the function with variable d as argument:
xUnsusedMasters(d);
The script is working for me, too.
Uwe
Message was edited by: Laubender
Copy link to clipboard
Copied
In fact, the script removes all unused master spreads, not unused master pages…
Uwe
Copy link to clipboard
Copied
I can't believe that's all it was - call the active document... D'oh!
The script does what I had hoped it would do - remove any unused master spreads in the master pages panel, but without removing master pages BASED ON other master pages.
Thank you both Uwe and Jarek for your assistance and perseverance in assisting me with this script.
Colly
Copy link to clipboard
Copied
@Colly and Jarek – I have to revive this old thread, because today I came accross a situation where the script Colly digged out will not work as expected. I ran it onto a "simple" document where one master spread was based on another master spread.
See my screenshot here:
Master A is applied for pages 1 and 4
Master B is applied for pages 2 and 3
Master B is based on Master A for even pages
(Master A is applied to Master B for even pages)
Master B is based on Master C for odd pages
(Master C is applied to Master B for odd pages)
So page 3 is showing all elements of Master B (odd pages) and Master C (odd pages)
Unfortunately:
Master C will be removed after running xUnusedMasters(app.activeDocument);
However it will do its job on Colin's test file (download link posted in answer #4 and still downloadable)
Tested with InDesign CS5.5 v7.5.3, CS6 v8.1.0, CC v9.3.0 and CC 2014.2 v10.2.0 on Mac OSX 10.7.5
Uwe
Copy link to clipboard
Copied
Instead of asking:
app.documents[0].masterSpreads[1].appliedMaster.name;
//Result from my posted screenshot: "A-Musterseite"
we have to look at all pages of a master spread:
app.documents[0].masterSpreads[1].pages[0].appliedMaster.name;
//Result from my posted screenshot: "A-Musterseite"
Then we will get a more differentiated picture what is used:
app.documents[0].masterSpreads[1].pages[1].appliedMaster.name;
//Result from my posted screenshot: "C-Musterseite"
Here a loop through all my master spreads as seen in the screenshot:
var myDoc = app.documents[0];
var myMasterSpreads = myDoc.masterSpreads.everyItem().getElements();
for(var m=0;m<myMasterSpreads.length;m++){
var currentMasterPages = myMasterSpreads
.pages.everyItem().getElements();
for(var n=0;n<currentMasterPages.length;n++){
if(currentMasterPages
.appliedMaster === null){ $.writeln(m+"\t"+n+"\t"+String(currentMasterPages
.appliedMaster)); };
else{$.writeln(m+"\t"+n+"\t"+currentMasterPages
.appliedMaster.name)}; }
};
/*
Result:
0 0 null
0 1 null
1 0 A-Musterseite
1 1 C-Musterseite
2 0 null
2 1 null
*/
Uwe
Copy link to clipboard
Copied
<Groan>
Have just replicated Uwe's issue on a Mac OS 10.9.5 running IDCC2014 v10.2.0.69, so happens there too.
For what it's worth, i've never seen a master page where the recto page is based on another master, but the verso pages is based on a separate master. It took me a moment to realise that this is a simple drag and drop in the master pages portion of the pages palette, but it was a first for me. Nevertheless, anyone who has created master pages like that, and uses the script linked to in post 7 of this thread will experience the same issue.
Copy link to clipboard
Copied
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 )

