Skip to main content
Inspiring
September 6, 2022
Answered

How to hide layer set with specific name

  • September 6, 2022
  • 3 replies
  • 549 views

How to hide specific layer set using javascript, if even exist on PSD? I need to hide the group called Print preview, but is not always on PSD.

This topic has been closed for replies.
Correct answer c.pfaffenbichler

Try this: 

// 2022, use it at your own risk;
if (app.documents.length > 0) {
var thisName = "Print preview";
var theLayers = collectLayersByName(thisName);
for (var x = 0; x < theLayers.length; x++) {
    var idhide = stringIDToTypeID( "hide" );
    var desc22 = new ActionDescriptor();
    var list2 = new ActionList();
    var ref8 = new ActionReference();
    ref8.putIdentifier( stringIDToTypeID( "layer" ), theLayers[x][2] );
    list2.putReference( ref8 );
    desc22.putList( stringIDToTypeID( "null" ), list2 );
    executeAction( idhide, desc22, DialogModes.NO );
    }
};
////// collect layers with certain name //////
function collectLayersByName (aName) {
// get number of layers;
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('numberOfLayers'));
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 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'));
if (theName == aName) {theLayers.push([theName, theIndex, theID])}
};
}
catch (e) {};
};
return theLayers
};

3 replies

Stephen Marsh
Community Expert
Community Expert
September 6, 2022

Try this one, the normal limitations apply with getByName:

 

// Code modified from a script from Chuck Uebele

#target photoshop

toggleGroupVisibility("Print preview");

function toggleGroupVisibility(groupName) {
    if (documents.length > 0) {
        try {
            var toggleGroup = activeDocument.layerSets.getByName(groupName);
            toggleGroup.visible = toggleGroup.visible === false;
        } catch (e) { }
    }
}

 

 

Or to simply hide:

 

// Code modified from a script from Chuck Uebele

#target photoshop

hideGroupVisibility("Print preview");

function hideGroupVisibility(groupName) {
    if (documents.length > 0) {
        try {
            var hideGroup = activeDocument.layerSets.getByName(groupName);
            hideGroup.visible = false;
        } catch (e) { }
    }
}
Legend
September 6, 2022

 

try{
(r = new ActionReference()).putName(stringIDToTypeID('layer'), 'Print preview');
(d = new ActionDescriptor()).putReference(stringIDToTypeID('target'), r);
executeAction(stringIDToTypeID('hide'), d, DialogModes.NO);} catch (e) {}

 

 

@c.pfaffenbichler's answer is more correct as code checks the layer type before hiding it

 

Surprisingly, but this way also works:

 

try{
(r = new ActionReference()).putName(stringIDToTypeID('layerSection'), 'Print preview');
(d = new ActionDescriptor()).putReference(stringIDToTypeID('target'), r);
executeAction(stringIDToTypeID('hide'), d, DialogModes.NO);} catch (e) {}

 

(though on one condition - the document should not have another layer named "Print preview" or it can be, but located above the group)

c.pfaffenbichler
Community Expert
c.pfaffenbichlerCommunity ExpertCorrect answer
Community Expert
September 6, 2022

Try this: 

// 2022, use it at your own risk;
if (app.documents.length > 0) {
var thisName = "Print preview";
var theLayers = collectLayersByName(thisName);
for (var x = 0; x < theLayers.length; x++) {
    var idhide = stringIDToTypeID( "hide" );
    var desc22 = new ActionDescriptor();
    var list2 = new ActionList();
    var ref8 = new ActionReference();
    ref8.putIdentifier( stringIDToTypeID( "layer" ), theLayers[x][2] );
    list2.putReference( ref8 );
    desc22.putList( stringIDToTypeID( "null" ), list2 );
    executeAction( idhide, desc22, DialogModes.NO );
    }
};
////// collect layers with certain name //////
function collectLayersByName (aName) {
// get number of layers;
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('numberOfLayers'));
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 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'));
if (theName == aName) {theLayers.push([theName, theIndex, theID])}
};
}
catch (e) {};
};
return theLayers
};