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

Please help me to iterate through the layers and find their colors

Explorer ,
Sep 06, 2023 Sep 06, 2023

Copy link to clipboard

Copied

Dear experts

I have attached a PSD herewith.

I want to iterate through the layers and find the colors of them.

The layers can be either SOLID FILL layers or NORMAL layers which are rasterized from the COLOR FILL layers. So they have only one color, that is a known factor. Moreover the layers can be like a border, the inside part can be transparent, like layer 4.

Getting the colors from the SOLID FILL layers is very easy, but for NORMAL layers we need to check the average color or the most prominent color.

We have the code for picking up the most prominent color from a document from my earlier post -
https://community.adobe.com/t5/photoshop-ecosystem-discussions/adobe-script-selecting-the-most-promi...

 

But the above mentioned code is for picking up the most prominent color from the whole document, and not from a layer. Hence I am not able to use this code for my purpose. I have spent a lot of time to modify the above code for my purpose, it is now looping through all the layers, but it's a nasty code; it needs a lot of modification. But since it's AM code, it's beyound my knowledge 🙂 Also it's not working properly. For the 4th layer, it is showing that the most prominent color is the white, because it's flattening the layers. So I need to delete the background layer. Then it would give me the most prominent color. I tried to delete the background layers, but I am not able to. How can I delete the background layer from the temp file colors.raw??? My code -

const DE_THRESHOLD = 15, RESIZE_TO = 35;

