• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

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

Guest
Mar 11, 2012 Mar 11, 2012

Copy link to clipboard

Copied

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.

TOPICS
Actions and scripting

Views

14.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Valorous Hero , Mar 11, 2012 Mar 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){
                  i

...

Votes

Translate

Translate
Adobe
Valorous Hero ,
Mar 11, 2012 Mar 11, 2012

Copy link to clipboard

Copied

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;
};


Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 11, 2012 Mar 11, 2012

Copy link to clipboard

Copied

Thanks for your quick reply, Paul!

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Mar 11, 2012 Mar 11, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 11, 2012 Mar 11, 2012

Copy link to clipboard

Copied

Great! It works!

Now what is the change needs to be done in order for the layers to be deleted instead of marked in red?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Mar 11, 2012 Mar 11, 2012

Copy link to clipboard

Copied

This script will delete those layers....

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;
                      }
                  }
              }
          }
}
var toDelete=[];
for(var l in layerIDs){
    if(layerIDs[1].toString() == "N") {
       toDelete.push(getIDX(Number(layerIDs[0])));
        }
    }for(var t in toDelete){
        selLayer(Number(toDelete));
        doc.activeLayer.remove();
        }
}
main();
function getIDX(idx) {
    var ref = new ActionReference();
    ref.putProperty( charIDToTypeID("Prpr") , stringIDToTypeID( "layerID" ));
    ref.putIndex( charIDToTypeID( "Lyr " ), idx );
    return executeActionGet(ref).getInteger(stringIDToTypeID( "layerID" ));
}

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 getLayerItemIndexByLayerID(id) {
    var ref = new ActionReference();
    ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));
    ref.putIdentifier( charIDToTypeID( "Lyr " ), id );
try{
    return executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ));
}catch(e){return true;}
};
function selLayer(layerID,add){
var result =getLayerItemIndexByLayerID(layerID);
if(result > 0){
    try{
    activeDocument.backgroundLayer;
    var bkGround = 1;
    }catch(e) {var bkGround = 0;}
selectLayerByIndex(result - bkGround ,add);
}else{
alert("Layer does not exist"); 
  }
};
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;
};


Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 11, 2012 Mar 11, 2012

Copy link to clipboard

Copied

Thank you so much!

You've have just made my time so much more productive!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 12, 2012 Mar 12, 2012

Copy link to clipboard

Copied

Additional question.

After the script has finished its' business (which id does perfectly - thanks again!), there are lots of empty folders left.

Is there a way to add a part that would get rid of those too?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Mar 12, 2012 Mar 12, 2012

Copy link to clipboard

Copied

That's a bit tricky as there could be nested folders that are empty.

I have added the code so that it will do three passes on layersets this can be increased if required, hope this ok.

Also added an alert to say that it has finished.

If you deal with a lot of layercomps, there are a couple of scripts here that might be of use to you?

http://www.ps-scripts.com/bb/viewtopic.php?f=9&t=4681&sid=42f9afd11302c4cffa4bf8f265ea7bf5

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;
                      }
                  }
              }
          }
}
var toDelete=[];
for(var l in layerIDs){
    if(layerIDs[1].toString() == "N") {
       toDelete.push(getIDX(Number(layerIDs[0])));
        }
    }for(var t in toDelete){
        selLayer(Number(toDelete));
        doc.activeLayer.remove();
        }
removeEmptyLayerSets();
alert("All Done");
}
main();
function removeEmptyLayerSets(){
var numberOfPasses=3;
for(var o = 0;o<numberOfPasses;o++){
selectAllLayers();
var sel = getSelectedLayersIdx();
var layerSets= [];
for (var a in sel){
if( isLayerSet( Number(sel) )){
    layerSets.push(getIDX(Number(sel
) ));
    }
}
for(var p in layerSets){
         selLayer(Number(layerSets

),false);
if(activeDocument.activeLayer.layers.length == 0){
    try{
   if(activeDocument.activeLayer.typename == "LayerSet") activeDocument.activeLayer.remove();
   }catch(e){}
    }
}
}
}
function getIDX(idx) {
    var ref = new ActionReference();
    ref.putProperty( charIDToTypeID("Prpr") , stringIDToTypeID( "layerID" ));
    ref.putIndex( charIDToTypeID( "Lyr " ), idx );
    return executeActionGet(ref).getInteger(stringIDToTypeID( "layerID" ));
}
function isLayerSet(idx){
  var propName = stringIDToTypeID( 'layerSection' );// can't replace
   var ref = new ActionReference();
   ref.putProperty( 1349677170 , propName);
   ref.putIndex( 1283027488, idx );
   var desc =  executeActionGet( ref );
   var type = desc.getEnumerationValue( propName );
   var res = typeIDToStringID( type );
   return res == 'layerSectionStart' ? true:false;
}
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 getLayerItemIndexByLayerID(id) {
    var ref = new ActionReference();
    ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));
    ref.putIdentifier( charIDToTypeID( "Lyr " ), id );
