0

I have this function which gets all shape layers of a document...

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

I changes to this if (lyr.name == "Something") { instead of if (lyr.type == 4) { so now I managed to get all layers by the name "Something". I tried something like this if (lyr.name.includes("Stitches")) { so to get all layers where their names contains the word "Something", but I get an error!!!

So, how can I get all shape layers where their names contains specific word? In other words, first, how can I use if (lyr.type == 4) { and if (lyr.name.includes("Stitches")) { together and second what is the correct syntax of the "include" part?

3

Photoshop uses ancient JS engine, something like ES3, so there's no .include(). You can use String.indexOf() instead:

if (lyr.name.indexOf("Stitches") !== -1) {
    //"Stitches" is found in the name
}
  • 1
    did you remove if (lyr.type == 4)? – Sergey Kritskiy Jun 26 at 10:21
  • 1
    Of course, otherwise it's going to try to get a property that only exists on Shape layers (that are type 4 in PS nomenclature). – Sergey Kritskiy Jun 26 at 10:24
  • 1
    Syntax is if (condition1 && condition2) {}. That's quite a basic thing, if you're asking this I'd recommend to take a course on JavaScript first. – Sergey Kritskiy Jun 26 at 10:25
  • 2
    Please edit your post with the code that produces the error, otherwise it's impossible to tell what went wrong. – Sergey Kritskiy Jun 26 at 10:28
  • 1
    No it's okay now... Obviously was my fault, it was a typo. Sorry and thank you very much!!! – Simonetos The Greek Jun 26 at 10:32
1

Use lyr.name.match("string") :)

  • I get the same error as with Sergey example. It stops here var adj = desc.getList(stringIDToTypeID("adjustment")).getObjectValue(0); and says "The requested property does not exist". – Simonetos The Greek Jun 26 at 10:23
  • 1
    sergey already provide double if! hope it resolved your issue :D – Mr.Online Jun 26 at 10:27
  • 1
    Your example "works" too!!! Thank you very much!!! It was my fault, I had a typo in "double if". I accepted Sergeys answer as correct because he answered first, by seconds. :) – Simonetos The Greek Jun 26 at 10:38
  • don't worry at least i also learned something from Sergey :D – Mr.Online Jun 26 at 10:40

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.