// ISO_RandomizeSeeds.jsx
// Artist: https://www.jasonfletcher.info/
// Code generated by ChatGPT-4o
// Date: April 16, 2025
// Description: This After Effects script will look at the selected comps in the Project window and then randomize the seed value for all wiggle expressions (in all layers).

(function () {
    app.beginUndoGroup("Randomize Wiggle Seeds");

    var selectedItems = app.project.selection;

    var modifiedCount = 0;
    var compsAffected = 0;

    function isComp(item) {
        return item instanceof CompItem;
    }

    function randomInt(min, max) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }

    function updateSeedInExpression(expr, newSeed) {
        return expr.replace(/seedRandom\(\s*\d+\s*,\s*(true|false)\s*\)/, "seedRandom(" + newSeed + ", $1)");
    }

    function processProperty(prop) {
        if (prop.canSetExpression && prop.expressionEnabled && prop.expression) {
            if (/seedRandom\(/.test(prop.expression) && /wiggle\(/.test(prop.expression)) {
                var newSeed = randomInt(1, 10000);
                var newExpr = updateSeedInExpression(prop.expression, newSeed);

                if (newExpr !== prop.expression) {
                    prop.expression = newExpr;
                    modifiedCount++;
                }
            }
        }
    }

    function processProperties(group) {
        for (var i = 1; i <= group.numProperties; i++) {
            var prop = group.property(i);

            if (prop.numProperties > 0) {
                processProperties(prop);
            } else {
                processProperty(prop);
            }
        }
    }

    function processComp(comp) {
        var before = modifiedCount;

        for (var j = 1; j <= comp.numLayers; j++) {
            var layer = comp.layer(j);
            processProperties(layer);
        }

        if (modifiedCount > before) {
            compsAffected++;
        }
    }

    for (var i = 0; i < selectedItems.length; i++) {
        var comp = selectedItems[i];
        if (isComp(comp)) {
            processComp(comp);
        }
    }

    app.endUndoGroup();

    alert(
        "Expressions updated: " + modifiedCount + "\n" +
        "Comps affected: " + compsAffected
    );
})();