Skip to main content
Retsied
Known Participant
December 2, 2022
Answered

Javascript to hide all art layers

  • December 2, 2022
  • 3 replies
  • 3215 views

Trying to quickly hide all art layers, then show the top layer only. Below is what I have tried, none of which works. Ideally would like to like to avoid a loop to index each layer indivdually and hide it, seems like there has to be a way to select all and hide. 

 

#target photoshop  
function main(){  
if(!documents.length) return;  
var startRulerUnits = app.preferences.rulerUnits;  
app.preferences.rulerUnits = Units.PIXELS; 
var doc = activeDocument;  
var res = doc.resolution;  

var layers = app.activeDocument.layers;
for (var i =0; i<layers.length; i++) {
  layers.visible = false;
}
doc.artLayers.visible = false;
doc.artLayers.layers.visible = false; // "returns no such component"
doc.activeLayer = doc.layers[0]
doc.activeLayer.visible = true;

}  
main();  

 

This topic has been closed for replies.
Correct answer Stephen Marsh

Would you mind dropping your loop to individually turn each layer off? 


Here it is in a loop over all top-level layers:

 

#target photoshop

function main() {
  
    activeDocument.activeLayer = activeDocument.layers[activeDocument.layers.length - 1];

    // Loop forward over top level layers
    // for (var i = 0; i < activeDocument.layers.length; i++) {
    // Loop backward over top level layers
    for (var i = activeDocument.layers.length - 1; i >= 0; i--) {
        try {
            activeDocument.activeLayer = activeDocument.layers[i];
            activeDocument.activeLayer.visible = false;
        } catch (e) { }
    }
    activeDocument.activeLayer = activeDocument.layers[0];
    activeDocument.activeLayer.visible = true;
}
activeDocument.suspendHistory("Undo Script...", "main()");

3 replies

Stephen Marsh
Community Expert
Community Expert
December 6, 2022

@Retsied 

 

OK, you obviously liked the for loop over all top level layers/groups... But what about the other option that I posted?

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/javascript-to-hide-all-art-layers/td-p/13389308#U13395052

 

Retsied
RetsiedAuthor
Known Participant
December 6, 2022

Both technically work but I'd like to actually explicitly turn off layers instead of toggling visibility so the loop works better for me in this case and isn't too slow as I only have about 20 or so layers. I originally wanted to avoid a loop since I'm not sure how many I will have at the end and was hoping there was an easy way to do this like (doc.activeLayers.layers.all.visible = false, or something) but so far works fine. Thanks for the help

Retsied
RetsiedAuthor
Known Participant
December 6, 2022

doc.artLayers.layers.all.visible = false

c.pfaffenbichler
Community Expert
Community Expert
December 2, 2022

So Adjustment Layers and Groups should stay visible? 

Are you aware that Layers in Groups are filial to the Groups, not the Document? (Edit: in DOM, that is.)

 

Addressing large numbers of Layers via DOM is usually slow and you may want to switch to AM code for this. 

 

Retsied
RetsiedAuthor
Known Participant
December 2, 2022

I don't actually have any Adjustment Layers or Groups, so in this case is actually fine to hide all layers

Stephen Marsh
Community Expert
Community Expert
December 2, 2022
quote

I don't actually have any Adjustment Layers or Groups, so in this case is actually fine to hide all layers


By @Retsied

 

 

That simplifies and changes everything then!

 

//activeDocument.activeLayer = activeDocument.layers[activeDocument.layers.length - 1];
activeDocument.activeLayer = activeDocument.layers[0];
toggleLayerVisibility(true);


function toggleLayerVisibility(toggleOptionsPalette) {
    var s2t = function (s) {
        return app.stringIDToTypeID(s);
    };
    var descriptor = new ActionDescriptor();
    var list = new ActionList();
    var reference = new ActionReference();
    reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
    list.putReference(reference);
    descriptor.putList(s2t("null"), list);
    descriptor.putBoolean(s2t("toggleOptionsPalette"), toggleOptionsPalette);
    executeAction(s2t("show"), descriptor, DialogModes.NO);
}

 

Mylenium
Legend
December 2, 2022

You're merely blindly setting attributes but there's no actual conditionals testing the visibility, so I'm not sure what that would even achieve. I'm not into PS scripting, but this just seems wrong on the most abstract level. i also think you realyl need to assign the attributes in a loop, not globally fire them blindly.

 

Mylenium

Retsied
RetsiedAuthor
Known Participant
December 2, 2022

Mylenium, not sure I know what you mean. 

The same attribute works to toggle layer visibility by name:

 

doc.artLayers["layer name"].visible = false;