// ISO_PasteClipboardIntoFootage.jsx
// Artist: https://www.jasonfletcher.info/
// Code generated by ChatGPT-5.5
// Date: June 2, 2026
// Description: This After Effects script will paste the clipboard contents (such as effects or masks) into each of the footage layers within a comp. The footage file extension is determined by the user. This operation will be executed for the selected comps in the Project window. Supports both single footage files and frame sequences.

(function main() {

    app.beginUndoGroup("Paste Clipboard Into Footage Layers");

    var proj = app.project;
    var items = proj.selection;
    var affectedComps = 0;

    // Ask user for extension
    var extInput = prompt("Enter footage file extension to target (e.g. mp4, png, mov)", "mp4");

    if (extInput === null) {
        app.endUndoGroup();
        alert("Script cancelled.");
        return;
    }

    var ext = extInput.replace(".", "").replace(/\s/g, "").toLowerCase();
    if (ext === "") ext = "mp4";

    var extRegex = new RegExp("\\." + ext + "$", "i");

    if (items.length === 0) {
        app.endUndoGroup();
        alert("Please select one or more comps in the Project panel.");
        return;
    }

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

        if (!(items[i] instanceof CompItem))
            continue;

        var comp = items[i];
        var compHadMatches = false;

        comp.openInViewer();

        for (var l = 1; l <= comp.numLayers; l++) {

            var layer = comp.layer(l);
            var isMatch = false;

            try {
                if (
                    layer.source &&
                    layer.source.name &&
                    extRegex.test(layer.source.name)
                ) {
                    isMatch = true;
                }
            } catch (err) {}

            if (isMatch) {

                // Deselect all layers
                for (var d = 1; d <= comp.numLayers; d++) {
                    comp.layer(d).selected = false;
                }

                layer.selected = true;
                app.activeViewer.setActive();

                // Paste clipboard
                app.executeCommand(20);

                layer.selected = false;

                compHadMatches = true;
            }
        }

        if (compHadMatches) {
            affectedComps++;
        }
    }

    app.endUndoGroup();

    alert(
        "Clipboard pasted into " +
        affectedComps +
        " comps (target extension: ." + ext + ")"
    );

})();