for (var x=0; x<app.activeDocument.layerSets.length; x++) {
    var layerSet = app.activeDocument.layerSets[x];
    if (layerSet.name=="Images" || layerSet.name=="Images_1" || layerSet.name=="Images_2") {
        for (var layerIndex = 0; layerIndex < layerSet.layers.length; layerIndex++) {
            var layer = layerSet.layers[layerIndex];
            app.activeDocument.activeLayer = layer;
            var bounds = layer.bounds;
            var width = bounds[2].value - bounds[0].value;
            var height = bounds[3].value - bounds[1].value;
            app.documents.add( width, height, 300, "temp",NewDocumentMode.RGB, DocumentFill.WHITE,1.0, BitsPerChannelType.EIGHT, "Adobe RGB (1998)" );
            
            app.activeDocument = app.documents[0];
            var idslct = charIDToTypeID( "slct" );
            var desc219 = new ActionDescriptor();
            var idnull = charIDToTypeID( "null" );
            var ref1 = new ActionReference();
            var idLyr = charIDToTypeID( "Lyr " );
            ref1.putName( idLyr, layer.name);
            desc219.putReference( idnull, ref1 );
            var idMkVs = charIDToTypeID( "MkVs" );
            desc219.putBoolean( idMkVs, false );
            var idLyrI = charIDToTypeID( "LyrI" );
            var list4 = new ActionList();
            list4.putInteger( 13 );
            desc219.putList( idLyrI, list4 );
            executeAction( idslct, desc219, DialogModes.NO );
    
            // =======================================================
            var idDplc = charIDToTypeID("Dplc");
            var desc585 = new ActionDescriptor();
            var idnull = charIDToTypeID("null");
            var ref123 = new ActionReference();
            var idLyr = charIDToTypeID("Lyr ");
            var idOrdn = charIDToTypeID("Ordn");
            var idTrgt = charIDToTypeID("Trgt");
            ref123.putEnumerated(idLyr, idOrdn, idTrgt);
            desc585.putReference(idnull, ref123);
            var idT = charIDToTypeID("T   ");
            var ref124 = new ActionReference();
            var idDcmn = charIDToTypeID("Dcmn");
            ref124.putName(idDcmn, "temp");
            desc585.putReference(idT, ref124);
            var idNm = charIDToTypeID("Nm  ");
            desc585.putString(idNm, layer.name);
            var idVrsn = charIDToTypeID("Vrsn");
            desc585.putInteger(idVrsn, 5);
            var idIdnt = charIDToTypeID("Idnt");
            var list62 = new ActionList();
            list62.putInteger(112);
            list62.putInteger(113);
            list62.putInteger(114);
            list62.putInteger(115);
            desc585.putList(idIdnt, list62);
            executeAction(idDplc, desc585, DialogModes.NO);

            app.activeDocument = app.documents[1];
            // var doc = app.activeDocument;
            // for (var x = 0; x < app.activeDocument.layers.length; x++) {
            //     if (app.activeDocument.activeLayer.isBackgroundLayer) {
            //         app.activeDocument.activeLayer.remove();
            //         x=x-1;
            //     }
            // }

            var apl = new AM('application'),
            doc = new AM('document');
            // doc=app.activeDocument;
            for (var x = 0; x < app.activeDocument.layers.length; x++) {
                if (app.activeDocument.activeLayer.isBackgroundLayer) {
                    app.activeDocument.activeLayer.remove();
                }
            }

            if (apl.getProperty('numberOfDocuments')) {
            var docRes = doc.getProperty('resolution'),
                docW = doc.getProperty('width') * docRes / 72,
                docH = doc.getProperty('height') * docRes / 72;
            doc.duplicate();
            doc.resize(docW > docH ? 'width' : 'height', RESIZE_TO);
            var f = new File(Folder.temp + '/colors.raw');
            // doc.flatten();
            
            doc.saveToRAW(f);
            doc.close('no');
            // for (var x = 0; x < app.activeDocument.layers.length; x++) {
            //     if (app.activeDocument.activeLayer.isBackgroundLayer) {
            //         app.activeDocument.activeLayer.remove();
            //         x=x-1;
            //     }
            // }
            var colors = findColors(f);
            // alert (colors);
            // alert (colors[0]);
            // alert (colors[1]);
            // alert (colors[2]);
            // f.remove();
            for (var i = 0; i < colors.length; i++) {
                colors[i][3] = 0;
                for (var x = 0; x < colors.length; x++) {
                    if (x != i && dE(colors[i], colors[x]) <= DE_THRESHOLD) colors[i][3]++
                }
            }
            var result = 0,
                idx = null;
            for (var i = 0; i < colors.length; i++) {
                if (colors[i][3] >= result) { result = colors[i][3]; idx = i; }
            }

            // alert (result);
            var c = new SolidColor;
            c.rgb.red = colors[idx][0],
                c.rgb.green = colors[idx][1],
                c.rgb.blue = colors[idx][2];
            foregroundColor = c;
            // alert (c);
            }

            function findColors(f) {
                var content = '';
                if (f.exists) {
                    f.open('r');
                    f.encoding = "BINARY";
                    content = f.read();
                    f.close();
                    f.remove();
                    return colors = function (s) {
                        var m = [],
                            offset = 0;
                        do {
                            var c = [];
                            for (i = 0; i < 3; i++) {
                                var k = s.charCodeAt(offset + i);
                                c.push(k)
                            };
                            m.push(c)
                            offset += 3;
                        } while (offset < s.length)
                        return m;
                    }(content);
                }
            }

            function dE(a, b) {
                return Math.sqrt(Math.pow(a[0] - b[0], 2) + Math.pow(a[1] - b[1], 2) + Math.pow(a[2] - b[2], 2));
            }

            function AM(target) {
                var s2t = stringIDToTypeID,
                    t2s = typeIDToStringID,
                    c2t = charIDToTypeID;
                target = target ? s2t(target) : null;
                this.getProperty = function (property, id, idxMode) {
                    property = s2t(property);
                    (r = new ActionReference()).putProperty(s2t('property'), property);
                    id != undefined ? (idxMode ? r.putIndex(target, id) : r.putIdentifier(target, id)) :
                        r.putEnumerated(target, s2t('ordinal'), s2t('targetEnum'));
                    return getDescValue(executeActionGet(r), property)
                }
                this.hasProperty = function (property, id, idxMode) {
                    property = s2t(property);
                    (r = new ActionReference()).putProperty(s2t('property'), property);
                    id ? (idxMode ? r.putIndex(target, id) : r.putIdentifier(target, id))
                        : r.putEnumerated(target, s2t('ordinal'), s2t('targetEnum'));
                    return executeActionGet(r).hasKey(property)
                }
                this.resize = function (dimension, value) {
                    (d = new ActionDescriptor()).putUnitDouble(s2t(dimension), s2t("pixelsUnit"), value);
                    d.putBoolean(s2t("constrainProportions"), true);
                    d.putEnumerated(c2t("Intr"), s2t("interpolationType"), s2t("automaticInterpolation"));
                    executeAction(s2t("imageSize"), d, DialogModes.NO);
                }
                this.flatten = function () {
                    executeAction(s2t("flattenImage"), new ActionDescriptor(), DialogModes.NO);
                }
                this.saveToRAW = function (f) {
                    (d = new ActionDescriptor()).putBoolean(s2t('copy'), true);
                    (d1 = new ActionDescriptor()).putObject(s2t("as"), s2t("rawFormat"), d);
                    d1.putPath(s2t("in"), f);
                    executeAction(s2t("save"), d1, DialogModes.NO);
                }
                this.duplicate = function () {
                    (r = new ActionReference()).putEnumerated(target, s2t("ordinal"), s2t("targetEnum"));
                    (d = new ActionDescriptor()).putReference(s2t("null"), r);
                    executeAction(s2t("duplicate"), d, DialogModes.NO);
                }
                this.close = function (yesNo) {
                    (d = new ActionDescriptor()).putEnumerated(s2t("saving"), s2t("yesNo"), s2t(yesNo));
                    executeAction(s2t("close"), d, DialogModes.NO);
                }
                function getDescValue(d, p) {
                    switch (d.getType(p)) {
                        case DescValueType.OBJECTTYPE: return { type: t2s(d.getObjectType(p)), value: d.getObjectValue(p) };
                        case DescValueType.LISTTYPE: return d.getList(p);
                        case DescValueType.REFERENCETYPE: return d.getReference(p);
                        case DescValueType.BOOLEANTYPE: return d.getBoolean(p);
                        case DescValueType.STRINGTYPE: return d.getString(p);
                        case DescValueType.INTEGERTYPE: return d.getInteger(p);
                        case DescValueType.LARGEINTEGERTYPE: return d.getLargeInteger(p);
                        case DescValueType.DOUBLETYPE: return d.getDouble(p);
                        case DescValueType.ALIASTYPE: return d.getPath(p);
                        case DescValueType.CLASSTYPE: return d.getClass(p);
                        case DescValueType.UNITDOUBLE: return (d.getUnitDoubleValue(p));
                        case DescValueType.ENUMERATEDTYPE: return { type: t2s(d.getEnumerationType(p)), value: t2s(d.getEnumerationValue(p)) };
                        default: break;
                    };
                }
            }

            app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
        }
    }
}



