Hello everybody.
I have an old script that I have used many times to remove unused layers. Until today I thought it worked well:
var layers = app.documents[0].layers.everyItem().getElements();for(var i=layers.length-1;i>=0;i--){ if(layers[i].pageItems.length==0){ layers[i].remove(); }}
However, today I used the script on a document that had a layer that was only ever used once on a master page that the other master pages were based upon. To my surprise, when the script was run, this specific layer was deleted.
my scripting knowledge is poor, but looking at the script it looks like the script is looking at all layers that have anything in them, and any layer that has no item is to be deleted. however, I think that pageItems only refers to regular pages or master pages, not masters BASED ON other masters.
I think the problem is similar to another that I had recently where I had a script that removed unused masters, but removed masters that other masters were based on, and I think the principles are the same. That thread can be found at:
http://forums.adobe.com/message/5922004#5922004
in short, the script that worked there was as follows:
xUnusedMasters(myDoc);
function xUnusedMasters( docRef ) {
var mpNames = new Object () ;
var allPages = docRef.pages ;for ( var pIndex = allPages.length - 1 ; pIndex >= 0 ; pIndex-- ) {
mpNames = addMasterName ( mpNames , allPages[pIndex] ) ;}
var allMasterPages = docRef.masterSpreads ;for ( var mpIndex = allMasterPages.length - 1 ; mpIndex >= 0 ; mpIndex-- ) {if ( ! mpNames[allMasterPages[mpIndex].name] ) {
allMasterPages[mpIndex].remove() ;}}return ;
function addMasterName ( MPDB , pageRef ) {if ( pageRef == null ) {return MPDB ; }if ( pageRef.constructor.name == 'MasterSpread' ) {
MPDB[pageRef.name] = true ;}return addMasterName ( MPDB , pageRef.appliedMaster ) ;}}
is it possible to modify the above script so that instead of removing unused master pages, it removes unused layers in a similar fashion?
Many thanks,
Colly