// ISO_StaggerSelectedLayersInPairs.jsx
// Artist: https://www.jasonfletcher.info/
// Code generated by ChatGPT-5.5 Mini
// Date: July 24, 2026
// Description: This After Effects script will stagger the selected layers using a user-defined frame interval. The layers will be staggered in paired groups of two.

(function () {
    app.beginUndoGroup("Stagger Selected Layers In Pairs");

    try {

        if (!(app.project.activeItem instanceof CompItem)) {
            alert("Please open a composition and select layers in the Timeline panel.");
            return;
        }

        var comp = app.project.activeItem;

        if (comp.selectedLayers.length === 0) {
            alert("No layers are selected. Please select the layers you want to stagger.");
            return;
        }

        var intervalFrames = prompt(
            "Enter stagger interval (frames):",
            "10"
        );

        if (intervalFrames === null) {
            return;
        }

        intervalFrames = parseInt(intervalFrames, 10);

        if (isNaN(intervalFrames) || intervalFrames < 0) {
            alert("Invalid stagger interval. Please enter a positive number of frames.");
            return;
        }

        var selectedLayers = comp.selectedLayers;
        var frameDuration = comp.frameDuration;

        var affectedCount = 0;
        var skippedCount = 0;

        var groupIndex = 0;

        for (var i = 0; i < selectedLayers.length; i += 2) {

            var offsetFrames = groupIndex * intervalFrames;
            var offsetTime = offsetFrames * frameDuration;

            var layer1 = selectedLayers[i];
            var layer2 = null;

            if (layer1 !== null) {

                layer1.startTime += offsetTime;
                affectedCount++;
            } else {
                skippedCount++;
            }

            if (i + 1 < selectedLayers.length) {

                layer2 = selectedLayers[i + 1];

                if (layer2 !== null) {

                    layer2.startTime += offsetTime;
                    affectedCount++;

                } else {
                    skippedCount++;
                }
            }

            groupIndex++;
        }

        alert(
            "Stagger complete.\n\n" +
            "Layers processed: " + affectedCount + "\n" +
            "Skipped layers: " + skippedCount + "\n" +
            "Group size: 2 layers\n" +
            "Stagger interval: " + intervalFrames + " frames"
        );

    } catch (err) {

        alert(
            "An error occurred while staggering layers:\n\n" +
            err.toString()
        );

    } finally {

        app.endUndoGroup();

    }

})();