Skip to main content
Codypyperokay
Participant
October 7, 2016
Question

Can I create an action that turns off all layers named a certain name?

  • October 7, 2016
  • 2 replies
  • 610 views

I need to create an action to turn off all of the layers named "BG" for my example. I can create an action to turn off one at a time, but I have a ton of layers living folders named "BG". I want to be able to toggle them on and off. Thanks!

This topic has been closed for replies.

2 replies

JJMack
Community Expert
Community Expert
October 7, 2016

You could script that you can not do that in an action. Scripting is programming. So you can use logic to loop through layers and alter the one you want to. If you can identify then some how. Like checking if the layer name  is "BG".  

JJMack
Codypyperokay
Participant
October 7, 2016

So what would that look like in a script?

SuperMerlin
Inspiring
October 8, 2016

#target photoshop;

app.bringToFront();

if(documents.length) main();

function main(){

var win = new Window("dialog","Show/Hide Layers");

win.alignChildren="left";

win.st1 = win.add("statictext",undefined,"Layer Name");

win.et1 = win.add("edittext",undefined,"BG");

win.et1.preferredSize=[300,20];

win.grp1 = win.add("group");

win.grp1.rb1 = win.grp1.add("radiobutton",undefined,"Hide Layers");

win.grp1.rb1.value=true;

win.grp1.rb2 = win.grp1.add("radiobutton",undefined,"Show Layers");

win.grp2 = win.add("group");

win.grp2.bu1 = win.grp2.add("button",undefined,"Process");

win.grp2.bu1.preferredSize=[150,35];

win.grp2.bu2 = win.grp2.add("button",undefined,"Cancel");

win.grp2.bu2.preferredSize=[150,35];

win.grp2.bu1.onClick=function(){

if(win.et1.text==""){

    alert("Layer name required!");

    return;

    }

win.close(0);

var hs = 0;

if(win.grp1.rb2.value) hs = 1;

hideLayers(win.et1.text.toString(),hs);

}

win.show();

function hideLayers(Name,showHide){

   var ref = new ActionReference();

   ref.putProperty( charIDToTypeID( "Prpr" ), charIDToTypeID( 'NmbL' ));

   ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );

   var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL')) +1;

   var Names=[];

try{

    activeDocument.backgroundLayer;

var i = 0; }catch(e){ var i = 1; };

   for(i;i<count;i++){

       if(i == 0) continue;

        ref = new ActionReference();

        ref.putIndex( charIDToTypeID( 'Lyr ' ), i );

        var desc = executeActionGet(ref);

        var layerName = desc.getString(charIDToTypeID( 'Nm  ' ));

        if(layerName.toString() == Name.toString() ) hideShowLayer(i,showHide);

   };

};

function hideShowLayer(index,hs) {

    if(hs == undefined) hs = 'Shw ';

    if(hs == 0) hs = 'Hd  ';

    if(hs == 1) hs = 'Shw ';

    var ref = new ActionReference();

    ref.putIndex(charIDToTypeID("Lyr "), index);

    var desc = new ActionDescriptor();

    desc.putReference(charIDToTypeID('null'), ref);

    executeAction(charIDToTypeID(hs), desc, DialogModes.NO);

};

};