Skip to main content
March 11, 2012
Answered

Script for deleting Hidden Layers which are not used in any Layer Comp?

  • March 11, 2012
  • 3 replies
  • 15423 views

Hello.

I've turend the web upside down searching for a script that would do the following:

  1. Search through all the layer comps in the document
  2. Check which layers are hidden on all Layer Comps
  3. Remove/mark with color the layers found.

If anybody can provide a script that would do such a thing - it would be highly appreciated.

Regards,

Fade.

This topic has been closed for replies.
Correct answer Paul Riggott

This should mark those layers Red...

function main(){
if(!documents.length) return;
var doc = activeDocument;
selectAllLayers();
var layersSelected=getSelectedLayersIdx();
var layerIDs=[];
for(var d =0;d<layersSelected.length;d++){ layerIDs.push([[layersSelected],["N"]]);}
for( var c = 0; c < doc.layerComps.length; c++ ){
      doc.layerComps.apply();
      for(var z in layersSelected){
          if(getLayerVisibilityByIndex( Number(layersSelected))){
              for(var s in layerIDs){
                  if(Number(layersSelected) == Number(layerIDs[0])){
                      layerIDs[1] = "Y";
                      break;
                      }
                  }
              }
          }
}
for(var l in layerIDs){
    if(layerIDs[1].toString() == "N") {
       selectLayerByIndex(Number(layerIDs[0]),false);
       layerColour("Red");
        }
    }
}
main();
function layerColour(colour) {
    switch (colour.toLocaleLowerCase()){
        case 'red': colour = 'Rd  '; break;
        case 'orange' : colour = 'Orng'; break;
        case 'yellow' : colour = 'Ylw '; break;
        case 'yellow' : colour = 'Ylw '; break;
        case 'green' : colour = 'Grn '; break;
        case 'blue' : colour = 'Bl  '; break;
        case 'violet' : colour = 'Vlt '; break;
        case 'gray' : colour = 'Gry '; break;
        case 'none' : colour = 'None'; break;
        default : colour = 'None'; break;
        }
    var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    desc.putReference( charIDToTypeID('null'), ref );
        var desc2 = new ActionDescriptor();
        desc2.putEnumerated( charIDToTypeID('Clr '), charIDToTypeID('Clr '), charIDToTypeID(colour) );
    desc.putObject( charIDToTypeID('T   '), charIDToTypeID('Lyr '), desc2 );
    executeAction( charIDToTypeID('setd'), desc, DialogModes.NO );
};
function getLayerVisibilityByIndex( idx ) {
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "Vsbl" ));
ref.putIndex( charIDToTypeID( "Lyr " ), idx );
return executeActionGet(ref).getBoolean(charIDToTypeID( "Vsbl" ));
};
function selectLayerByIndex(index,add){
   add = (add == undefined)  ? add = false : add;
var ref = new ActionReference();
    ref.putIndex(charIDToTypeID("Lyr "), index);
    var desc = new ActionDescriptor();
    desc.putReference(charIDToTypeID("null"), ref );
         if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
      desc.putBoolean( charIDToTypeID( "MkVs" ), false );
     try{
    executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){}
};