So can you please modify this code so that it can currectly pickup the colors in all the cases?

Thanks in advance...
 

TOPICS
Actions and scripting

Views

489
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, 2023 Sep 07, 2023

 

 

app.activeDocument = app.documents[0];

 

 

seems like a problematic way to address an image. 

How do you know documents[0] is the relevant open image? 

 

Your primary complaint (»for each layer I am duplicating the layer into a new document and processing the same logic that is applicable for a whole document«) seems a bit strange to me – if you want to get the values for each Layer then what else do you expect to do than process each Layer individually? 

 

And it still seems somewhat unclea

...

Votes

Translate
Adobe
Explorer ,
Sep 07, 2023 Sep 07, 2023

Copy link to clipboard

Copied

Hi

I have now corrected the code, it's now processing each layer and correctly determining the layer color.

But as you can see, for each layer I am duplicating the layer into a new document and processing the same logic that is applicable for a whole document. So this is not a very good piece of code!

Can we do it in a nicer way?

 

const DE_THRESHOLD = 15, RESIZE_TO = 35;

for (var x=0; x<app.activeDocument.layerSets.length; x++) {
    var layerSet = app.activeDocument.layerSets[x];
    if (layerSet.name=="Images" || layerSet.name=="Images_1" || layerSet.name=="Images_2") {
        for (var layerIndex = 0; layerIndex < layerSet.layers.length; layerIndex++) {
            var layer = layerSet.layers[layerIndex];
            app.activeDocument.activeLayer = layer;
            var bounds = layer.bounds;
            var width = bounds[2].value - bounds[0].value;
            var height = bounds[3].value - bounds[1].value;
            app.documents.add( width, height, 300, "temp",NewDocumentMode.RGB, DocumentFill.WHITE,1.0, BitsPerChannelType.EIGHT, "Adobe RGB (1998)" );
            
            app.activeDocument = app.documents[0];
            var idslct = charIDToTypeID( "slct" );
            var desc219 = new ActionDescriptor();
            var idnull = charIDToTypeID( "null" );
            var ref1 = new ActionReference();
            var idLyr = charIDToTypeID( "Lyr " );
            ref1.putName( idLyr, layer.name);
            desc219.putReference( idnull, ref1 );
            var idMkVs = charIDToTypeID( "MkVs" );
            desc219.putBoolean( idMkVs, false );
            var idLyrI = charIDToTypeID( "LyrI" );
            var list4 = new ActionList();
            list4.putInteger( 13 );
            desc219.putList( idLyrI, list4 );
            executeAction( idslct, desc219, DialogModes.NO );
    
            // =======================================================
            var idDplc = charIDToTypeID("Dplc");
            var desc585 = new ActionDescriptor();
            var idnull = charIDToTypeID("null");
            var ref123 = new ActionReference();
            var idLyr = charIDToTypeID("Lyr ");
            var idOrdn = charIDToTypeID("Ordn");
            var idTrgt = charIDToTypeID("Trgt");
            ref123.putEnumerated(idLyr, idOrdn, idTrgt);
            desc585.putReference(idnull, ref123);
            var idT = charIDToTypeID("T   ");
            var ref124 = new ActionReference();
            var idDcmn = charIDToTypeID("Dcmn");
            ref124.putName(idDcmn, "temp");
            desc585.putReference(idT, ref124);
            var idNm = charIDToTypeID("Nm  ");
            desc585.putString(idNm, layer.name);
            var idVrsn = charIDToTypeID("Vrsn");
            desc585.putInteger(idVrsn, 5);
            var idIdnt = charIDToTypeID("Idnt");
            var list62 = new ActionList();
            list62.putInteger(112);
            list62.putInteger(113);
            list62.putInteger(114);
            list62.putInteger(115);
            desc585.putList(idIdnt, list62);
            executeAction(idDplc, desc585, DialogModes.NO);

            app.activeDocument = app.documents[1];
            var doc = app.activeDocument;
            for (var x = 0; x < app.activeDocument.layers.length; x++) {
                var layer = app.activeDocument.layers[x];
                if (layer.isBackgroundLayer) {
                    layer.remove();
                }
            }

            processEachLayer();

            app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
        }
    }
}

