2
votes

I have a little JS script I wrote to rename layer names so that they're all unique. It just recursively loops through all the layers in a Photoshop document you have open and will make a list of all the names. If it finds that a name is already in that list, it will append a number to the layer name until it finds a unique name.

I have that working, however one odd behaviour is that in the course of this script running it turns every layer visible. No part of my script is intended to affect visibility. And for testing, I added lines to turn off visibility on every layer after they were checked/renamed, but that doesn't change anything. Every layer is still visible at the end.

For reference, I'm using Photoshop CS2, and the scripts are run through File > Scripts > Rename Duplicate Layers

Here's the two lines I used for disabling visibility (that didn't work):

layerSet.artLayers[i].visibility = false;

and

layerSet.layerSets[i].visibility = false;

And here's the full body of the regular script, that doesn't attempt to change visibility at all.

function main()
{
    if (app.documents.length <= 0)
    {
        return;
    }

    var doc = app.activeDocument;
    layerNames = [];
    renameLayerNames(doc, layerNames);
    alert("Script complete.");
}

// Recursively iterate over layers to rename them
function renameLayerNames(layerSet, layerNames)
{
    var name = '';
    for (var i = 0; i < layerSet.artLayers.length; i++)
    {
        name = layerSet.artLayers[i].name;
        name = uniqueName(name, layerNames);
        layerSet.artLayers[i].name = name;
    }
    for (var i = 0; i < layerSet.layerSets.length; i++)
    {
        name = layerSet.layerSets[i].name;
        name = uniqueName(name, layerNames);
        layerSet.layerSets[i].name = name;

        // Recurse
        renameLayerNames(layerSet.layerSets[i], layerNames);
    }
}

// Ensure name is unique, or add an incrementing number to it.
function uniqueName(name, layerNames)
{
    dupe = 0;
    original = name;
    while (contains(layerNames, name)){
        dupe++;
        name = original + ' ' + dupe.toString();
    }
    layerNames.push(name);
    return name;
}

// Check if array contains object
function contains(array, object)
{
    for (var i = 0; i < array.length; i++){
        if (array[i] == object){
            return true
        }
    }
    return false
}
1

1 Answers

1
votes

I believe the property is visible not visibility i.e.

layerSet.artLayers[i].visible = false;
layerSet.layerSets[i].visible = false;