Skip to main content
Known Participant
September 6, 2022
Answered

Adobe Photoshop Script to Select the first Art Layer

  • September 6, 2022
  • 2 replies
  • 2634 views

Hey!

I am fairly new to JS/photoshop scripting, so this is probably a super simple question.

I am just looking for help in creating the first part of a script that will select the layers based on position and also a way to select layers based on layer type. 


I will be running a script to select the first Art Layer in a file that has multiple layers. 

Thank you all for the help, I hope to be able to contribute as well soon!

This topic has been closed for replies.
Correct answer Stephen Marsh

@J.C.C.1 wrote:

So far this seems like it works, Stephen, but I am curious if you know a good way to get the length of the selected acitve layers afterwards/

I was trying this, but had no luck.

var doc = app.activeDocument
var activeSelectionLength = doc.activeDocument.activeLayer.length
alert(activeSelectionLength)

 

There is no native method that I am aware of, perhaps there is some AM code... All of the ways that I know rely on "hacks", which is sometimes all that we have.

 

https://gist.github.com/hilukasz/03b17ee78414aadff995

 


This script dupes the selected layers into a temporary doc in order to count them. This script does not include any selected layer groups in the count, it is not bulletproof in my tests with nested groups inside groups, so YMMV:

 

selectedLayersLength();

function selectedLayersLength() {

    // Dupe selected layers to temp doc
    var idMk = charIDToTypeID("Mk  ");
    var desc302 = new ActionDescriptor();
    var idnull = charIDToTypeID("null");
    var ref122 = new ActionReference();
    var idDcmn = charIDToTypeID("Dcmn");
    ref122.putClass(idDcmn);
    desc302.putReference(idnull, ref122);
    var idNm = charIDToTypeID("Nm  ");
    desc302.putString(idNm, "", "TempLayerCountDoc", "");
    var idUsng = charIDToTypeID("Usng");
    var ref123 = new ActionReference();
    var idLyr = charIDToTypeID("Lyr ");
    var idOrdn = charIDToTypeID("Ordn");
    var idTrgt = charIDToTypeID("Trgt");
    ref123.putEnumerated(idLyr, idOrdn, idTrgt);
    desc302.putReference(idUsng, ref123);
    var idVrsn = charIDToTypeID("Vrsn");
    desc302.putInteger(idVrsn, 5);
    executeAction(idMk, desc302, DialogModes.NO);

    // Get the layer count
    var layerCount = app.activeDocument.layers.length;

    // Close the temp doc
    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

    // Count notification
    alert(layerCount);
}

 

2 replies

Stephen Marsh
Community Expert
September 6, 2022

Can you post a screenshot of the layers panel structure, with the target layer clearly marked, thanks!

J.C.C.1Author
Known Participant
September 7, 2022

Hey! 
There are a few situations for how this layer structure will be, with very little consistency to start, and I am hoping to make this a Powershell operable code, which are all probably redflags for this...

Situation 1 

Just one layer with a layer mask



Situation two 

One layer with layer mask, one solid color layer on the bottom. The target layer will not always be name original either


Situation 3
One text layer, one image/artlayer, one solid color layer.


I have a script to select all text layers, so I may just use that script and switch the layer kind to art layers. 

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

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

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



This is the thread I got this code from 
https://community.adobe.com/t5/photoshop-ecosystem-discussions/select-only-the-text-layers/td-p/11017397

I am assuming that I would switch the text sheetKind from 3 to 1 for Pixel?

J.C.C.1Author
Known Participant
September 7, 2022

So far this seems like it works, Stephen, but I am curious if you know a good way to get the length of the selected acitve layers afterwards/

I was trying this, but had no luck.

var doc = app.activeDocument
var activeSelectionLength = doc.activeDocument.activeLayer.length
alert(activeSelectionLength)



J.C.C.1Author
Known Participant
September 6, 2022

I think I may have solved this myself.

Would this work?

J.C.C.1Author
Known Participant
September 6, 2022
var doc = app.activeDocument

var layerCount = app.activeDocument.layers.length
alert (layerCount)
var x = (layerCount-2)
app.activeDocument.activeLayer = app.activeDocument.layers[x]
if (app.activeDocument.activeLayer.typename === "ArtLayer")