I finally get the correct script by combining little pieces from here and there
After selecting the top folder, this script will loop through each subfolder, one at a tim.e, to open up two tif files -- which contain cy5 and texas respectively. Then the script will overlay the cy5 file with texas file in a new RBG file and save it with a similar name as the texas file except that the overlay file will replace Texasred with Overlay:
#target photoshop
app.bringToFront();
main(); //call the main function
function main(){
var folders =[];
var topLevel = Folder.selectDialog(""); // select top folder
if(topLevel == null) return; //if cancelled quit
folders = FindAllFolders(topLevel, folders); // call function FindAllFolders
folders.unshift(topLevel); // add new items to the beginning of an array
for(var z=0; z < folders.length; z++){// loop through all subfolders
var fileList = folders.getFiles("*.tif"); //get a list of all tifs in this subfolder
for(var a=0; a < fileList.length; a+=2)
{//loop through all files in folder; 2 at a time
if (fileList.name.match(/cy5/i)){// the file should contain cy5
var docref= app.open(fileList)};//open file
if (fileList[a+1].name.match(/texas/i)){// the file should contain texas
var docref1=app.open(fileList[a+1])};//open file
//** insert "entire overlay script final" recorded by scriptistener
//Marker - Sep 17, 2016, 6:21:49 PM
// start=======================================================
//create one new RGB customized file
var idMk = charIDToTypeID( "Mk " );
var desc136 = new ActionDescriptor();
var idNw = charIDToTypeID( "Nw " );
var desc137 = new ActionDescriptor();
var idMd = charIDToTypeID( "Md " );
var idRGBM = charIDToTypeID( "RGBM" );
desc137.putClass( idMd, idRGBM );
var idWdth = charIDToTypeID( "Wdth" );
var idRlt = charIDToTypeID( "#Rlt" );
desc137.putUnitDouble( idWdth, idRlt, 665.760000 );
var idHght = charIDToTypeID( "Hght" );
var idRlt = charIDToTypeID( "#Rlt" );
desc137.putUnitDouble( idHght, idRlt, 498.720000 );
var idRslt = charIDToTypeID( "Rslt" );
var idRsl = charIDToTypeID( "#Rsl" );
desc137.putUnitDouble( idRslt, idRsl, 150.000000 );
var idpixelScaleFactor = stringIDToTypeID( "pixelScaleFactor" );
desc137.putDouble( idpixelScaleFactor, 1.000000 );
var idFl = charIDToTypeID( "Fl " );
var idFl = charIDToTypeID( "Fl " );
var idBckC = charIDToTypeID( "BckC" );
desc137.putEnumerated( idFl, idFl, idBckC );
var idDpth = charIDToTypeID( "Dpth" );
desc137.putInteger( idDpth, 16 );
var idprofile = stringIDToTypeID( "profile" );
desc137.putString( idprofile, "sRGB IEC61966-2.1" );
var idDcmn = charIDToTypeID( "Dcmn" );
desc136.putObject( idNw, idDcmn, desc137 );
executeAction( idMk, desc136, DialogModes.NO );
//end of new file creation Marker - Sep 17, 2016, 6:22:19 PM
// start=======================================================
// run the Overlay action
var idPly = charIDToTypeID( "Ply " );
var desc138 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref90 = new ActionReference();
var idActn = charIDToTypeID( "Actn" );
ref90.putName( idActn, "Overlay" );
var idASet = charIDToTypeID( "ASet" );
ref90.putName( idASet, "Default Actions" );
desc138.putReference( idnull, ref90 );
executeAction( idPly, desc138, DialogModes.NO );
// end of Overlay action Marker - Sep 17, 2016, 6:24:08 PM
// start=======================================================
// save the overlay file
var idsave = charIDToTypeID( "save" );
var desc139 = new ActionDescriptor();
var idAs = charIDToTypeID( "As " );
var desc140 = new ActionDescriptor();
var idBytO = charIDToTypeID( "BytO" );
var idPltf = charIDToTypeID( "Pltf" );
var idMcnt = charIDToTypeID( "Mcnt" );
desc140.putEnumerated( idBytO, idPltf, idMcnt );
var idTIFF = charIDToTypeID( "TIFF" );
desc139.putObject( idAs, idTIFF, desc140 );
var idIn = charIDToTypeID( "In " );
var Name= fileList[a+1].name.replace("Texasred", "Overlay"); //rename the new RGB Overlay file
desc139.putPath( idIn, new File( folders+"/" + Name ) );
var idLwCs = charIDToTypeID( "LwCs" );
desc139.putBoolean( idLwCs, true );
executeAction( idsave, desc139, DialogModes.NO );
// end of file saving Marker - Sep 17, 2016, 6:26:43 PM
// start =======================================================
// close all files
var idCls = charIDToTypeID( "Cls " );
executeAction( idCls, undefined, DialogModes.NO );
// =======================================================
var idCls = charIDToTypeID( "Cls " );
executeAction( idCls, undefined, DialogModes.NO );
// =======================================================
var idCls = charIDToTypeID( "Cls " );
executeAction( idCls, undefined, DialogModes.NO );
// end of file closing Marker - Sep 17, 2016, 6:27:43 PM
//** end of insert of entire overlay script final
}//end filelist loop
}//end subfolders loop
}//end main function
function FindAllFolders( srcFolderStr, destArray) {
var fileFolderArray = Folder( srcFolderStr ).getFiles();
for ( var i = 0; i < fileFolderArray.length; i++ ) {
var fileFoldObj = fileFolderArray;
if ( fileFoldObj instanceof File ) {
} else {
destArray.push( Folder(fileFoldObj) ); // add new items to the end of an array
FindAllFolders( fileFoldObj.toString(), destArray );
}
}
return destArray;
}