-1

As the title says, I need someone to give me an explanation of this function below, line by line, for learning purposes.

Thank you in advance!!!

function getAllShapeLayersData() {
    var lyrs = [];
    try {
        activeDocument.backgroundLayer;
        var layers = 0
    }
    catch (e) {
        var layers = 1;
    };
    while (true) {
        ref = new ActionReference();
        ref.putIndex(charIDToTypeID('Lyr '), layers);
        try {
            var desc = executeActionGet(ref);
        }
        catch (err) {
            break;
        }

        var lyr = {};
        lyr.type = desc.getInteger(stringIDToTypeID("layerKind"));
        lyr.name = desc.getString(charIDToTypeID("Nm  "));
        lyr.id = desc.getInteger(stringIDToTypeID("layerID"));

        if (lyr.type == 4) { // shape layer
            var adj = desc.getList(stringIDToTypeID("adjustment")).getObjectValue(0);

            if (adj.hasKey(stringIDToTypeID("color"))) {
                var curColor = new SolidColor();
                curColor.rgb.red = adj.getObjectValue(stringIDToTypeID("color")).getUnitDoubleValue(stringIDToTypeID("red"));
                curColor.rgb.green = adj.getObjectValue(stringIDToTypeID("color")).getUnitDoubleValue(stringIDToTypeID("grain"));
                curColor.rgb.blue = adj.getObjectValue(stringIDToTypeID("color")).getUnitDoubleValue(stringIDToTypeID("blue"));
                lyr.color = curColor;
                lyrs.push(lyr);
            }
        }
        layers++;
    }
    return lyrs
}
4
// make new function named getAllShapeLayersData
function getAllShapeLayersData() {

    // make empty list lyrs (for returning later)
    var lyrs = [];

    //if the thing in try fails go to catch
    try {
        // get the background layer
        activeDocument.backgroundLayer;
        var layers = 0
    }
    catch (e) {
        // Ok so there was no background layer
        var layers = 1;
        // note: this is probably copied code one
        // could skip this the shape layer is unlikely 
        // to be a background layer cos it can not be. 
        // But ok defensive coding is fine
    };
    // ok lets loop untill we reach a future condition
    while (true) {

        // lets prepare a object cor accepting the hooks for 
        // the nonscripting  raw c API, you can find these functions 
        // with the script listener plugin, they are a bit 
        // clunky cos its c++ not really javascript.
        ref = new ActionReference();
        // Fetch layer with index layers
        ref.putIndex(charIDToTypeID('Lyr '), layers);
        try {
            // get the raw api object
            var desc = executeActionGet(ref);
        }
        catch (err) {
            // well that didn't work lets quit the loop
            break;
        }

        // Lets make a object lyr for easier use that has
        var lyr = {};
        // members type kind and raw c API ID
        lyr.type = desc.getInteger(stringIDToTypeID("layerKind"));
        lyr.name = desc.getString(charIDToTypeID("Nm  "));
        lyr.id = desc.getInteger(stringIDToTypeID("layerID"));

        if (lyr.type == 4) { // shape layer

            // get the adjustment layer (fill in most cases)
            var adj = desc.getList(stringIDToTypeID("adjustment")).getObjectValue(0);

            // well ok so we dont actually know what adjustment layer we have.
            // But does it have a color value we can change? (i.e. a fill)
            if (adj.hasKey(stringIDToTypeID("color"))) {
                // buld a color
                var curColor = new SolidColor();
                // fetch red, green and blue color
                curColor.rgb.red = adj.getObjectValue(stringIDToTypeID("color")).getUnitDoubleValue(stringIDToTypeID("red"));
                curColor.rgb.green = adj.getObjectValue(stringIDToTypeID("color")).getUnitDoubleValue(stringIDToTypeID("grain"));
                curColor.rgb.blue = adj.getObjectValue(stringIDToTypeID("color")).getUnitDoubleValue(stringIDToTypeID("blue"));
                // add it to the object we created with type, name, id
                lyr.color = curColor;
                // add it to the return list
                lyrs.push(lyr);
            }
        } // exit both ifs
        // increment layers by one 
        layers++;
    } // and loop
    // return a list of objects that have name, id , type and color of layer.
    return lyrs
}

Thats it.

| improve this answer | |
  • Thank you very much for your time!!! This if (lyr.type == 4) how do we know that is a shape layer? Is there any list so to know what is what? Or, is there any way to do something like this if (lyr.type == LayerKind.SOLIDFILL)? – Simonetos The Greek Jul 5 '19 at 7:32
  • 1
    @SimonetosTheGreek No, different API. – joojaa Jul 5 '19 at 7:48
  • 1
    Well presumably the original script maker iterated trough every object they have and printed the type value and looked at the name and type. – joojaa Jul 5 '19 at 7:52
  • 1
    @SimonetosTheGreek then you add a if inside the if after the color is fetched. – joojaa Jul 5 '19 at 8:04
  • 1
    @joojaa commented code are always welcomed for someone like me who still learning :D loved explaination +1 for that – Mr.Online Jul 5 '19 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.