try{
    return executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ));
}catch(e){return true;}
};
function selLayer(layerID,add){
var result =getLayerItemIndexByLayerID(layerID);
if(result > 0){
    try{
    activeDocument.backgroundLayer;
    var bkGround = 1;
    }catch(e) {var bkGround = 0;}
selectLayerByIndex(result - bkGround ,add);
}else{
alert("Layer does not exist"); 
  }
};
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() {
    var desc29 = new ActionDescriptor();
        var ref23 = new ActionReference();
        ref23.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    desc29.putReference( charIDToTypeID('null'), ref23 );
    executeAction( stringIDToTypeID('selectAllLayers'), desc29, DialogModes.NO );
}
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;
};


Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 12, 2012 Mar 12, 2012

Copy link to clipboard

Copied

Perfect!

Thank you very very much!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 08, 2013 Aug 08, 2013

Copy link to clipboard

Copied

Thank you very much for providing such a useful script.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 03, 2014 Jun 03, 2014

Copy link to clipboard

Copied

Thanks for creating this, it is very useful!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 06, 2017 May 06, 2017

Copy link to clipboard

Copied

Hi,
After generating the text to jsx. file and paste it in the script folder,
I opened psd file and tried to run the script ( selection first).

I keep getting this error message:
What am i doing wrong?
Thanks
Yuval.

Screen Shot 2017-05-06 at 22.54.26.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 06, 2017 May 06, 2017

Copy link to clipboard

Copied

Open your script file in a test editor  capture the screen and post the screen capture,  I think you messed up creating the script.  Line 1 should be

  1. function main(){ 

that function is used in line 33

         33.  main();

I do not see the string FasdUAS in the thread????

JJMack

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Nov 03, 2021 Nov 03, 2021

Copy link to clipboard

Copied

I thought i add the working version again here, all scripts added before the forum redesign are broken.
This script works for me, only small issue is when layers are clipped to layers below. SOmetimes it causes an issue them not being deleted.

// https://community.adobe.com/t5/photoshop/script-for-deleting-hidden-layers-which-are-not-used-in-any-layer-comp/m-p/4000805
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[d]],
            ["N"]
        ]);
    }
    for (var c = 0; c < doc.layerComps.length; c++) {
        doc.layerComps[c].apply();
        for (var z in layersSelected) {
            if (getLayerVisibilityByIndex(Number(layersSelected[z]))) {
                for (var s in layerIDs) {
                    if (Number(layersSelected[z]) == Number(layerIDs[s][0])) {
                        layerIDs[s][1] = "Y";
                        break;
                    }
                }
            }
        }
    }
    var toDelete = [];
    for (var l in layerIDs) {
        if (layerIDs[l][1].toString() == "N") {
            toDelete.push(getIDX(Number(layerIDs[l][0])));
        }
    }
    for (var t in toDelete) {
        selLayer(Number(toDelete[t]));
        doc.activeLayer.remove();
    }
    removeEmptyLayerSets();
    alert("All Done");
}
main();

function removeEmptyLayerSets() {
    var numberOfPasses = 3;
    for (var o = 0; o < numberOfPasses; o++) {
        selectAllLayers();
        var sel = getSelectedLayersIdx();
        var layerSets = [];
        for (var a in sel) {
            if (isLayerSet(Number(sel[a]))) {
                layerSets.push(getIDX(Number(sel[a])));
            }
        }
        for (var p in layerSets) {
            selLayer(Number(layerSets[p]), false);
            if (activeDocument.activeLayer.layers.length == 0) {
                try {
                    if (activeDocument.activeLayer.typename == "LayerSet") activeDocument.activeLayer.remove();
                } catch (e) {}
            }
        }
    }
}

function getIDX(idx) {
    var ref = new ActionReference();
    ref.putProperty(charIDToTypeID("Prpr"), stringIDToTypeID("layerID"));
    ref.putIndex(charIDToTypeID("Lyr "), idx);
    return executeActionGet(ref).getInteger(stringIDToTypeID("layerID"));
}

