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

Script to toggle visibility of specific layers

Explorer ,
Mar 01, 2023 Mar 01, 2023

Copy link to clipboard

Copied

I am wondering if it's possible for a script to find any and all layers in a PSD, targeting using either the layer name or the color of the layer, and turning the visibility off. If any targeted layer's visibility is already off, it would stay off and not turn on.

TOPICS
Actions and scripting , macOS

Views

2.9K

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 2 Correct answers

Community Expert , Mar 04, 2023 Mar 04, 2023

@RRowe 

 

Try this script, it would need to be written in AM code to run faster, which is beyond my current ability (I only got this far by standing on the shoulders of giants):

 

/*
Hide All Layers Labeled In Orange.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-toggle-visibility-of-specific-layers/td-p/13618157
v1.0 - 4th March 2023, Stephen Marsh
*/

#target photoshop

function main() {

    if (!documents.length) {
        alert('There are no documents open!');
...

Votes

Translate

Translate
Community Expert , Mar 07, 2023 Mar 07, 2023

For sake of completeness, here is a version using the layer name rather than colour label.

 

This is looking for a case-sensitive regular expression based match including a word space either before or after the social media site name as per the layered example used in this topic for the text layers. This could just be using a text prefix such as "Hide_" or similar before the layer name.

 

/*
Hide All Social Media Named Layers.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/scr
...

Votes

Translate

Translate
Adobe
Community Expert ,
Mar 01, 2023 Mar 01, 2023

Copy link to clipboard

Copied

Yes it is possible.

 

please provide s sample screenshot of the before and alter layers panel to illustrate. A sample file would also be helpful as well as some specific details.

Votes

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
Explorer ,
Mar 01, 2023 Mar 01, 2023

Copy link to clipboard

Copied

I have attached a sample PSD and two screen shots of the layers, before and after. 
As you can see in the sample file, the targeted layers are colored orange, but that could be done in any fashion. For example, instead of being orange, the layer name could be the target. Perhaps the layer name could include a specific word or phrase. Whatever targets properly. 

Votes

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 ,
Mar 01, 2023 Mar 01, 2023

Copy link to clipboard

Copied

The layers labeled with orange are all text layers... So no colour labels or special keywords are required if this is only for text layers.

Votes

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
Explorer ,
Mar 02, 2023 Mar 02, 2023

Copy link to clipboard

Copied

Meaning, it's easy to target text-only layers?
I do realize the sample I sent was all text layers, but there will definitely be some guide layers that have color blocks and other information, like you see in the Red Labeled "Group 1". Those layers would also be subject to having their visibility toggled off as well. 

This document will be generating image assets, and in the I need all layers with information and guides to be turned off so only the true image/photographic layers export. We could toggle manually, but trying to make this as failproof and automated as possible.

Votes

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 ,
Mar 02, 2023 Mar 02, 2023

Copy link to clipboard

Copied

Yes, that's right, different layer kinds have specific properties making them easy to isolate/target. Text layers are just one example.

 

That being said, you need more flexibility than that.

 

Layer colour labels are limited, will there be enough of them to differentiate your various combinations?

Votes

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
Explorer ,
Mar 03, 2023 Mar 03, 2023

Copy link to clipboard

Copied

There will be enough to differentiate what I need. In fact, to make it easier these files could easy use just a single color. I don't forsee needing more than that in this case. So if we were able to target even a single color, I think this would work.

Votes

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 ,
Mar 04, 2023 Mar 04, 2023

Copy link to clipboard

Copied

@RRowe 

 

Try this script, it would need to be written in AM code to run faster, which is beyond my current ability (I only got this far by standing on the shoulders of giants):

 

/*
Hide All Layers Labeled In Orange.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-toggle-visibility-of-specific-layers/td-p/13618157
v1.0 - 4th March 2023, Stephen Marsh
*/

#target photoshop

function main() {

    if (!documents.length) {
        alert('There are no documents open!');
    } else {
        processAllLayersAndSets(activeDocument);
    }

    function processAllLayersAndSets(obj) {
        for (var al = obj.layers.length - 1; 0 <= al; al--) {
            activeDocument.activeLayer = obj.layers[al];
            orangeLayerVisibilityToFalse();
        }

        for (var ls = obj.layerSets.length - 1; 0 <= ls; ls--) {
            processAllLayersAndSets(obj.layerSets[ls]);
            orangeLayerVisibilityToFalse();
        }
    }

    function orangeLayerVisibilityToFalse() {
        try {
            // Courtesy of Paul Riggott
            // Colors returned:
            // "none","red","orange","yellowColor","grain","blue","violet","gray"
            var ref = new ActionReference();
            ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
            var appDesc = executeActionGet(ref);
            if (typeIDToStringID(appDesc.getEnumerationValue(stringIDToTypeID('color'))) == "orange") {
                // Set the layer visibility to false
                activeDocument.activeLayer.visible = false;
            }
        } catch (e) {
            alert("Error!" + "\r" + e + ' ' + e.line);
        }
    }
}

activeDocument.suspendHistory('Hide All Layers Labeled In Orange...', 'main()');

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

Votes

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
Explorer ,
Mar 06, 2023 Mar 06, 2023

Copy link to clipboard

Copied

@Stephen_A_Marsh This seems to work just as needed! I've tested it a few way and so far all is good. 
Thank you so much for your help, as usual!

Votes

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 ,
Mar 06, 2023 Mar 06, 2023

Copy link to clipboard

Copied

Thanks, you're welcome!

Votes

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
Explorer ,
May 30, 2023 May 30, 2023

Copy link to clipboard

Copied

@Stephen_A_Marsh Hello again! I've noticed something happening with this script, and taking a look at the script myself I can't see anything obvious that would be causing this issue. The script perfectly turns the visibility off of any layer labeled in orange, but it is also turning ON any layer whose visibility is off when the script is run. 

I discovered this when I duplicated your script and modified it to target any blue layers. I ran the orange layers script, it turned orange off, then ran the blue layer script. Blue layers turned off but it cause the orange layers to turn back on. I tested with other non-colored layers by turning them off before running either script and the same thing happened. Anything off when running the script gets turned on. Any ideas?

Votes

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 ,
May 30, 2023 May 30, 2023

Copy link to clipboard

Copied

LATEST

edited

 

// hide layer of certain color;
// 2023, use it at your own risk;
if (app.documents.length > 0) {
    var theLayers = hideLayersOfColor ("orange");
    if (theLayers.length > 0) {
        alert (theLayers.join("\n"));
    }
};
////// collect layers //////
function hideLayersOfColor (thisColor) {
    // the file;
    var myDocument = app.activeDocument;
    // 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'));
        var theColor = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("color")));
        var theVisibility = layerDesc.getBoolean(stringIDToTypeID('visible'));
//    var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
//    var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
    if (theColor == thisColor) {
        theLayers.push([theName, theIndex, theID, theColor, theVisibility]);
// hide layers;
        if (theVisibility == true) {
            var desc4 = new ActionDescriptor();
            var list1 = new ActionList();
            var ref1 = new ActionReference();
            ref1.putIdentifier( stringIDToTypeID( "layer" ), theID);
            list1.putReference( ref1 );
            desc4.putList( stringIDToTypeID( "null" ), list1 );
            executeAction( stringIDToTypeID( "hide" ), desc4, DialogModes.NO );
        }
    }
    };
    }
    catch (e) {};
    };
    return theLayers
    };

 

 

