CS5 Hiding layers is painfully slow

flash-cs5, javascript, optimization, photoshop, photoshop-script

Solution

The only thing I can think of is try try to loop through the individual layers in `app.activeDocument.layers` which contains all the layers and groups. When you do this you'll notice that grouped layers will still retain their original `visible` property but are hidden because their parent group is hidden.

#target photoshop

var myLayers = app.activeDocument.layers;
var myLayersLength = myLayers.length;

for (var i=0; i<myLayersLength; i++) {
    myLayers[i].visible = false;
}

EDIT: So I tested this solution on a 400mb file with 50 layers and it worked in seriously less than a second. Are you sure the problem is with Photoshop?

If you have to iterate through every single layer and child-layer individually to perform an action you can do it recursively:

#target photoshop

var doc = app.activeDocument;
findLayers(doc);


function findLayers(set) {
    for (var i=0; i<set.layerSets.length; i++) {

        //recursive call
        findLayers(set.layerSets[i]);

        //iterate sub-layers and hide
        for (var j=0; j<set.layerSets[i].layers.length; j++) {
            set.layerSets[i].layers[j].visible = false;
        }
    }

    //hide top-level layers
    for (var l=0; l<set.layers.length; l++) {
        set.layers[l].visible = false;
    }
}

This takes a bit longer, ~20 seconds on my machine, but it will hit every single layer in a document.

NOTE: I also tested your original scripts from the question and they don't work on un-grouped layers because you're iterating through `document.layerSets` instead of `document.layers`

Problem

Is it only me that thinks the CS5 scripts runs painfully slow? These few lines takes over 1 minute to execute. ``` for (n=0; n<app.activeDocument.layerSets.length; n++) { app.activeDocument.layerSets[n].visible = false; } ``` The number of layerSets are 20. I'm running the CS5.1 64bit version on a Vista Home Premium system, AMD Athlon 64 X2 Dual Core 5200+ with 8GB RAM. I tried to Export the script as a .JSXBIN but it still takes over 1 minute. CPU usage for CS5.1 goes from 3% to 57% when the CS5.1 is running the .JSXBIN script. There must be something wrong here, how can I speed up the scripts? // Thanks * EDIT * It seems like CS5's own DOM implementation is the problem here. The script speeded up more than twice by reading DOM related values into local variables. ``` var LayerCount = app.activeDocument.layerSets.length; var LayerRoot = app.activeDocument.layerSets; for (n=0; n<LayerCount; n++) { LayerRoot[n].visible = false; } ``` ...but still, it far to much time to just change a property in 20 objects. Any help with optimizing would be appreciated :)

Original source