Don't have any unsaved files open when running this script.
If you are using PSD, I have simplified the code to save in the current file format, by simply closing/saving the edited file. As the PSD options are no longer set, you might have to set maximise compatibility depending on your preferences. If TIFF you may or may not need to select save options.
Does this 1.1 version work without locking up? It obviously doesn't lock up for me, but I don't use Lr and I'm not sure of the issue with resaving back to a file that is in your Lr library/catalogue.
/*
Layer & Save Matching Retouched JPEG over All Open PSD Files.jsx
v1.1 - 7th May 2023, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/help-loading-images-into-layers-by-filename/td-p/13769691
Notes:
All open files should have been previously saved, unsaved files may interrupt the batch processing
JPEG file names must match the open PSD name, with the addition of "_retouched.jpg" at the end of the doc name
*/
#target photoshop
if (app.documents.length > 0) {
(function () {
var jpgFolder = Folder.selectDialog('Please select the retouched JPEG folder:');
if (jpgFolder === null) return;
var savedDisplayDialogs = app.displayDialogs;
app.displayDialogs = DialogModes.NO;
while (app.documents.length > 0) {
var psdName = activeDocument.name.replace(/\.[^\.]+$/, '');
var jpgName = psdName + "_retouched.jpg";
var jpgFile = jpgFolder + '/' + jpgName;
placeFile(File(jpgFile), 100);
activeDocument.activeLayer.rasterize(RasterizeType.ENTIRELAYER);
activeDocument.close(SaveOptions.SAVECHANGES);
}
app.displayDialogs = savedDisplayDialogs;
app.beep();
})();
}
else {
alert('One or more previously saved docs must be open to use this script!');
}
function placeFile(file, scale) {
try {
var idPlc = charIDToTypeID("Plc ");
var desc2 = new ActionDescriptor();
var idnull = charIDToTypeID("null");
desc2.putPath(idnull, new File(file));
var idFTcs = charIDToTypeID("FTcs");
var idQCSt = charIDToTypeID("QCSt");
var idQcsa = charIDToTypeID("Qcsa");
desc2.putEnumerated(idFTcs, idQCSt, idQcsa);
var idOfst = charIDToTypeID("Ofst");
var desc3 = new ActionDescriptor();
var idHrzn = charIDToTypeID("Hrzn");
var idPxl = charIDToTypeID("#Pxl");
desc3.putUnitDouble(idHrzn, idPxl, 0.000000);
var idVrtc = charIDToTypeID("Vrtc");
var idPxl = charIDToTypeID("#Pxl");
desc3.putUnitDouble(idVrtc, idPxl, 0.000000);
var idOfst = charIDToTypeID("Ofst");
desc2.putObject(idOfst, idOfst, desc3);
var idWdth = charIDToTypeID("Wdth");
var idPrc = charIDToTypeID("#Prc");
desc2.putUnitDouble(idWdth, idPrc, scale);
var idHght = charIDToTypeID("Hght");
var idPrc = charIDToTypeID("#Prc");
desc2.putUnitDouble(idHght, idPrc, scale);
var idAntA = charIDToTypeID("AntA");
desc2.putBoolean(idAntA, true);
executeAction(idPlc, desc2, DialogModes.NO);
} catch (e) { }
}