Votes

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 ,
Mar 07, 2023 Mar 07, 2023

Copy link to clipboard

Copied

For sake of completeness, here is a version using the layer name rather than colour label.

 

This is looking for a case-sensitive regular expression based match including a word space either before or after the social media site name as per the layered example used in this topic for the text layers. This could just be using a text prefix such as "Hide_" or similar before the layer name.

 

/*
Hide All Social Media Named Layers.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-toggle-visibility-of-specific-layers/td-p/13618157
v1.0 - 7th March 2023, Stephen Marsh
*/

#target photoshop

function main() {

    if (!documents.length) {
        alert('There are no documents open!');
    } else {
        processAllLayersAndSets(activeDocument);
    }

    function processAllLayersAndSets(obj) {
        for (var al = obj.layers.length - 1; 0 <= al; al--) {
            activeDocument.activeLayer = obj.layers[al];
            socialmediaLayerVisibilityToFalse();
        }

        for (var ls = obj.layerSets.length - 1; 0 <= ls; ls--) {
            processAllLayersAndSets(obj.layerSets[ls]);
            socialmediaLayerVisibilityToFalse();
        }
    }

    function socialmediaLayerVisibilityToFalse() {
        try {
            if (/Facebook |Instagram |Twitter |LinkedIn | YouTube/.test(activeDocument.activeLayer.name) === true) {
                // Set the layer visibility to false
                activeDocument.activeLayer.visible = false;
            }
        } catch (e) {
            alert("Error!" + "\r" + e + ' ' + e.line);
        }
    }
}

activeDocument.suspendHistory('Hide All Social Media Named Layers...', 'main()');

Votes

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
Explorer ,
Mar 07, 2023 Mar 07, 2023

Copy link to clipboard

Copied

Well, that covers all the ways this document may evolve. Thank you for the thorough attention on this!

Votes

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 ,
Mar 07, 2023 Mar 07, 2023

Copy link to clipboard

Copied

All good, I really don't like doing things by half.

Votes

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