I edited the code just a little :
{
app.beginUndoGroup("Pre-Compose to Layer Duration");
var myComp = app.project.activeItem;
var copySelected = myComp.selectedLayers.slice();
if(myComp instanceof CompItem) {
var myLayers = myComp.selectedLayers;
if(myLayers.length > 0){
var newIndex = copySelected[copySelected.length-1].index;
var myNewInPoint = copySelected[0].inPoint;
var newInPoint = myLayers[0].inPoint;
var newOutPoint = myLayers[0].outPoint;
var newCompName = "comp ";
var layerName = myLayers[0].name;
if(layerName.length > 26) {
layerName = layerName.substring(0, 26);
}
newCompName += layerName;
var layerIndices = new Array();
for (var i = 0; i < myLayers.length; i++) {
layerIndices[layerIndices.length] = myLayers[i].index;
if (myLayers[i].inPoint < newInPoint) newInPoint = myLayers[i].inPoint;
if (myLayers[i].outPoint > newOutPoint) newOutPoint = myLayers[i].outPoint;
}
var newComp = myComp.layers.precompose(layerIndices, newCompName, true );
var preCompLayer = myComp.selectedLayers[0];
preCompLayer.outPoint = newOutPoint;
newComp.workAreaStart = newInPoint;
newComp.duration = newOutPoint;
newComp.openInViewer().setActive();
app.executeCommand(app.findMenuCommandId("Trim Comp to Work Area"));
myComp.openInViewer().setActive();
myComp.layer(newIndex).startTime = myNewInPoint;
}else{
alert("select at least one layer to precompose.");
}
}else{
alert("please select a composition.");
}
app.endUndoGroup();
}
but the problem with this code is that you have to select the layers from bottom to top or
you will get an error
maybe someone could enhance it ?