• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit Search
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.4K

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

LATEST

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