Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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');
Copy link to clipboard
Copied
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]) {
Copy link to clipboard
Copied
Hi,
thank you, work's like a charm!
Copy link to clipboard
Copied
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 );
}
}
};
};
Find more inspiration, events, and resources on the new Adobe Community
Explore Now