app.displayDialogs = DialogModes.NO;
var allDocs = [];
for(var i=0;i<app.documents.length;i++)
    allDocs.push(app.documents[i]);
 
    var targetDoc = undefined;

    for(var i=allDocs.length-1;i>=0;i--)
    {
        
        app.activeDocument = allDocs[i];
        if(!targetDoc) 
        {
         targetDoc  = allDocs[i].duplicate();
         allDocs[i].close(SaveOptions.DONOTSAVECHANGES);
        continue;
        }
        
        try{
        app.activeDocument.mergeVisibleLayers();
        }catch(e){}
        
        try{
            var idDlt = charIDToTypeID( "Dlt " );
                var desc13 = new ActionDescriptor();
                var idnull = charIDToTypeID( "null" );
                    var ref3 = new ActionReference();
                    var idLyr = charIDToTypeID( "Lyr " );
                    var idOrdn = charIDToTypeID( "Ordn" );
                    var idhidden = stringIDToTypeID( "hidden" );
                    ref3.putEnumerated( idLyr, idOrdn, idhidden );
                desc13.putReference( idnull, ref3 );
            executeAction( idDlt, desc13, DialogModes.NO );
        }catch(e){}
        app.activeDocument.layers[0].duplicate(targetDoc,ElementPlacement.PLACEATBEGINNING);
        allDocs[i].close(SaveOptions.DONOTSAVECHANGES);
    }