(function() {
    try {
        app.beginUndoGroup("Change BG");

        function lockLayers(myComp) {
            var totalLayers = myComp.numLayers;
            if (totalLayers >= 1) {

                // If first frame locked change state
               (myComp.layer(1).locked) ? state = false : state = true;

                for (var i = 1; i <= totalLayers; i++) {
                    myComp.layer(i).locked = state;
                }

            } else {
                return;
            }
        }

        var comp = app.project.activeItem;
        if (!comp || !(comp instanceof CompItem)) {
            // Current comp is not selected
            // Change selected composition BG colors
            var mySelectedItems = [];

            for (var i = 1; i <= app.project.numItems; i++) {
                if (app.project.item(i).selected)
                    mySelectedItems[mySelectedItems.length] = app.project.item(i);
            }

            if (mySelectedItems.length > 0) {
                for (var k = 0; k < mySelectedItems.length; k++) {
                    var mySelection = mySelectedItems[k];
                    lockLayers(mySelection);
                }
            }
            return;
        } else { // Current Comp
            lockLayers(app.project.activeItem);
        }

       // app.project.activeItem.bgColor = [0, 0, 0];
    } catch (err) {
        alert(err);
    } finally {
        app.endUndoGroup();
    }
})();