Skip to main content
Inspiring
November 29, 2023
Answered

Hide layers (inside subgroups) if it's name start with "vis_"

  • November 29, 2023
  • 2 replies
  • 1496 views

I want to hide all layers ( inside subgroups) if the  name start with "vis_"
This works with specific layer names, I tried to adapt with indexOf, no luck...

 

function toggleVisibility(layerName) {
    var desc11 = new ActionDescriptor();
    var idnull = stringIDToTypeID("null");
    var list6 = new ActionList();
    var ref8 = new ActionReference();
    var idlayer = stringIDToTypeID("layer");
    ref8.putName(idlayer, layerName);
    list6.putReference(ref8);
    desc11.putList(idnull, list6);
    var desc = new ActionDescriptor()
    var ref = new ActionReference();

    var idlayer = stringIDToTypeID("layer");
    ref = new ActionReference();
    ref.putName(idlayer, layerName);
    var desc = executeActionGet(ref);
    var vis = desc.getBoolean(charIDToTypeID("Vsbl"));

    var idhide = stringIDToTypeID("hide");
        executeAction(idhide, desc11, DialogModes.NO);
     
}

toggleVisibility('Layer 1');

 

 

This topic has been closed for replies.
Correct answer siomosp

Please comment out or delete the line

app.activeDocument.activeLayer = curLay;

and replace the line beginning with

if(curLay.name.indexOf ...

with this line:

if( (curLay.name.indexOf("vis_") >= 0) && (curLay.typename != "LayerSet") ){

 


Hi,  the modified script works fine, thank you!!

 

 

iterLayers(app.activeDocument);

function iterLayers(l) {
    for(var i=0; i<l.layers.length; i++) {
        var curLay = l.layers[i];
 //       app.activeDocument.activeLayer = curLay;

        if(curLay.typename == "LayerSet") {
            iterLayers(curLay);
        } else {
       if( (curLay.name.indexOf("vis_") >= 0) && (curLay.typename != "LayerSet") ){
                curLay.visible = false;
            }
        }
    }
}

 

 

2 replies

GNDGN
Inspiring
November 30, 2023

This does the job:

iterLayers(app.activeDocument);

function iterLayers(l) {
    for(var i=0; i<l.layers.length; i++) {
        var curLay = l.layers[i];
        app.activeDocument.activeLayer = curLay;

        if(curLay.typename == "LayerSet") {
            iterLayers(curLay);
        } else {
            if(curLay.name.indexOf("vis_") >= 0) {
                curLay.visible = false;
            }
        }
    }
}
____________________Robotic Process Automation in Desktop Publishing (Book): https://doi.org/10.1007/978-3-658-39375-5
siomospAuthor
Inspiring
November 30, 2023

GNDGN,  thank you but...
although script hides the correct layers, it is relativeli slow and  breaks all other layers visibility !

GNDGN
Inspiring
November 30, 2023

Could you provide a sample file?

____________________Robotic Process Automation in Desktop Publishing (Book): https://doi.org/10.1007/978-3-658-39375-5
Stephen Marsh
Community Expert
Community Expert
November 30, 2023

Screenshots of the layers panel (before/after) and or a sample PSD file is always helpful to illustrate.