// ISO_TrackMatteMulti.jsx
// Artist: https://www.jasonfletcher.info/
// Code generated by ChatGPT-5.5
// Date: July 24, 2026
// Description: This After Effects script will assign a track matte (alpha mode) using the odd-numbered layers to the even-numbered layers in the active composition.

(function () {
    app.beginUndoGroup("Set Alpha Track Matte Every Other Layer");

    try {
        var comp = app.project.activeItem;

        if (!(comp instanceof CompItem)) {
            alert("Please make an active composition the frontmost Timeline panel and run the script again.");
            return;
        }

        var processedCount = 0;
        var unchangedCount = 0;
        var skippedCount = 0;
        var skippedLayers = [];

        var i;
        for (i = 2; i <= comp.numLayers; i += 2) {
            var layer = comp.layer(i);
            var matteLayer = comp.layer(i - 1);

            if (layer === null || matteLayer === null) {
                skippedCount++;
                skippedLayers.push(i);
                continue;
            }

            // Leave existing track mattes unchanged.
            if (layer.hasTrackMatte) {
                unchangedCount++;
                continue;
            }

            try {
                layer.setTrackMatte(matteLayer, TrackMatteType.ALPHA);
                processedCount++;
            } catch (err) {
                skippedCount++;
                skippedLayers.push(i);
            }
        }

        var message = "";
        message += "Alpha Track Matte assignment complete.\n\n";
        message += "Composition: " + comp.name + "\n";
        message += "Track Matte Type: Alpha Matte\n\n";
        message += "Layers updated: " + processedCount + "\n";
        message += "Layers already using a track matte: " + unchangedCount + "\n";
        message += "Layers skipped: " + skippedCount;

        if (skippedLayers.length > 0) {
            message += "\n\nSkipped layer numbers:\n" + skippedLayers.join(", ");
        }

        alert(message);
    }
    catch (err) {
        alert("An error occurred:\n\n" + err.toString());
    }
    finally {
        app.endUndoGroup();
    }
})();