Take the 2-minute tour ×
Graphic Design Stack Exchange is a question and answer site for Graphic Design professionals, students, and enthusiasts. It's 100% free, no registration required.

I'm trying to iterate through every layer in a document and for some reason my script only goes through the first half (13 out of 25) and then at 14 it says that the element doesn't exist. Could you guys help me figure out why? Below is the code snippet which goes through each layer

function processLayers(extension)
{
    var doc = app.activeDocument;
    //go through each layer and create a folder
    alert('length is ' + doc.artLayers.length);
    var length = doc.artLayers.length;
    for (var layer = 0; layer < length; layer++)
    {
        var folderName = generateFolderName(doc.artLayers[layer].name, extension);
        //create folder in photoshop with folderName
        var folder = doc.layerSets.add();
        folder.name = folderName;
        doc.artLayers[layer].move(folder, ElementPlacement.INSIDE);     
    }
}
share|improve this question

3 Answers 3

There are lots of reasons a script may stop prematurely. Often times it depends on the document. In this case, it could be a bad character in a layer name. Try killing layer 14 and see what happens.

To debug, run your script from Adobe's ExtendScript Tool Kit. You can just let it get stuck or you can set specific breaks to inspect particular points in the process.

share|improve this answer
    
deleting layer 14 just caused it to crash on layer 13 the next time, and i am debugging from extendscript - its crashing on generateFolderName(doc.artLayers[layer].name, extension); and saying that the item doesn't exist –  daveroz Aug 13 '13 at 22:29
    
Do you see that object available in the Data Browser? I'm not familiar with the extension element, is there any chance that's causing you trouble? Is the script processing in the correct order, or is there a change it's getting ahead of itself in the array? So many questions ... –  plainclothes Aug 13 '13 at 23:43
    
extension seems to just be a parameter, most likely a string, that is used to make a name in combination with the layer name. –  Johannes Aug 14 '13 at 16:35
    
I figured as much, but we really can't solve his issue without knowing what all the variables are doing. –  plainclothes Aug 14 '13 at 17:17

Since I don't have a working copy of your script this is a little difficult for me, but I wrote a script before and here's a function that goes throw and hides all layers:

function hideLayers(ref) {
    var len = ref.layers.length;
    for (var i = 0; i < len; i++) {
        var layer = ref.layers[i];
        if (layer.typename == 'LayerSet') hideLayers(layer);
        else layer.visible = false;
    }
}

You'll notice that it simple grabs the length of the layers object, and then iterates over each layer. In that regard out scripts are nearly identical. If a layer group is encountered (as check by the if statement) then it does a recursive call to hide all nested layers. (Also my ref parameter is activeDocument).

What I would suggest, for debugging purposes, is see if you can run my function without any errors. If so, then you know the issue isn't with iterating over all the layers, but rather with the logic you are trying to execute on each layer, and then make sure that whatever logic you're trying to use is valid on that type of layer (raster, text, shape, etc.)

Another technique would be to fill your loop with helpful alerts (or better yet just logs or breakpoints), and check the value of the variables as they are coming through.

share|improve this answer

also add it could be a grouping issue. You may need to isolate everything on one layer and have nothing in a folder.

share|improve this answer
    
could you elaborate on this? –  daveroz Aug 13 '13 at 22:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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