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

Select layers by kind script

Community Beginner ,
Mar 02, 2021 Mar 02, 2021

Copy link to clipboard

Copied

Just a general question that could be very useful in some of my scripts.

 

For example, selecting all text layers and grouping - any idea how you would script such a thing?

 

Much like the sorting layers feature. 

 

Thanks!

TOPICS
Actions and scripting , SDK

Views

2.1K

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

Photoshop, InDeign and Illustrator all use layers/groups to organise stacking order. 

 

Acrobat uses the term "layers" as they are familiar, however, layers in Acrobat are actually called "optional content groups" or OCG. These behave as filters in the sense that conent can be isolated and have their order changed or have visibility turned on and off. This does not affect stacking order. OCG essentially behave in a similar way to visibility filters in the Photoshop layer panel.

 

I agree with JJ

...

Votes

Translate

Translate
Adobe
Community Expert ,
Mar 02, 2021 Mar 02, 2021

Copy link to clipboard

Copied

A script should have no problem doing that but reordering the layer stack by grouping text layer into a new Layerset group would  rain on document composite image most likely. Changing a document layer stacking order will normally change the document composite image. 

Capture.jpg

 

JJMack

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 Beginner ,
Mar 02, 2021 Mar 02, 2021

Copy link to clipboard

Copied

I know that it's doable. 

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

Copy link to clipboard

Copied

You just need to process photoshop layer objects. Why would one want to group layer by kind and change the layers stacking order. 

JJMack

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 Beginner ,
Mar 02, 2021 Mar 02, 2021

Copy link to clipboard

Copied

Because file organization is professional. 

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

Copy link to clipboard

Copied

Why do state file organization is professional. What does the meant to you.  What does that have to with layers, Layer kind layersets  layer order etc. What is unprofessional with Photoshop document organization and data structures?  IMO Photoshop is a professional tool that has some issues.  Photoshop is a Document editor that saves File of various types.  Photoshop supports many file formats.

JJMack

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
Guide ,
Mar 02, 2021 Mar 02, 2021

Copy link to clipboard

Copied

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

Copy link to clipboard

Copied

@jazz-y

 

Haha, I see that we were both thinking along the same lines!

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

Copy link to clipboard

Copied

Photoshop, InDeign and Illustrator all use layers/groups to organise stacking order. 

 

Acrobat uses the term "layers" as they are familiar, however, layers in Acrobat are actually called "optional content groups" or OCG. These behave as filters in the sense that conent can be isolated and have their order changed or have visibility turned on and off. This does not affect stacking order. OCG essentially behave in a similar way to visibility filters in the Photoshop layer panel.

 

I agree with JJMack that it is potentially damaging to the composition of the image to change the stacking order of layers by isolating them into layer groups based on their layer kind. I say potentially as it will depend on how simple the layer structure is. There are scripts to rename or colour layers if the idea is to have layers that are easier to identify and still retain more complex stacking orders.

 

Here are some links that may be of interest:

 

https://community.adobe.com/t5/photoshop/select-only-the-text-layers/m-p/11017397

https://community.adobe.com/t5/photoshop/select-all-text-layers/m-p/8489549

https://github.com/nvkzNemo/experimental-ps-scripts/blob/master/selectAllTextLayers.jsx

 

Here is a proof of concept script, it only works on text layers, however you can easily extend the script for additional layer kinds:

 

 

// community.adobe.com/t5/photoshop/select-layers-by-kind-script/td-p/11867138

// community.adobe.com/t5/photoshop/select-only-the-text-layers/m-p/11017843
// DmitryEgorov

// Select ALL Layers of Specific Layer Kind - Works with layer sets and nested sets!

var AM = new ActionManager
    if (app.documents.length) selectLayerKind ()

function selectLayerKind() {
    var output = [],
        from = AM.getDocProperty('hasBackgroundLayer') ? 0 : 1,
        to = AM.getDocProperty('numberOfLayers')
        
        if (to) AM.deselectLayers ()
        
    for (var i = from; i <= to; i++) {

        // Any Layer = 0
        // Pixel Layer  = 1 (including Background layer)
        // Adjustment Layer = 2
        // Text Layer = 3
        // Vector Layer = 4
        // SmartObject Layer = 5
        // Video Layer = 6
        // LayerGroup Layer = 7
        // 3D Layer = 8
        // Gradient Layer = 9
        // Pattern Layer = 10
        // Solid Color Layer = 11
        // Background Layer = 12
        // HiddenSectionBounder = 13

        if (AM.getLayerProperty('layerKind', i) == 3) AM.selectLayerByIndex(i, true) // Change the number as above!

    }

    /* Stephen Marsh 2021 - Call the function to group selected layers and name as "Text Layers" */
    makeGroup("Text Layers", 7, 8, "Text Layers");

}

