var currentlyOpenComp = app.project.activeItem;
if(currentlyOpenComp instanceof CompItem){ //1. This verifies that the current active item is a comp
var allMyLayers = currentlyOpenComp.selectedLayers; //2. Grabs selected layers
var allMyLayersLength = allMyLayers.length;
var timeDurationValue = 0; //Generic variable to collect time to
var checkTheseLayerDurations, checkTheseLayerDurationsLength; //Variables for later
for(var i=0; i<allMyLayersLength; i++){ //Loop through selected layers
if(allMyLayers instanceof AVLayer && allMyLayers.source instanceof CompItem){ //Verify that layer is AVLayer and then check it's source to make sure it's a CompItem
checkTheseLayerDurations = allMyLayers.source.layers; //Grabs the layers inside the comp
checkTheseLayerDurationsLength = checkTheseLayerDurations.length;
for(var d=1; d<=checkTheseLayerDurationsLength; d++){ //Loop through layers to grab time durations
timeDurationValue += checkTheseLayerDurations.source.duration; //Assumes each layer is an AVLayer that has a source, and grabs it's duration and adds it to the previous time total
}
allMyLayers.source.duration = timeDurationValue; //Sets the comp duration to the final duration total.
}
}
}