// ISO_MarkersEveryXFrames.jsx
// Artist: https://www.jasonfletcher.info/
// Code generated by ChatGPT-5.5 Mini
// Date: July 24, 2026
// Description: This After Effects script will add markers into the active comp at a user-defined frame interval.

(function () {

    var scriptName = "Add Markers Every X Frames";

    app.beginUndoGroup(scriptName);

    try {

        var comp = app.project.activeItem;

        if (comp === null || !(comp instanceof CompItem)) {
            alert("Please open a composition in the Timeline panel before running this script.");
            return;
        }

        var frameRate = comp.frameRate;

        var dialog = new Window("dialog", "Add Markers Every X Frames");

        dialog.orientation = "column";
        dialog.alignChildren = ["fill", "top"];

        var intervalGroup = dialog.add("group");
        intervalGroup.add("statictext", undefined, "Marker Interval (frames):");
        var intervalInput = intervalGroup.add("edittext", undefined, "10");
        intervalInput.characters = 10;

        var durationGroup = dialog.add("group");
        durationGroup.add("statictext", undefined, "Total Duration (frames):");
        var durationInput = durationGroup.add("edittext", undefined, "100");
        durationInput.characters = 10;

        var buttonGroup = dialog.add("group");
        buttonGroup.alignment = "center";

        var okButton = buttonGroup.add("button", undefined, "Apply");
        var cancelButton = buttonGroup.add("button", undefined, "Cancel");

        cancelButton.onClick = function () {
            dialog.close();
        };

        okButton.onClick = function () {
            dialog.close(1);
        };

        if (dialog.show() !== 1) {
            return;
        }

        var markerIntervalFrames = parseInt(intervalInput.text, 10);
        var totalDurationFrames = parseInt(durationInput.text, 10);

        if (isNaN(markerIntervalFrames) || markerIntervalFrames <= 0) {
            alert("Marker Interval must be a positive number of frames.");
            return;
        }

        if (isNaN(totalDurationFrames) || totalDurationFrames <= 0) {
            alert("Total Duration must be a positive number of frames.");
            return;
        }

        var markerIntervalSeconds = markerIntervalFrames / frameRate;
        var totalDurationSeconds = totalDurationFrames / frameRate;

        var markerCount = 0;

        var currentFrame = 0;

        while (currentFrame <= totalDurationFrames) {

            var markerTime = currentFrame / frameRate;

            // Create a marker with no comment, no text note, and no metadata.
            var markerValue = new MarkerValue("");

            comp.markerProperty.setValueAtTime(markerTime, markerValue);

            markerCount++;

            currentFrame += markerIntervalFrames;
        }

        alert(
            "Markers added successfully.\n\n" +
            "Composition: " + comp.name + "\n" +
            "Marker Interval: " + markerIntervalFrames + " frames\n" +
            "Total Duration: " + totalDurationFrames + " frames\n" +
            "Markers Created: " + markerCount + "\n\n" +
            "Markers contain no text notes."
        );

    } catch (err) {

        alert(
            "An error occurred while adding markers.\n\n" +
            err.toString()
        );

    } finally {

        app.endUndoGroup();

    }

})();