1

I have this function below and before it calls bevel(); function, I want first to add an alert where it says how many layers going to be change...

function goThroughLayers(parentLayer){
    for(var i=0; i<parentLayer.layers.length; i++){
        curLayer = parentLayer.layers[i];
        doc.activeLayer = curLayer;         
        if(curLayer.typename == 'LayerSet'){
            goThroughLayers (curLayer)
        }else{
            if(curLayer.parent.name == 'Group 2' && curLayer.name == 'Rectangle 1' && curLayer.kind == LayerKind.SOLIDFILL){                  
                bevel ();
            }
        }
    }
}

Any idea how can I do this?

7
  • 1
    Then you would need to loop 2 times. To collect the actual matching layers
    – joojaa
    Commented Jul 4, 2019 at 9:52
  • 1
    you can make a temp array like var temp = [] and then you can add one command temp.push(curLayer) and in your alert you can pass alert(temp.length)
    – Mr.Online
    Commented Jul 4, 2019 at 9:54
  • 1
    it will be fine if you can share full code and situation so we can reproduce because this current one doesn't helping me to understand clearly!
    – Mr.Online
    Commented Jul 4, 2019 at 10:54
  • 1
    I made it!!! I did what you both said... I'll "spruce up" my code and I'll add it here if someone else need it... Commented Jul 4, 2019 at 11:41
  • 1
    by running your code i figured that if and only if your layer name is Rectangle 1 shape layer inside of Group 2 then it should get Bevel effect now where especially and why you want to get count of layer>]
    – Mr.Online
    Commented Jul 4, 2019 at 11:42

1 Answer 1

2

Well, I made two loops. One for count the layers that are going to change and one that adds an effect to specific layers as joojaa said. I also used push() method as Design Phoenix said. Here is my code...

function main(){

    var doc = activeDocument;
    var count = [];
    var curLayerA;   
    var curLayerB;

    goThroughLayersA(doc);   

    if (confirm('You are about to add selected effect on ' + count.length + ' layers. Do you want to continue?', false, 'Message')) {
        goThroughLayersB(doc); 
    }
    else {
        return;
    }

    function goThroughLayersA(parentLayer){
        for(var i = 0; i < parentLayer.layers.length; i++){
            curLayerA = parentLayer.layers[i];
            doc.activeLayer = curLayerA;         
            if(curLayerA.typename == 'LayerSet'){
                goThroughLayersA (curLayerA)
            }else{
                if(curLayerA.parent.name == 'Group 2' && curLayerA.name == 'Rectangle 1' && curLayerA.kind == LayerKind.SOLIDFILL){
                    count.push(curLayerA);
                }
            }
        }
    }   

    function goThroughLayersB(parentLayer){
        for(var i = 0; i < parentLayer.layers.length; i++){
            curLayerB = parentLayer.layers[i];
            doc.activeLayer = curLayerB;         
            if(curLayerB.typename == 'LayerSet'){
                goThroughLayersB (curLayerB)
            }else{
                if(curLayerB.parent.name == 'Group 2' && curLayerB.name == 'Rectangle 1' && curLayerB.kind == LayerKind.SOLIDFILL){
                    bevel();
                }
            }
        }
    }

    function bevel(){
        // Code for bevel and emboss effect here!!!
    } 

}
main();

If you believe that the code is okay and nothing better can be done, please let me know so to mark the answer as correct.

6
  • 1
    You should probably code this so that goThroughLayers accepts a function instead of repeating everything.
    – joojaa
    Commented Jul 4, 2019 at 17:07
  • @joojaa can you give me an example? Commented Jul 4, 2019 at 17:35
  • 1
    Something like this
    – joojaa
    Commented Jul 4, 2019 at 18:13
  • I tested your code and it works only if I uncomment doc.activeLayer = curLayer;!!! Commented Jul 4, 2019 at 18:58
  • That sounds weird. Works fine for me without it.
    – joojaa
    Commented Jul 5, 2019 at 6:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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