function isLayerSet(idx) {
    var propName = stringIDToTypeID('layerSection'); // can't replace  
    var ref = new ActionReference();
    ref.putProperty(1349677170, propName);
    ref.putIndex(1283027488, idx);
    var desc = executeActionGet(ref);
    var type = desc.getEnumerationValue(propName);
    var res = typeIDToStringID(type);
    return res == 'layerSectionStart' ? true : false;
}

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 getLayerItemIndexByLayerID(id) {
    var ref = new ActionReference();
    ref.putProperty(charIDToTypeID("Prpr"), charIDToTypeID("ItmI"));
    ref.putIdentifier(charIDToTypeID("Lyr "), id);
    try {
        return executeActionGet(ref).getInteger(charIDToTypeID("ItmI"));
    } catch (e) {
        return true;
    }
};

function selLayer(layerID, add) {
    var result = getLayerItemIndexByLayerID(layerID);
    if (result > 0) {
        try {
            activeDocument.backgroundLayer;
            var bkGround = 1;
        } catch (e) {
            var bkGround = 0;
        }
        selectLayerByIndex(result - bkGround, add);
    } else {
        alert("Layer does not exist");
    }
};

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() {
    var desc29 = new ActionDescriptor();
    var ref23 = new ActionReference();
    ref23.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
    desc29.putReference(charIDToTypeID('null'), ref23);
    executeAction(stringIDToTypeID('selectAllLayers'), desc29, DialogModes.NO);
}

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;
};

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 06, 2014 Jan 06, 2014

Copy link to clipboard

Copied

So I've got an issue where in I have a .PSD file with hundreds upon hundred that that is 3.3gb when open, running in PS CS6.  When I run either the mark red or delete script, it looks like it selects the entire list of layers, but then freezes. I 've opened task manager and the amount of processor usage varies, so I'm thinking it might be doing something but after just now letting it run for a good 30 minutes, no result.

Am I out of luck with regard to using to using either of your scripts or do you think there might be something I'm missing? Just for giggles I've tried both running the script by placing it into the "presets/scripts" folder and simply browsing to where I saved it from the extend script tool.

Any thoughts would be very much appreciated.

Cheers,

Patrick

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 24, 2014 Sep 24, 2014

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 24, 2014 Sep 24, 2014

Copy link to clipboard

Copied

Paul does not visit here any more he is missed.

JJMack

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jan 16, 2020 Jan 16, 2020

Copy link to clipboard

Copied

Ive been using this one for years. THough my version is a little different. I guess the new FOrum also wrecked most scripts in the loops.


My version does delete all hidden folders as well

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 16, 2018 Nov 16, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 16, 2018 Nov 16, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 19, 2018 Nov 19, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 19, 2018 Nov 19, 2018

Copy link to clipboard

Copied

Ah, thanks for clarifying Chris, as this topic is all about layer comps, I was of course focused on that aspect. I’d suggest that you start a new topic dedicated to your different but related question.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 15, 2022 Mar 15, 2022

Copy link to clipboard

Copied

EDIT
Sorry just read you dont use layer comps. Easy fix to get this one to work. Simply make 1 layercomp when your design is done, than run this script. It will work as intended and not delete those items you talked about.

 

Earlier response

Did you try my version?

Ive used this one since i updated and i dont have any issue other than that clipped items sometimes are kept.
I useSmartObjects a lot, i always linkin images and use the SmartObjects in 98% of my files.

I use layercomps when i make designs and then when final version is chosen i clean this file and save it as a new version. I dont have any issues with SmartObjects being deleted.

 

My latest adition removes the history so its faster and saves memory, it also enters fullscreen so it doesnt need to redraw the UI.

Have a try and let me know if this does work

 

// https://community.adobe.com/t5/photoshop/script-for-deleting-hidden-layers-which-are-not-used-in-any-layer-comp/m-p/4000805
function main() {
    if (!documents.length) return;
    // Enter fullscreen so app doesn tneed to redraw UI
    app.runMenuItem(stringIDToTypeID('screenModeFullScreen'));

    var doc = activeDocument;
    selectAllLayers();
    var layersSelected = getSelectedLayersIdx();
    var layerIDs = [];
    for (var d = 0; d < layersSelected.length; d++) {
        layerIDs.push([
            [layersSelected[d]],
            ["N"]
        ]);
    }
    for (var c = 0; c < doc.layerComps.length; c++) {
        doc.layerComps[c].apply();
        for (var z in layersSelected) {
            if (getLayerVisibilityByIndex(Number(layersSelected[z]))) {
                for (var s in layerIDs) {
                    if (Number(layersSelected[z]) == Number(layerIDs[s][0])) {
                        layerIDs[s][1] = "Y";
                        break;
                    }
                }
            }
        }
    }
    var toDelete = [];
    for (var l in layerIDs) {
        if (layerIDs[l][1].toString() == "N") {
            toDelete.push(getIDX(Number(layerIDs[l][0])));
        }
    }
    for (var t in toDelete) {
        selLayer(Number(toDelete[t]));
        doc.activeLayer.remove();
    }
    removeEmptyLayerSets();
    
    // Return to standard screen mode
    app.runMenuItem(stringIDToTypeID('screenModeStandard'));
    
    alert("All Done");
}

