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

A script to toggle a named layer or group? Not the currently active group.

Explorer ,
May 14, 2020 May 14, 2020

I wonder if someone help me achieve this please - I need a script to toggle the visibilty of a named layer group, without making it the active group - so I can assign an F Key shortcut to very quickly toggle the layer group. This is for fast retouching. Achieving this is something I've been struggling with recently.

 

Ideally, best case, I would also like to have a variable within the script - so that this layer group could have one of three potential names. And it would toggle the visibility of whichever of these three groups was present in the file. 

 

Thank you to anyone that can help.

 

 

TOPICS
Actions and scripting , Windows
2.6K
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
Adobe
Community Expert ,
May 14, 2020 May 14, 2020

Try this. It will only work with a groups or layers that are not nested in a group. It will also only select one of the three layers in the order that is in the code. Change the names to what your layers are named.

#target photoshop
var doc = activeDocument;
var layer1 = 'first layer name';
var layer2 = 'second layer name';
var layer3 = 'third layer name';

try{var toggleLayer = doc.layers.getByName(layer1)}
catch(e){
    try{var toggleLayer = doc.layers.getByName(layer2)}
    catch(e){
        try{var toggleLayer = doc.layers.getByName(layer3)}
        catch(e){}
        }
    }

toggleLayer.visible = toggleLayer.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
Community Expert ,
May 14, 2020 May 14, 2020

Hi, You can use following snippet to toggle the visibility of layer by calling this function with parameter of layer name

 

 

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

    if(vis){
        var idhide = stringIDToTypeID("hide");
        executeAction(idhide, desc11, DialogModes.NO);
    }else{
        var idshow = stringIDToTypeID( "show" );
        executeAction(idshow, desc11, DialogModes.NO);
    }
}

toggleVisibility('Layer 1');

 

 

This snippet will handle layers inside the layersets as well. To enter layer name dynamically, you can create small dialog with one edit input, asking user to enter name of the layer.

 

Let us know if this helps you.

Thanks

Best regards
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 script is work's great and it is fast, thank you
It can be adapted for Layer name starts with string, eg "vis_" ?

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

    if(vis){
        var idhide = stringIDToTypeID("hide");
        executeAction(idhide, desc11, DialogModes.NO);
    }else{
        var idshow = stringIDToTypeID( "show" );
        executeAction(idshow, desc11, DialogModes.NO);
    }
}

toggleVisibility('Layer 1');

 

 

toggleVisibility('Layer 1');

 

 

 

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
Community Expert ,
Nov 30, 2023 Nov 30, 2023

You can amend the line 

if (thisLayer[0] == theNames[p]) {

which compares the names. 

The »good« method would probably be using a RegExp and the method »match« instead of »is equal« but if you are unfamiliar with regular expressions you can just slice the name (thisLayer[0]) and stick with »==«. 

 

Eit: 

if (thisLayer[0].slice(0,4) == theNames[p]) {
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

Hi, 
thank you, work's like a charm!

 

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
Community Expert ,
May 14, 2020 May 14, 2020

 

The Array »theNames« can contain multiple name-Strings. 

// show or hide layers of certain name;
// 2020, use it at your own risk;
if (app.documents.length > 0) {
app.activeDocument.suspendHistory("stuff", "main ()");
};
////////////////////////////////////
function main () {
// the file;
var myDocument = app.activeDocument;
// get number of layers;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// process the layers;
var theLayers = new Array;
for (var m = 0; m <= theNumber; m++) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
// if not layer group collect values;
if (layerSet != "layerSectionEnd" /*&& layerSet != "layerSectionStart" && isBackground != true*/) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
var theVisibility = layerDesc.getBoolean(stringIDToTypeID('visible'));
theLayers.push([theName, theID, theIndex, theVisibility])
};
}
catch (e) {};
};
////////////////////////////////////
var theNames = ["123", "456"];
////////////////////////////////////
for (var o = 0; o < theLayers.length; o++) {
var thisLayer = theLayers[o];
// check the layer names;
for (var p = 0; p < theNames.length; p++) {
if (thisLayer[0] == theNames[p]) {
// hide or show depending on current setting;
if (thisLayer[3] == true) {var idhide = stringIDToTypeID( "hide" )}
else {var idhide = stringIDToTypeID( "show" )};
var desc22 = new ActionDescriptor();
var list2 = new ActionList();
var ref8 = new ActionReference();
ref8.putIdentifier( stringIDToTypeID( "layer" ), thisLayer[1] );
list2.putReference( ref8 );
desc22.putList( stringIDToTypeID( "null" ), list2 );
executeAction( idhide, desc22, DialogModes.NO );
}
}
};
};

 

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