Adobe Photoshop Script to Select the first Art Layer

Community Beginner ,
Sep 06, 2022 Sep 06, 2022

Copy link to clipboard

Copied

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!

TOPICS
Actions and scripting

Views

139

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

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

Likes

Translate

Translate
Community Beginner ,
Sep 06, 2022 Sep 06, 2022

Copy link to clipboard

Copied

I think I may have solved this myself.

Would this work?

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

Copy link to clipboard

Copied

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

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

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

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

Copy link to clipboard

Copied

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

justinc36627912_0-1662515823911.png

Situation two 

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

justinc36627912_1-1662515872780.png


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

justinc36627912_2-1662515981131.png


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/1101...

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

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 Beginner ,
Sep 07, 2022 Sep 07, 2022

Copy link to clipboard

Copied

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)



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

Copy link to clipboard

Copied


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

 

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

Copy link to clipboard

Copied

This script groups the selected layers into a temporary layerSet 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:

 

countSelectedLayers();

function countSelectedLayers() {
    // Ignores layer groups in the count
    var A = [];
    var desc11 = new ActionDescriptor();
    var ref9 = new ActionReference();
    ref9.putClass(stringIDToTypeID('layerSection'));
    desc11.putReference(charIDToTypeID('null'), ref9);
    var ref10 = new ActionReference();
    ref10.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
    desc11.putReference(charIDToTypeID('From'), ref10);
    executeAction(charIDToTypeID('Mk  '), desc11, DialogModes.NO);

    var gL = activeDocument.activeLayer.layers;
    // Notification
    alert(gL.length);

    for (var i = 0; i < gL.length; i++) {
        A.push(gL[i]);
    }

    executeAction(charIDToTypeID('undo'), undefined, DialogModes.NO);
    return A;
}

 

count-layers.png

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

Copy link to clipboard

Copied

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

 

count-layers.png

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 Beginner ,
Sep 08, 2022 Sep 08, 2022

Copy link to clipboard

Copied

Okay awesome! I think that will work, and I can always set an error message for files with layers nested more than a twice. I think between these two segements of code I have everything I need for this script! Thank you as always!

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

Copy link to clipboard

Copied

LATEST

@justinc36627912 wrote:

Okay awesome! I think that will work, and I can always set an error message for files with layers nested more than a twice. I think between these two segements of code I have everything I need for this script! Thank you as always!


 

Glad it works for you, I would like a more robust script, however, as long as you know the limitations. Looping into all the nested layer sets within layer sets isn't that easy for me.

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

Save time with Adobe Express quick actions