function selectAllLayers(layer) {
if(layer == undefined) layer = 0;
topLayer=true;
if(!activeDocument.layers[0].visible){
    topLayer=false;
    }
activeDocument.activeLayer = activeDocument.layers[activeDocument.layers.length-1];
if(activeDocument.activeLayer.isBackgroundLayer)
if(!activeDocument.layers[activeDocument.layers.length-2].visible){
activeDocument.activeLayer = activeDocument.layers[activeDocument.layers.length-2];
activeDocument.activeLayer.visible=false;
}else{
    activeDocument.activeLayer = activeDocument.layers[activeDocument.layers.length-2];
    }
var BL = activeDocument.activeLayer.name;
activeDocument.activeLayer = activeDocument.layers[layer];
    var desc5 = new ActionDescriptor();
        var ref3 = new ActionReference();
        ref3.putName( charIDToTypeID('Lyr '), BL);
    desc5.putReference( charIDToTypeID('null'), ref3 );
    desc5.putEnumerated( stringIDToTypeID('selectionModifier'), stringIDToTypeID('selectionModifierType'), stringIDToTypeID('addToSelectionContinuous') );
    desc5.putBoolean( charIDToTypeID('MkVs'), false );
    executeAction( charIDToTypeID('slct'), desc5, DialogModes.NO );
if(!topLayer) activeDocument.layers[0].visible=false;
};
function getSelectedLayersIdx(){
   var selectedLayers = new Array;
   var ref = new ActionReference();
   ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
   var desc = executeActionGet(ref);
   if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
      desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
       var c = desc.count
       var selectedLayers = new Array();
       for(var i=0;i<c;i++){
         try{
            activeDocument.backgroundLayer;
            selectedLayers.push(  desc.getReference( i ).getIndex() );
         }catch(e){
            selectedLayers.push(  desc.getReference( i ).getIndex()+1 );
         }
       }
    }else{
      var ref = new ActionReference();
      ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));
      ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
      try{
         activeDocument.backgroundLayer;
         selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1);
      }catch(e){
         selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));
      }
   }
   return selectedLayers;
};


3 replies

Hypermind
Participant
November 16, 2018

Hi, I'm a little late to this post, but the script here works great.

However, I'm wondering if it's possible to have a script that would instead go through all layers & smart objects and delete hidden layers.

That would be amazing if possible?

Thanks

Chris

Stephen Marsh
Community Expert
Community Expert
November 17, 2018

Chris, scripts previously posted in this topic do just that, go back to the top of the thread and work your way down (post #5 and or #8).

Re: Script for deleting Hidden Layers which are not used in any Layer Comp?

Re: Script for deleting Hidden Layers which are not used in any Layer Comp?

Prepression: Downloading and Installing Adobe Scripts

Hypermind
Participant
November 19, 2018

Thanks for your reply Stephen, however I have tried both of these scripts, and both delete all layers within a PSD, regardless of whether there are smart objects with layers visible or not, and it also deletes visible layers that are not part of any smart objects.

What I was hoping for, was a script that would only delete hidden layers, both standard layers in a PSD and those that sit within smart objects.

Edit: Just to be clear I am not using layer comps

Cheers

Chris

Participating Frequently
September 24, 2014

Thanks for this great script.

A thought on an improvement: it seems that if you have a layer group set to visible with all of its layers turned off, the delete script leaves these layers intact. Is there any way to script this at the layer level as well, or could this be a bug?

Running Photoshop CC 2014

Michael

JJMack
Community Expert
Community Expert
September 25, 2014

Paul does not visit here any more he is missed.

JJMack
Paul Riggott
Paul RiggottCorrect answer
Inspiring
March 11, 2012

This should mark those layers Red...

function main(){
if(!documents.length) return;
var doc = activeDocument;
selectAllLayers();
var layersSelected=getSelectedLayersIdx();
var layerIDs=[];
for(var d =0;d<layersSelected.length;d++){ layerIDs.push([[layersSelected],["N"]]);}
for( var c = 0; c < doc.layerComps.length; c++ ){
      doc.layerComps.apply();
      for(var z in layersSelected){
          if(getLayerVisibilityByIndex( Number(layersSelected))){
              for(var s in layerIDs){
                  if(Number(layersSelected) == Number(layerIDs[0])){
                      layerIDs[1] = "Y";
                      break;
                      }
                  }
              }
          }
}
for(var l in layerIDs){
    if(layerIDs[1].toString() == "N") {
       selectLayerByIndex(Number(layerIDs[0]),false);
       layerColour("Red");
        }
    }
}
main();
function layerColour(colour) {
    switch (colour.toLocaleLowerCase()){
        case 'red': colour = 'Rd  '; break;
        case 'orange' : colour = 'Orng'; break;
        case 'yellow' : colour = 'Ylw '; break;
        case 'yellow' : colour = 'Ylw '; break;
        case 'green' : colour = 'Grn '; break;
        case 'blue' : colour = 'Bl  '; break;
        case 'violet' : colour = 'Vlt '; break;
        case 'gray' : colour = 'Gry '; break;
        case 'none' : colour = 'None'; break;
        default : colour = 'None'; break;
        }
    var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    desc.putReference( charIDToTypeID('null'), ref );
        var desc2 = new ActionDescriptor();
        desc2.putEnumerated( charIDToTypeID('Clr '), charIDToTypeID('Clr '), charIDToTypeID(colour) );
    desc.putObject( charIDToTypeID('T   '), charIDToTypeID('Lyr '), desc2 );
    executeAction( charIDToTypeID('setd'), desc, DialogModes.NO );
};
function getLayerVisibilityByIndex( idx ) {
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "Vsbl" ));
ref.putIndex( charIDToTypeID( "Lyr " ), idx );
return executeActionGet(ref).getBoolean(charIDToTypeID( "Vsbl" ));
};
function selectLayerByIndex(index,add){
   add = (add == undefined)  ? add = false : add;
var ref = new ActionReference();
    ref.putIndex(charIDToTypeID("Lyr "), index);
    var desc = new ActionDescriptor();
    desc.putReference(charIDToTypeID("null"), ref );
         if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
      desc.putBoolean( charIDToTypeID( "MkVs" ), false );
     try{
    executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){}
};

