// ISO_MoveTimeIndicatorTimecode.jsx
// Artist: https://www.jasonfletcher.info/
// Code generated by ChatGPT-5.5
// Date: June 11, 2026
// Description: This After Effects script will open the selected comps in the Project window and move the Current Time Indicator to a user-specified timecode (H:MM:SS:FF).

(function () {

    app.beginUndoGroup("Move CTI to Timecode");

    try {

        var userInput = prompt(
            "Enter the target timecode (H:MM:SS:FF):",
            "0:00:00:00"
        );

        if (userInput === null) {
            alert("Operation cancelled.");
            return;
        }

        userInput = userInput.replace(/^\s+|\s+$/g, "");

        // Accept:
        // 1:02:03:12
        // 01:02:03:12
        var timecodeRegex = /^\d{1,2}:\d{2}:\d{2}:\d{2}$/;

        if (!timecodeRegex.test(userInput)) {
            alert(
                "Invalid timecode format.\n\n" +
                "Please use:\n" +
                "H:MM:SS:FF\n\n" +
                "Examples:\n" +
                "1:02:03:12\n" +
                "01:02:03:12"
            );
            return;
        }

        var parts = userInput.split(":");

        var hours = parseInt(parts[0], 10);
        var minutes = parseInt(parts[1], 10);
        var seconds = parseInt(parts[2], 10);
        var frames = parseInt(parts[3], 10);

        if (minutes > 59 || seconds > 59) {
            alert(
                "Invalid timecode.\n\n" +
                "Minutes and seconds must be between 00 and 59."
            );
            return;
        }

        var selectedItems = app.project.selection;

        if (selectedItems.length === 0) {
            alert("Please select at least one composition in the Project panel.");
            return;
        }

        var processedCount = 0;
        var skippedCount = 0;

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

            var item = selectedItems[i];

            if (item !== null && item instanceof CompItem) {

                var frameRate = item.frameRate;

                if (frames >= frameRate) {
                    alert(
                        "Invalid frame value for composition:\n\n" +
                        item.name + "\n\n" +
                        "Entered frame number: " + frames + "\n" +
                        "Composition frame rate: " + frameRate + " fps\n\n" +
                        "Frame value must be less than the composition frame rate."
                    );
                    return;
                }

                var targetTime =
                    (hours * 3600) +
                    (minutes * 60) +
                    seconds +
                    (frames / frameRate);

                item.openInViewer();
                item.time = targetTime;

                processedCount++;
            } else {
                skippedCount++;
            }
        }

        alert(
            "CTI moved successfully.\n\n" +
            "Timecode: " + userInput + "\n" +
            "Compositions affected: " + processedCount + "\n" +
            "Items skipped: " + skippedCount
        );

    } catch (err) {

        alert(
            "An error occurred:\n\n" +
            err.toString()
        );

    } finally {

        app.endUndoGroup();

    }

}());
