// Isosceles_WorkAreaStartToFootageStart.jsx
// Code generated by ChatGPT-5
// Date: October 9, 2025
// Description: This After Effects script will move the "Work Area Start" to the earliest contained footage start in the comp. This will be applied to the comps that are selected in the Project window.

/*
  Move Work Area Start to Earliest Footage Start
  - Applies to all selected CompItems in the Project panel.
  - Finds the minimum layer.inPoint for layers whose source is a FootageItem.
  - Snaps the resulting Work Area Start to the nearest previous frame boundary.
  - Preserves the existing Work Area End whenever possible.
  - If the new start would be at or beyond the current Work Area End,
    the Work Area Duration is set to one frame.
*/

(function moveWorkAreaStartToEarliestFootage() {
    app.beginUndoGroup("Move Work Area Start to Earliest Footage Start");

    var sel = app.project.selection;
    if (!sel || sel.length === 0) {
        alert("No items selected. Please select one or more compositions in the Project panel.");
        return;
    }

    var updated = [];
    var skipped = [];

    for (var i = 0; i < sel.length; i++) {
        var item = sel[i];

        if (!(item instanceof CompItem)) {
            skipped.push(
                (item && item.name)
                    ? (item.name + " (not a composition)")
                    : "Unnamed item (not a composition)"
            );
            continue;
        }

        var comp = item;
        var minStart = Infinity;

        // Iterate layers (1-based)
        for (var li = 1; li <= comp.numLayers; li++) {
            try {
                var layer = comp.layer(li);
                if (!layer) continue;

                // Only consider layers whose source is a FootageItem
                if (layer.source && (layer.source instanceof FootageItem)) {
                    var startTime = layer.inPoint;

                    if (isFinite(startTime) && startTime < minStart) {
                        minStart = startTime;
                    }
                }
            } catch (e) {
                // Ignore layer errors and continue
            }
        }

        if (minStart === Infinity) {
            skipped.push(comp.name + " (no footage layers found)");
            continue;
        }

        // Snap start to previous frame boundary
        var frameDur = comp.frameDuration;
        var startFrameCount = Math.floor(minStart / frameDur);
        var snappedMinStart = startFrameCount * frameDur;

        // Preserve current work area end
        var currentEnd = comp.workAreaStart + comp.workAreaDuration;

        if (snappedMinStart >= currentEnd) {
            comp.workAreaStart = snappedMinStart;
            comp.workAreaDuration = frameDur;
        } else {
            comp.workAreaStart = snappedMinStart;
            comp.workAreaDuration = currentEnd - snappedMinStart;
        }

        updated.push(
            comp.name +
            " → workAreaStart = " +
            snappedMinStart.toFixed(3) +
            "s"
        );
    }

    var msg = "";
    if (updated.length) {
        msg += "Updated:\n" + updated.join("\n") + "\n\n";
    }
    if (skipped.length) {
        msg += "Skipped:\n" + skipped.join("\n");
    }

    alert(msg);

    app.endUndoGroup();
})();