function processEachLayer () {
    var apl = new AM('application'),
            doc = new AM('document');
            // doc=app.activeDocument;

            if (apl.getProperty('numberOfDocuments')) {
            var docRes = doc.getProperty('resolution'),
                docW = doc.getProperty('width') * docRes / 72,
                docH = doc.getProperty('height') * docRes / 72;
            doc.duplicate();
            doc.resize(docW > docH ? 'width' : 'height', RESIZE_TO);
            var f = new File(Folder.temp + '/colors.raw');
            // doc.flatten();
            
            doc.saveToRAW(f);
            doc.close('no');
            
            var colors = findColors(f);
            // alert (colors);
            // alert (colors[0]);
            // alert (colors[1]);
            // alert (colors[2]);
            // f.remove();
            for (var i = 0; i < colors.length; i++) {
                colors[i][3] = 0;
                for (var x = 0; x < colors.length; x++) {
                    if (x != i && dE(colors[i], colors[x]) <= DE_THRESHOLD) colors[i][3]++
                }
            }
            var result = 0,
                idx = null;
            for (var i = 0; i < colors.length; i++) {
                if (colors[i][3] >= result) { result = colors[i][3]; idx = i; }
            }

            // alert (result);
            var c = new SolidColor;
            c.rgb.red = colors[idx][0],
                c.rgb.green = colors[idx][1],
                c.rgb.blue = colors[idx][2];
            foregroundColor = c;
            // alert (c);
            }

            function findColors(f) {
                var content = '';
                if (f.exists) {
                    f.open('r');
                    f.encoding = "BINARY";
                    content = f.read();
                    f.close();
                    f.remove();
                    return colors = function (s) {
                        var m = [],
                            offset = 0;
                        do {
                            var c = [];
                            for (i = 0; i < 3; i++) {
                                var k = s.charCodeAt(offset + i);
                                c.push(k)
                            };
                            m.push(c)
                            offset += 3;
                        } while (offset < s.length)
                        return m;
                    }(content);
                }
            }

            function dE(a, b) {
                return Math.sqrt(Math.pow(a[0] - b[0], 2) + Math.pow(a[1] - b[1], 2) + Math.pow(a[2] - b[2], 2));
            }

            function AM(target) {
                var s2t = stringIDToTypeID,
                    t2s = typeIDToStringID,
                    c2t = charIDToTypeID;
                target = target ? s2t(target) : null;
                this.getProperty = function (property, id, idxMode) {
                    property = s2t(property);
                    (r = new ActionReference()).putProperty(s2t('property'), property);
                    id != undefined ? (idxMode ? r.putIndex(target, id) : r.putIdentifier(target, id)) :
                        r.putEnumerated(target, s2t('ordinal'), s2t('targetEnum'));
                    return getDescValue(executeActionGet(r), property)
                }
                this.hasProperty = function (property, id, idxMode) {
                    property = s2t(property);
                    (r = new ActionReference()).putProperty(s2t('property'), property);
                    id ? (idxMode ? r.putIndex(target, id) : r.putIdentifier(target, id))
                        : r.putEnumerated(target, s2t('ordinal'), s2t('targetEnum'));
                    return executeActionGet(r).hasKey(property)
                }
                this.resize = function (dimension, value) {
                    (d = new ActionDescriptor()).putUnitDouble(s2t(dimension), s2t("pixelsUnit"), value);
                    d.putBoolean(s2t("constrainProportions"), true);
                    d.putEnumerated(c2t("Intr"), s2t("interpolationType"), s2t("automaticInterpolation"));
                    executeAction(s2t("imageSize"), d, DialogModes.NO);
                }
                this.flatten = function () {
                    executeAction(s2t("flattenImage"), new ActionDescriptor(), DialogModes.NO);
                }
                this.saveToRAW = function (f) {
                    (d = new ActionDescriptor()).putBoolean(s2t('copy'), true);
                    (d1 = new ActionDescriptor()).putObject(s2t("as"), s2t("rawFormat"), d);
                    d1.putPath(s2t("in"), f);
                    executeAction(s2t("save"), d1, DialogModes.NO);
                }
                this.duplicate = function () {
                    (r = new ActionReference()).putEnumerated(target, s2t("ordinal"), s2t("targetEnum"));
                    (d = new ActionDescriptor()).putReference(s2t("null"), r);
                    executeAction(s2t("duplicate"), d, DialogModes.NO);
                }
                this.close = function (yesNo) {
                    (d = new ActionDescriptor()).putEnumerated(s2t("saving"), s2t("yesNo"), s2t(yesNo));
                    executeAction(s2t("close"), d, DialogModes.NO);
                }
                function getDescValue(d, p) {
                    switch (d.getType(p)) {
                        case DescValueType.OBJECTTYPE: return { type: t2s(d.getObjectType(p)), value: d.getObjectValue(p) };
                        case DescValueType.LISTTYPE: return d.getList(p);
                        case DescValueType.REFERENCETYPE: return d.getReference(p);
                        case DescValueType.BOOLEANTYPE: return d.getBoolean(p);
                        case DescValueType.STRINGTYPE: return d.getString(p);
                        case DescValueType.INTEGERTYPE: return d.getInteger(p);
                        case DescValueType.LARGEINTEGERTYPE: return d.getLargeInteger(p);
                        case DescValueType.DOUBLETYPE: return d.getDouble(p);
                        case DescValueType.ALIASTYPE: return d.getPath(p);
                        case DescValueType.CLASSTYPE: return d.getClass(p);
                        case DescValueType.UNITDOUBLE: return (d.getUnitDoubleValue(p));
                        case DescValueType.ENUMERATEDTYPE: return { type: t2s(d.getEnumerationType(p)), value: t2s(d.getEnumerationValue(p)) };
                        default: break;
                    };
                }
            }
}

 

