// ISO_LabelColorsToSelectedLayers.jsx
// Artist: https://www.jasonfletcher.info/
// Code generated by ChatGPT-5.5 Mini
// Date: July 24, 2026
// Description: This After Effects script will apply unique label colors to each of the selected layers in the active comp.

(function () {
    var scriptName = "Apply Unique Label Colors To Selected Layers";

    app.beginUndoGroup(scriptName);

    try {
        var comp = app.project.activeItem;

        if (comp === null || !(comp instanceof CompItem)) {
            alert("Please open a composition in the Timeline panel before running this script.");
            return;
        }

        var selectedLayers = comp.selectedLayers;

        if (selectedLayers === null || selectedLayers.length === 0) {
            alert("No layers are selected. Please select one or more layers in the active composition.");
            return;
        }

        // After Effects default label color indexes.
        // Label indexes range from 1-16 in current versions of After Effects.
        var defaultLabelColors = [];
        var i;

        for (i = 1; i <= 16; i++) {
            defaultLabelColors.push(i);
        }

        var affectedCount = 0;
        var skippedCount = 0;

        for (i = 0; i < selectedLayers.length; i++) {
            var layer = selectedLayers[i];

            if (layer !== null) {
                var colorIndex = defaultLabelColors[i % defaultLabelColors.length];

                try {
                    layer.label = colorIndex;
                    affectedCount++;
                } catch (err) {
                    skippedCount++;
                }
            } else {
                skippedCount++;
            }
        }

        alert(
            "Label colors applied successfully.\n\n" +
            "Composition: " + comp.name + "\n" +
            "Selected layers processed: " + affectedCount + "\n" +
            "Skipped layers: " + skippedCount + "\n\n" +
            "Colors used: Default After Effects label colors 1-16.\n" +
            "Colors will cycle when more than 16 layers are selected."
        );

    } catch (err) {
        alert(
            "An error occurred while applying label colors.\n\n" +
            err.toString()
        );
    } finally {
        app.endUndoGroup();
    }

})();