// ISO_PrecompAllLayers.jsx
// Artist: https://www.jasonfletcher.info/
// Code generated by ChatGPT-5.3
// Date: March 12, 2026
// Description: This After Effects script will precomp all layers within the selected comp and then put them together into a single precomp. It will then move the newly created precomp into a folder named "Precomped via script". This operation will be executed for the selected comps in the Project window. Since all layers are precomped into a single comp, only the "Pre-compose: move all attributes into the new composition" technique can be utilized in this context and the "Leave all attributes in <comp name>" technique is not possible.

{
    app.beginUndoGroup("Precompose Selected Comps");

    try {

        var project = app.project;
        var selection = project.selection;

        var processedCount = 0;
        var skippedNoLayers = 0;

        // -------------------------------------------------
        // Find or create destination folder
        // -------------------------------------------------
        var folderName = "Precomped via script";
        var precompFolder = null;

        for (var f = 1; f <= project.numItems; f++) {
            var item = project.item(f);
            if (item instanceof FolderItem && item.name === folderName) {
                precompFolder = item;
                break;
            }
        }

        if (precompFolder === null) {
            precompFolder = project.items.addFolder(folderName);
        }

        // -------------------------------------------------
        // Validate 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 comp = selection[i];

                if (!(comp instanceof CompItem)) {
                    continue;
                }

                if (comp.numLayers === 0) {
                    skippedNoLayers++;
                    continue;
                }

                // Build layer index array
                var layerIndices = [];
                for (var j = 1; j <= comp.numLayers; j++) {
                    layerIndices.push(j);
                }

                var newName = comp.name + "_PRECOMP";

                // -------------------------------------------------
                // Precompose and CAPTURE returned comp directly
                // -------------------------------------------------
                var createdPrecomp = comp.layers.precompose(
                    layerIndices,
                    newName,
                    true // move all attributes
                );

                // -------------------------------------------------
                // Move new comp safely (no name searching)
                // -------------------------------------------------
                if (createdPrecomp !== null && createdPrecomp instanceof CompItem) {
                    createdPrecomp.parentFolder = precompFolder;
                }

                processedCount++;
            }

            alert(
                "Comps which were precomposed: " + processedCount + "\n" +
                "Comps skipped (contained no layers): " + skippedNoLayers
            );
        }

    } catch (err) {

        alert(
            "Error: " + err.toString() +
            (err.line ? "\nLine: " + err.line : "")
        );

    } finally {

        app.endUndoGroup();
    }
}