Many thanks in advance...

Votes

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

Copy link to clipboard

Copied

 

 

app.activeDocument = app.documents[0];

 

 

seems like a problematic way to address an image. 

How do you know documents[0] is the relevant open image? 

 

Your primary complaint (»for each layer I am duplicating the layer into a new document and processing the same logic that is applicable for a whole document«) seems a bit strange to me – if you want to get the values for each Layer then what else do you expect to do than process each Layer individually? 

 

And it still seems somewhat unclear what you are doing with this Script – you are setting the Foreground Color repeatedly so what is the point? 

I inserted an alert but otherwise this might run slightly faster. (updated the code 2023-09-08)

 

 

 

const DE_THRESHOLD = 15, RESIZE_TO = 35;
if (app.documents.length > 0) {
    var myDocument = app.activeDocument;
    var theLayers = collectLayersNamesIndexOfCertainParentName ("Images");
    var theResults = new Array;
    for (var m = 0; m < theLayers.length; m++) {
        var thisOne = theLayers[m];
// duplicate layer;
        selectLayerByID(thisOne[2], false);
        var desc229 = new ActionDescriptor();
            var ref3 = new ActionReference();
            ref3.putClass( stringIDToTypeID( "document" ) );
        desc229.putReference( stringIDToTypeID( "null" ), ref3 );
        desc229.putString( stringIDToTypeID( "name" ), "temp" );
            var ref4 = new ActionReference();
            ref4.putEnumerated( stringIDToTypeID( "layer" ), stringIDToTypeID( "ordinal" ), stringIDToTypeID( "targetEnum" ) );
        desc229.putReference( stringIDToTypeID( "using" ), ref4 );
    executeAction( stringIDToTypeID( "make" ), desc229, DialogModes.NO );
// trim transparent;
    var desc232 = new ActionDescriptor();
    desc232.putEnumerated(stringIDToTypeID( "trimBasedOn" ), stringIDToTypeID( "trimBasedOn" ), stringIDToTypeID( "transparency" ));
    desc232.putBoolean( stringIDToTypeID( "top" ), true );
    desc232.putBoolean( stringIDToTypeID( "bottom" ), true );
    desc232.putBoolean( stringIDToTypeID( "left" ), true );
    desc232.putBoolean( stringIDToTypeID( "right" ), true );
executeAction( stringIDToTypeID( "trim" ), desc232, DialogModes.NO );
// get color;
        theResults.push([thisOne[0], processEachLayer()]);
    };
    alert ("the colors\n"+theResults.join("\n"))
};

