Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to flatten an array in Photoshop?

Here is my code :

var doc = app.activeDocument;
var allLayers = new Array;
var allLayers = collectAllLayers(doc, allLayers);

function collectAllLayers (doc, allLayers){
    for (var m = 0; m < doc.layers.length; m++){
        var theLayer = doc.layers[m];
        if (theLayer.typename === "ArtLayer"){
            allLayers.push(theLayer);
        }else{
            collectAllLayers(theLayer, allLayers);
        }
    }
    return allLayers;
}
alert("array_layers : " + allLayers);

I am getting in alert array like this

[Layer1],[Layer2],[Layer3];

and I want make it looks like this :

[Layer1,Layer2,Layer3];

Thanks for answers and help in advance!

like image 656
bafix2203 Avatar asked Nov 08 '22 02:11

bafix2203


1 Answers

The code in question already works correctly. Referring to the documentation on ArtLayer, you can prove this by printing some of the properties for each of the objects in the array:

function collectAllLayers (layerSet, layers){
    for (var i = 0; i < layerSet.layers.length; i++){
        var layer = layerSet.layers[i];

        if (layer.typename === "ArtLayer"){
            layers.push(layer);
        } else {
            collectAllLayers(layer, layers);
        }
    }

    return layers;
}

function printable (artLayers) {
    var layerDescriptions = [];

    for (var i = 0; i < artLayers.length; i++) {
        var layer = artLayers[i];

        layerDescriptions.push(
            '{ name: ' + layer.name +
            ', kind: ' + layer.kind +
            ', opacity: ' + layer.opacity +
            ', visible: ' + layer.visible +
            ' }'
        );
    }

    return layerDescriptions;
}

var artLayers = collectAllLayers(app.activeDocument, []);
var layerDescriptions = printable(artLayers);

alert(layerDescriptions);
like image 76
Patrick Roberts Avatar answered Nov 14 '22 22:11

Patrick Roberts