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

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

Engaged ,
Nov 29, 2023 Nov 29, 2023

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');

 

 

TOPICS
Actions and scripting
804
Translate
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 2 Correct answers

Participant , Nov 30, 2023 Nov 30, 2023

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

 

Translate
Engaged , Nov 30, 2023 Nov 30, 2023

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

 

 

Translate
Adobe
Community Expert ,
Nov 29, 2023 Nov 29, 2023

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

Translate
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
Participant ,
Nov 29, 2023 Nov 29, 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
Translate
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
Engaged ,
Nov 30, 2023 Nov 30, 2023

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

Translate
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
Participant ,
Nov 30, 2023 Nov 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
Translate
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
Engaged ,
Nov 30, 2023 Nov 30, 2023

Simplified sample psd attached 
(Original have mutiple "vis" groups and starting with "vis_" layers)
The goal is to hide all layers starting with "vis_" , but not the groups named "vis"

Translate
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
Participant ,
Nov 30, 2023 Nov 30, 2023

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

 

____________________
Robotic Process Automation in Desktop Publishing (Book): https://doi.org/10.1007/978-3-658-39375-5
Translate
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
Engaged ,
Nov 30, 2023 Nov 30, 2023

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

 

 

Translate
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
Engaged ,
Nov 30, 2023 Nov 30, 2023

But, it is relatively slow at my original psd  
Code using the  ActionDescriptor usually is much faster
If any one can improve the script I will be grateful 

Translate
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
Participant ,
Nov 30, 2023 Nov 30, 2023

What about the UXP way?:

const app = require('photoshop').app;

iterLayers(app.activeDocument);

function iterLayers(l) {
	l.layers.forEach(ls => {
		if(ls.layers != undefined) {
			iterLayers(ls);
		} else {
			if(ls.name.indexOf("vis_") >= 0) {
				ls.visible = false;
			}
		}
	});
}
____________________
Robotic Process Automation in Desktop Publishing (Book): https://doi.org/10.1007/978-3-658-39375-5
Translate
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
Engaged ,
Dec 01, 2023 Dec 01, 2023

@Stephen Marsh 
Hi, sample PSD is uploaded.
The script from GNGN working fine but it is relatively slow, running 3- 5 sec to  at original psd
Action Manager code is usualy faster, but I cannot find or edit a script to do the job
Thank you all


Translate
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
Engaged ,
Dec 01, 2023 Dec 01, 2023
LATEST
Translate
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