// ISO_TrimCompToWorkArea.jsx
// Artist: https://www.jasonfletcher.info/
// Code generated by ChatGPT-5.3
// Date: April 26, 2026
// Description: This After Effects script will trim the comp to its work area. This operation will be executed for the selected comps in the Project window.

{
    app.beginUndoGroup("Trim Selected Comps to Work Area");

    var proj = app.project;

    var trimmedCount = 0;

    if (!proj) {
        alert("No project open.");
    } else {
        var selection = proj.selection;

        if (selection.length === 0) {
            alert("Please select one or more compositions in the Project panel.");
        } else {

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

                if (item instanceof CompItem) {

                    var comp = item;

                    var start = comp.workAreaStart;
                    var duration = comp.workAreaDuration;
                    var end = start + duration;

                    // Shift all layers so work area start becomes 0
                    for (var j = 1; j <= comp.numLayers; j++) {
                        var layer = comp.layer(j);
                        layer.startTime -= start;
                    }

                    // Set new comp duration
                    comp.duration = duration;

                    // Reset work area
                    comp.workAreaStart = 0;
                    comp.workAreaDuration = duration;

                    trimmedCount++;
                }
            }
        }
    }

    app.endUndoGroup();

    alert(
        "Comps trimmed: " + trimmedCount
    );
}