How to hide layer set with specific name

Engaged ,
Sep 06, 2022 Sep 06, 2022

Copy link to clipboard

Copied

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.

TOPICS
Actions and scripting

Views

64

Likes

Translate

Translate

Report

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 1 Correct answer

Community Expert , Sep 06, 2022 Sep 06, 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(
...

Likes

Translate

Translate
Community Expert ,
Sep 06, 2022 Sep 06, 2022

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

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 ,
Sep 06, 2022 Sep 06, 2022

Copy link to clipboard

Copied

 

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)

Likes

Translate

Translate

Report

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 ,
Sep 06, 2022 Sep 06, 2022

Copy link to clipboard

Copied

LATEST

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

Likes

Translate

Translate

Report

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