function selectAllLayers(layer) {
if(layer == undefined) layer = 0;
topLayer=true;
if(!activeDocument.layers[0].visible){
    topLayer=false;
    }
activeDocument.activeLayer = activeDocument.layers[activeDocument.layers.length-1];
if(activeDocument.activeLayer.isBackgroundLayer)
if(!activeDocument.layers[activeDocument.layers.length-2].visible){
activeDocument.activeLayer = activeDocument.layers[activeDocument.layers.length-2];
activeDocument.activeLayer.visible=false;
}else{
    activeDocument.activeLayer = activeDocument.layers[activeDocument.layers.length-2];
    }
var BL = activeDocument.activeLayer.name;
activeDocument.activeLayer = activeDocument.layers[layer];
    var desc5 = new ActionDescriptor();
        var ref3 = new ActionReference();
        ref3.putName( charIDToTypeID('Lyr '), BL);
    desc5.putReference( charIDToTypeID('null'), ref3 );
    desc5.putEnumerated( stringIDToTypeID('selectionModifier'), stringIDToTypeID('selectionModifierType'), stringIDToTypeID('addToSelectionContinuous') );
    desc5.putBoolean( charIDToTypeID('MkVs'), false );
    executeAction( charIDToTypeID('slct'), desc5, DialogModes.NO );
if(!topLayer) activeDocument.layers[0].visible=false;
};
function getSelectedLayersIdx(){
   var selectedLayers = new Array;
   var ref = new ActionReference();
   ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
   var desc = executeActionGet(ref);
   if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
      desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
       var c = desc.count
       var selectedLayers = new Array();
       for(var i=0;i<c;i++){
         try{
            activeDocument.backgroundLayer;
            selectedLayers.push(  desc.getReference( i ).getIndex() );
         }catch(e){
            selectedLayers.push(  desc.getReference( i ).getIndex()+1 );
         }
       }
    }else{
      var ref = new ActionReference();
      ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));
      ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
      try{
         activeDocument.backgroundLayer;
         selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1);
      }catch(e){
         selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));
      }
   }
   return selectedLayers;
};


March 11, 2012

Thanks for your quick reply, Paul!

What extension should I save this file with? .js?

Paul Riggott
Inspiring
March 11, 2012

The norm is ".jsx" the script will require Photoshop CS3 or better.

It's best to use ExtendScript Toolkit (this gets installed with Photoshop) to copy the code and save it out.