// main();
// 2022-03-15
// Added
// hide historgram to speed up + fullscreen mode
app.activeDocument.suspendHistory("Delete Non-used items", 'main()');

function removeEmptyLayerSets() {
    var numberOfPasses = 3;
    for (var o = 0; o < numberOfPasses; o++) {
        selectAllLayers();
        var sel = getSelectedLayersIdx();
        var layerSets = [];
        for (var a in sel) {
            if (isLayerSet(Number(sel[a]))) {
                layerSets.push(getIDX(Number(sel[a])));
            }
        }
        for (var p in layerSets) {
            selLayer(Number(layerSets[p]), false);
            if (activeDocument.activeLayer.layers.length == 0) {
                try {
                    if (activeDocument.activeLayer.typename == "LayerSet") activeDocument.activeLayer.remove();
                } catch (e) {}
            }
        }
    }
}

function getIDX(idx) {
    var ref = new ActionReference();
    ref.putProperty(charIDToTypeID("Prpr"), stringIDToTypeID("layerID"));
    ref.putIndex(charIDToTypeID("Lyr "), idx);
    return executeActionGet(ref).getInteger(stringIDToTypeID("layerID"));
}

function isLayerSet(idx) {
    var propName = stringIDToTypeID('layerSection'); // can't replace  
    var ref = new ActionReference();
    ref.putProperty(1349677170, propName);
    ref.putIndex(1283027488, idx);
    var desc = executeActionGet(ref);
    var type = desc.getEnumerationValue(propName);
    var res = typeIDToStringID(type);
    return res == 'layerSectionStart' ? true : false;
}

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 getLayerItemIndexByLayerID(id) {
    var ref = new ActionReference();
    ref.putProperty(charIDToTypeID("Prpr"), charIDToTypeID("ItmI"));
    ref.putIdentifier(charIDToTypeID("Lyr "), id);
    try {
        return executeActionGet(ref).getInteger(charIDToTypeID("ItmI"));
    } catch (e) {
        return true;
    }
};

function selLayer(layerID, add) {
    var result = getLayerItemIndexByLayerID(layerID);
    if (result > 0) {
        try {
            activeDocument.backgroundLayer;
            var bkGround = 1;
        } catch (e) {
            var bkGround = 0;
        }
        selectLayerByIndex(result - bkGround, add);
    } else {
        alert("Layer does not exist");
    }
};

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() {
    var desc29 = new ActionDescriptor();
    var ref23 = new ActionReference();
    ref23.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
    desc29.putReference(charIDToTypeID('null'), ref23);
    executeAction(stringIDToTypeID('selectAllLayers'), desc29, DialogModes.NO);
}

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;
};

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 15, 2022 Mar 15, 2022

Copy link to clipboard

Copied

LATEST

On second thought, you dont have actually quite a simple method. If you use the layer panel filter options. You can show only hidden then delete them. If you then select all and delete them. You can basically make this yourself with scriptlistner

 

I found this as i was researching it a bit. I tried scrriptlistener but it does show items from the filter menu. They are not exposed enough to be usefull.

 

Someone else had the same question in this forum post

 

Ive set the author to the poster of that thread, for me the script works
After checking photoshop, its the same code when you run Headermenu . Layers > Delete > Delete hidden. Which means you can also make it an action and give it a shortcut if needed

 

If you need to also delete nested layers inside smartobjects, that becomes a bit more difficult. You could try making a function which loops over itself. 
delete hidden menu commanddelete hidden menu command

 

 

 

// Delete Hidden Layers
// jaroslav_bereza
// source: https://feedback-readonly.photoshop.com/conversations/photoshop/script-is-there-way-how-to-get-filtered-layers-like-from-layers-panel-filter/5f5f45a24b561a3d4257023a
function deleteHiddenLayers() {
    var idDlt = charIDToTypeID("Dlt "); // <- ordinary delete
    var desc213 = new ActionDescriptor();
    var idnull = charIDToTypeID("null");
    var ref110 = new ActionReference();
    var idLyr = charIDToTypeID("Lyr ");
    var idOrdn = charIDToTypeID("Ordn");
    var idhidden = stringIDToTypeID("hidden"); // <- there was usually "Trgt" for selected layers, but now there is "hidden" is this some magic word for filtering?
    ref110.putEnumerated(idLyr, idOrdn, idhidden);
    desc213.putReference(idnull, ref110);
    executeAction(idDlt, desc213, DialogModes.NO);
}

deleteHiddenLayers()

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines