Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


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

13.1K

Likes

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){        ...

Likes

Translate

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


Likes

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?

Likes

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.

Likes

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?

Likes

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


Likes

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!

Likes

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?

Likes

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


Likes

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!

Likes

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.

Likes

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!

Likes

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

Likes

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
Adobe Community Professional ,
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

Likes

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
Enthusiast ,
Nov 03, 2021 Nov 03, 2021

Copy link to clipboard

Copied

LATEST

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

Likes

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

Likes

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

Likes

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
Adobe Community Professional ,
Sep 24, 2014 Sep 24, 2014

Copy link to clipboard

Copied

Paul does not visit here any more he is missed.

JJMack

Likes

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
Enthusiast ,
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

Likes

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

Likes

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
Adobe Community Professional ,
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

Likes

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

Likes

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
Adobe Community Professional ,
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.

Likes

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