function ActionManager() {

    this.getDocProperty = function (property) {
        property = s2t(property)
        var ref = new ActionReference()
        ref.putProperty(s2t("property"), property)
        ref.putEnumerated(s2t("document"), s2t("ordinal"), s2t("targetEnum"))
        return getDescValue(executeActionGet(ref), property)
    }

    this.getLayerProperty = function (property, index) {
        property = s2t(property)
        var ref = new ActionReference()
        ref.putProperty(s2t("property"), property)
        ref.putIndex(s2t("layer"), index)
        return getDescValue(executeActionGet(ref), property)
    }

    this.selectLayerByIndex = function (idx, addToSelection) {
        var desc = new ActionDescriptor()
        var ref = new ActionReference()
        ref.putIndex(s2t("layer"), idx)
        desc.putReference(s2t("null"), ref)
        if (addToSelection) desc.putEnumerated(s2t("selectionModifier"), s2t("addToSelectionContinuous"), s2t("addToSelection"))
        executeAction(s2t("select"), desc, DialogModes.NO);
    }

    this.deselectLayers = function () {
        var desc = new ActionDescriptor()
        var ref = new ActionReference()
        ref.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"))
        desc.putReference(s2t("null"), ref)
        executeAction(s2t("selectNoLayers"), desc, DialogModes.NO)
    }

    function getDescValue(desc, property) {

        switch (desc.getType(property)) {
            case DescValueType.OBJECTTYPE:
                return (desc.getObjectValue(property));
                break;
            case DescValueType.LISTTYPE:
                return desc.getList(property);
                break;
            case DescValueType.REFERENCETYPE:
                return desc.getReference(property);
                break;
            case DescValueType.BOOLEANTYPE:
                return desc.getBoolean(property);
                break;
            case DescValueType.STRINGTYPE:
                return desc.getString(property);
                break;
            case DescValueType.INTEGERTYPE:
                return desc.getInteger(property);
                break;
            case DescValueType.LARGEINTEGERTYPE:
                return desc.getLargeInteger(property);
                break;
            case DescValueType.DOUBLETYPE:
                return desc.getDouble(property);
                break;
            case DescValueType.ALIASTYPE:
                return desc.getPath(property);
                break;
            case DescValueType.CLASSTYPE:
                return desc.getClass(property);
                break;
            case DescValueType.UNITDOUBLE:
                return (desc.getUnitDoubleValue(property));
                break;
            case DescValueType.ENUMERATEDTYPE:
                return (t2s(desc.getEnumerationValue(property)));
                break;
            case DescValueType.RAWTYPE:
                var tempStr = desc.getData(property);
                var rawData = new Array();
                for (var tempi = 0; tempi < tempStr.length; tempi++) {
                    rawData[tempi] = tempStr.charCodeAt(tempi);
                }
                return rawData;
                break;
            default:
                break;
        };
    }

    function s2t(s) { return stringIDToTypeID(s) }
    function t2s(t) { return typeIDToStringID(t) }
}

/* Stephen Marsh 2021 - Function to group selected layers */
function makeGroup(name2, layerSectionStart, layerSectionEnd, name3) {
    var c2t = function (s) {
        return app.charIDToTypeID(s);
    };
    var s2t = function (s) {
        return app.stringIDToTypeID(s);
    };
    var descriptor = new ActionDescriptor();
    var descriptor2 = new ActionDescriptor();
    var reference = new ActionReference();
    var reference2 = new ActionReference();
    reference.putClass(s2t("layerSection"));
    descriptor.putReference(c2t("null"), reference);
    reference2.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
    descriptor.putReference(s2t("from"), reference2);
    descriptor2.putString(s2t("name"), name2);
    descriptor.putObject(s2t("using"), s2t("layerSection"), descriptor2);
    descriptor.putInteger(s2t("layerSectionStart"), layerSectionStart);
    descriptor.putInteger(s2t("layerSectionEnd"), layerSectionEnd);
    descriptor.putString(s2t("name"), name3);
    executeAction(s2t("make"), descriptor, DialogModes.NO);
}

 

 

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
New Here ,
Aug 21, 2023 Aug 21, 2023

Copy link to clipboard

Copied

Thanks so much for sharing this code, it has been really helpful.
I'm trying to figure out how to knock off the layer visibility after it has selected the specific layer kind. 
Any help would be really appreciated.

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 ,
Aug 21, 2023 Aug 21, 2023

Copy link to clipboard

Copied

quote

I'm trying to figure out how to knock off the layer visibility after it has selected the specific layer kind. 
Any help would be really appreciated.


By @annm63429510

 

 

The standard DOM code to turn visibility off for the active layer is:

 

app.activeDocument.activeLayer.visible = false;

 

https://theiviaxx.github.io/photoshop-docs/Photoshop/Layer/visible.html#layer-visible 

 

That being said, it's only possible to affect one active layer at a time with the above code (which would only apply to the visibility of the topmost layer if multiple layers were selected). To affect multiple selected layers, you can use AM code:

 

// "show" or "hide"
selectedLayerVisibility("hide");

function selectedLayerVisibility(lyrVis) {
	var s2t = function (s) { return app.stringIDToTypeID(s); };
	var descriptor = new ActionDescriptor();
	var list = new ActionList();
	var reference = new ActionReference();
	reference.putEnumerated( s2t( "layer" ), s2t( "ordinal" ), s2t( "targetEnum" ));
	list.putReference( reference );
	descriptor.putList( s2t( "null" ), list );
	executeAction( s2t( lyrVis ), descriptor, DialogModes.NO );
}

 