function processEachLayer () {
    var apl = new AM('application'),
    doc = new AM('document');
    // doc=app.activeDocument;

    if (apl.getProperty('numberOfDocuments')) {
    var docRes = doc.getProperty('resolution'),
        docW = doc.getProperty('width') * docRes / 72,
        docH = doc.getProperty('height') * docRes / 72;
//    doc.duplicate();
    doc.resize(docW > docH ? 'width' : 'height', RESIZE_TO);
    var f = new File(Folder.temp + '/colors.raw');
    // doc.flatten();
    
    doc.saveToRAW(f);
    doc.close('no');
    
    var colors = findColors(f);
    f.remove();
    for (var i = 0; i < colors.length; i++) {
        colors[i][3] = 0;
        for (var x = 0; x < colors.length; x++) {
            if (x != i && dE(colors[i], colors[x]) <= DE_THRESHOLD) colors[i][3]++
        }
    }
    var result = 0,
        idx = null;
    for (var i = 0; i < colors.length; i++) {
        if (colors[i][3] >= result) { result = colors[i][3]; idx = i; }
    }

    var c = new SolidColor;
    c.rgb.red = colors[idx][0],
        c.rgb.green = colors[idx][1],
        c.rgb.blue = colors[idx][2];
    //foregroundColor = c;
    return [c.rgb.red, c.rgb.green, c.rgb.blue]
    }

    function findColors(f) {
        var content = '';
        if (f.exists) {
            f.open('r');
            f.encoding = "BINARY";
            content = f.read();
            f.close();
            f.remove();
            return colors = function (s) {
                var m = [],
                    offset = 0;
                do {
                    var c = [];
                    for (i = 0; i < 3; i++) {
                        var k = s.charCodeAt(offset + i);
                        c.push(k)
                    };
                    m.push(c)
                    offset += 3;
                } while (offset < s.length)
                return m;
            }(content);
        }
    }

    function dE(a, b) {
        return Math.sqrt(Math.pow(a[0] - b[0], 2) + Math.pow(a[1] - b[1], 2) + Math.pow(a[2] - b[2], 2));
    }

    function AM(target) {
        var s2t = stringIDToTypeID,
            t2s = typeIDToStringID,
            c2t = charIDToTypeID;
        target = target ? s2t(target) : null;
        this.getProperty = function (property, id, idxMode) {
            property = s2t(property);
            (r = new ActionReference()).putProperty(s2t('property'), property);
            id != undefined ? (idxMode ? r.putIndex(target, id) : r.putIdentifier(target, id)) :
                r.putEnumerated(target, s2t('ordinal'), s2t('targetEnum'));
            return getDescValue(executeActionGet(r), property)
        }
        this.hasProperty = function (property, id, idxMode) {
            property = s2t(property);
            (r = new ActionReference()).putProperty(s2t('property'), property);
            id ? (idxMode ? r.putIndex(target, id) : r.putIdentifier(target, id))
                : r.putEnumerated(target, s2t('ordinal'), s2t('targetEnum'));
            return executeActionGet(r).hasKey(property)
        }
        this.resize = function (dimension, value) {
            (d = new ActionDescriptor()).putUnitDouble(s2t(dimension), s2t("pixelsUnit"), value);
            d.putBoolean(s2t("constrainProportions"), true);
            d.putEnumerated(c2t("Intr"), s2t("interpolationType"), s2t("automaticInterpolation"));
            executeAction(s2t("imageSize"), d, DialogModes.NO);
        }
        this.flatten = function () {
            executeAction(s2t("flattenImage"), new ActionDescriptor(), DialogModes.NO);
        }
        this.saveToRAW = function (f) {
            (d = new ActionDescriptor()).putBoolean(s2t('copy'), true);
            (d1 = new ActionDescriptor()).putObject(s2t("as"), s2t("rawFormat"), d);
            d1.putPath(s2t("in"), f);
            executeAction(s2t("save"), d1, DialogModes.NO);
        }
        this.duplicate = function () {
            (r = new ActionReference()).putEnumerated(target, s2t("ordinal"), s2t("targetEnum"));
            (d = new ActionDescriptor()).putReference(s2t("null"), r);
            executeAction(s2t("duplicate"), d, DialogModes.NO);
        }
        this.close = function (yesNo) {
            (d = new ActionDescriptor()).putEnumerated(s2t("saving"), s2t("yesNo"), s2t(yesNo));
            executeAction(s2t("close"), d, DialogModes.NO);
        }
        function getDescValue(d, p) {
            switch (d.getType(p)) {
                case DescValueType.OBJECTTYPE: return { type: t2s(d.getObjectType(p)), value: d.getObjectValue(p) };
                case DescValueType.LISTTYPE: return d.getList(p);
                case DescValueType.REFERENCETYPE: return d.getReference(p);
                case DescValueType.BOOLEANTYPE: return d.getBoolean(p);
                case DescValueType.STRINGTYPE: return d.getString(p);
                case DescValueType.INTEGERTYPE: return d.getInteger(p);
                case DescValueType.LARGEINTEGERTYPE: return d.getLargeInteger(p);
                case DescValueType.DOUBLETYPE: return d.getDouble(p);
                case DescValueType.ALIASTYPE: return d.getPath(p);
                case DescValueType.CLASSTYPE: return d.getClass(p);
                case DescValueType.UNITDOUBLE: return (d.getUnitDoubleValue(p));
                case DescValueType.ENUMERATEDTYPE: return { type: t2s(d.getEnumerationType(p)), value: t2s(d.getEnumerationValue(p)) };
                default: break;
            };
        }
    }
};
////// collect layers //////
function collectLayersNamesIndexOfCertainParentName (parentName) {
// 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 theParentId = layerDesc.getInteger(stringIDToTypeID('parentLayerID'));
var groupName = getLayerNameById (theParentId);
if (groupName.match(parentName) != -1) {theLayers.push([theName, theIndex, theID, groupName, theParentId])}
};
}
catch (e) {};
};
return theLayers
};
////// get layer’s name by id //////
function getLayerNameById (theId) {
    var ref = new ActionReference();
    ref.putIdentifier( charIDToTypeID("Lyr "), theId); 
    var layerDesc = executeActionGet(ref);
    var theName = layerDesc.getString(stringIDToTypeID('name'));
    return theName
};
////// based on code by mike hale, via paul riggott //////
function selectLayerByID(id,add){
try {
add = undefined ? add = false:add 
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID("Lyr "), id);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID("null"), ref );
if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) ); 
desc.putBoolean( charIDToTypeID( "MkVs" ), false ); 
try{
executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message); 
}
} catch (e) {}
};

 

 

Votes

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 ,
Sep 11, 2023 Sep 11, 2023

Copy link to clipboard

Copied

Hi @c.pfaffenbichler 

Thanks a lot for your generous help, much thankful!

My program logic is that I need to check the colors of each layer, and if they are not white or grey or black i.e. if the red, green and blue values are not the same, then change their colors. Hence I need to check the colors of all the layers in a loop.

I have check many sample codes posted in this forum with the following lines -

var apl = new AM('application'),
doc = new AM('document'),
lr = new AM('layer'),

For example, this one -
Solved: Re: Script that updates (Copy/Paste) masks on mul... - Adobe Support Community - 12313689


So I was thinking instead of duplicating each layer as a new document, if we can directly process each layer without creating a new document, the code would run much faster. So our processEachLayer() function should be changed. But anyway, since this code is working fine, I finally gave up after trying for three days!

 

Thanks again for your time and eagerness to solve this issue. I am really really thankful!

Have a nice day.

Nirmalya

Votes

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 11, 2023 Sep 11, 2023

Copy link to clipboard

Copied

LATEST

I guess you could avoid the duplication by hiding all other Layers and saving the RAW as a copy. 

Votes

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