Could you give this a try?
It assumes the files are in order.
// merge images in folder;
// 2014, use it at your own risk;
#target photoshop
// dialog for folder-selection;
var theFolder = Folder.selectDialog ("select folder");
if (theFolder) {
var theFiles = theFolder.getFiles(getSomeFiles);
// create the psd-options;
psdOpts = new PhotoshopSaveOptions();
psdOpts.embedColorProfile = true;
psdOpts.alphaChannels = false;
psdOpts.layers = true;
psdOpts.spotColors = true;
// the number of files to combine;
var theNumber = 3;
// work through files;
for (var m = 0; m < theFiles.length / theNumber; m++) {
var thisNumber = m * theNumber;
var theFile = app.open(File(theFiles[thisNumber]));
// place;
for (var n = 1; n < theNumber; n++) {
thisNumber++;
if (File(theFiles[thisNumber]).exists == true) {placeFile (theFiles[thisNumber])}
};
// save the combined files;
theFile.saveAs(new File (theFolder +"/"+ theFile.name.replace(/\.\D{3,4}$/i, "_comb.psd")), psdOpts);
theFile.close(SaveOptions.DONOTSAVECHANGES)
}
};
////////////////////////////////////
////// get psds, tifs and jpgs from files //////
function getSomeFiles (theFile) {
if (theFile.name.match(/\.(jpg|tif|psd|pdf|)$/i)) {
return true
};
};
////// place file //////
function placeFile (thePath) {
// =======================================================
var idPlc = charIDToTypeID( "Plc " );
var desc14 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
desc14.putPath( idnull, new File( thePath ) );
var idFTcs = charIDToTypeID( "FTcs" );
var idQCSt = charIDToTypeID( "QCSt" );
var idQcsa = charIDToTypeID( "Qcsa" );
desc14.putEnumerated( idFTcs, idQCSt, idQcsa );
var idOfst = charIDToTypeID( "Ofst" );
var desc15 = new ActionDescriptor();
var idHrzn = charIDToTypeID( "Hrzn" );
var idPxl = charIDToTypeID( "#Pxl" );
desc15.putUnitDouble( idHrzn, idPxl, 0.000000 );
var idVrtc = charIDToTypeID( "Vrtc" );
var idPxl = charIDToTypeID( "#Pxl" );
desc15.putUnitDouble( idVrtc, idPxl, 0.000000 );
var idOfst = charIDToTypeID( "Ofst" );
desc14.putObject( idOfst, idOfst, desc15 );
var idWdth = charIDToTypeID( "Wdth" );
var idPrc = charIDToTypeID( "#Prc" );
desc14.putUnitDouble( idWdth, idPrc, 100 );
var idLnkd = charIDToTypeID( "Lnkd" );
desc14.putBoolean( idLnkd, true );
executeAction( idPlc, desc14, DialogModes.NO );
};