Depending on your requirements, it is also possible to toggle the visibility of the active layer (on/off or off/on) – or to select a layer without changing its visibility.

 

Hope this helps!

 

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
New Here ,
Aug 22, 2023 Aug 22, 2023

Copy link to clipboard

Copied

LATEST

This is amazing, thanks so much!

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

Copy link to clipboard

Copied

A related script using DOM code to determine layer kinds (rather than AM code).

 

// http://creativetuts.com/photoshop-script-determine-layer-kind/
// Photoshop Script
// "Determine Layer Kind"
// Script by Mehmet Şensoy
 
#target photoshop  
app.bringToFront();
 
var doc = app.activeDocument;
var layer = doc.activeLayer;// Save selected layer to variable:
 
function determinelayerkind (layer) {
if(layer.kind == LayerKind.TEXT) {layerkind = "Text Layer";return layerkind;} // Text Layer
else if(layer.kind == LayerKind.SOLIDFILL) {layerkind = "Shape Layer";return layerkind; } /* Shape Layer */ 
else if(layer.kind == LayerKind.BLACKANDWHITE) {layerkind = "Black and White adjustment layer";return layerkind; } /* Black and White */ 
else if(layer.kind == LayerKind.BRIGHTNESSCONTRAST) {layerkind = "Brightness contrast adjustment layer";return layerkind; } /* Brightness contrast adjustment layer */ 
else if(layer.kind == LayerKind.CHANNELMIXER) {layerkind = "Channel mixer adjustment layer";return layerkind; } /* Channel mixer adjustment layer */ 
else if(layer.kind == LayerKind.COLORBALANCE) {layerkind = "Color balance adjustment layer";return layerkind; } /* Color balance adjustment layer */ 
else if(layer.kind == LayerKind.CURVES) {layerkind = "Curves adjustment layer";return layerkind; } /* Curves adjustment layer */
else if(layer.kind == LayerKind.EXPOSURE) {layerkind = "Exposure layer";return layerkind; } /* Exposure layer */ 
else if(layer.kind == LayerKind.GRADIENTFILL) {layerkind = "Gradient fill";return layerkind; } /* Gradient fill */ 
else if(layer.kind == LayerKind.GRADIENTMAP) {layerkind = "Gradient map adjustment layer";return layerkind; } /* Gradient map adjustment layer */ 
else if(layer.kind == LayerKind.HUESATURATION) {layerkind = "Hue saturation adjustment layer";return layerkind; } /* Hue saturation adjustment layer */ 
else if(layer.kind == LayerKind.INVERSION) {layerkind = "Invert adjustment layer";return layerkind; } /* Invert adjustment layer */ 
else if(layer.kind == LayerKind.LAYER3D) {layerkind = "3D layer";return layerkind; } /* 3D layer */
else if(layer.kind == LayerKind.LEVELS) {layerkind = "Levels adjustment layer";return layerkind; } /* Levels adjustment layer */ 
else if(layer.kind == LayerKind.NORMAL) {layerkind = "Normal layer";return layerkind; } /* Normal Layer */ 
else if(layer.kind == LayerKind.PATTERNFILL) {layerkind = "Pattern fill layer";return layerkind; } /* Pattern fill layer */ 
else if(layer.kind == LayerKind.PHOTOFILTER) {layerkind = "Photo filter layer";return layerkind; } /* Photo filter layer */ 
else if(layer.kind == LayerKind.POSTERIZE) {layerkind = "Posterize adjustment layer";return layerkind; } /* Posterize adjustment layer */ 
else if(layer.kind == LayerKind.SELECTIVECOLOR) {layerkind = "Selective color adjustment layer.";return layerkind; } /* Selective color adjustment layer */ 
else if(layer.kind == LayerKind.SMARTOBJECT) {layerkind = "Smart object layer";return layerkind; } /* Smart object layer */ 
else if(layer.kind == LayerKind.THRESHOLD) {layerkind = "Threshold adjustment layer";return layerkind; } /* Threshold adjustment layer */ 
else if(layer.kind == LayerKind.VIBRANCE) {layerkind = "Vibrance layer";return layerkind; } /* Vibrance layer */ 
else if(layer.kind == LayerKind.VIDEO) {layerkind = "Video layer";return layerkind; } /* Video layer */ 
}
 
alert ("Current Layer is a " + determinelayerkind(layer));

 

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

Copy link to clipboard

Copied

we made a Photoshop plug-in that can do this. The unlimited version is 12$. 

ACA682F0-6645-4D3E-9483-AA0420C3B09E.png

 

Full Version (12$):
https://exchange.adobe.com/apps/cc/c36e02fb/swift


Trial Version (Limited to 5 Actions):
https://exchange.adobe.com/apps/cc/412c1dcd/swift-trial-version

 

Its also a big help with getting the layers renamed.

157C23B6-64D8-4BBB-9B93-A58BF0DD8414.png19C5A5CC-206D-4D62-BE75-F35FFA475496.png

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