Mobile app version of vmapp.org
Login or Join
Speyer780

: Script iterating through layers stops halfway 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

@Speyer780

Posted in: #AdobePhotoshop #Layers #PhotoshopScripting

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

10.04% popularity Vote Up Vote Down


Login to follow query

More posts by @Speyer780

4 Comments

Sorted by latest first Latest Oldest Best

 

@Heady304

Inserting layers into folder groups is the workflow when importing PSD's into ToonBoom Harmony. Looking at the code above, iterating forward seems to be the problem, as each time you add a group, it's probably adding to the layer count. Going backwards solves the issue for me! hope this helps.

function PrepLayersForToonBoomImport()
{
var doc = app.activeDocument;
//go through each layer and create a folder
//alert('length is ' + doc.artLayers.length);
var length = doc.artLayers.length-1;
for (var layer = length; layer >= 0 ; layer--)
{
var folderName = doc.artLayers[layer].name;
//create folder in photoshop with folderName
var folder = doc.layerSets.add();
folder.name = folderName;
doc.artLayers[layer].move(folder, ElementPlacement.INSIDE);
}
}

PrepLayersForToonBoomImport()

10% popularity Vote Up Vote Down


 

@Speyer780

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.

10% popularity Vote Up Vote Down


 

@Sent7350415

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

10% popularity Vote Up Vote Down


 

@Ravi4787994

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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme