The following script has basic error checking for unsaved files, however it processes all open files equally, regardless of whether they were originally JPEG or PSD or TIFF etc.
#target photoshop
/* Start Saved Document Error Check - Part A: Try */
savedDoc();
function savedDoc() {
try {
app.activeDocument.path;
/* Finish Saved Document Error Check - Part A: Try */
/* Main Code Start */
while (app.documents.length >= 1) {
var jpegOptions = new JPEGSaveOptions();
jpegOptions.quality = 12; // Quality Level
jpegOptions.embedColorProfile = true; // or false
jpegOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpegOptions.matte = MatteType.NONE;
for (var i = 0; i < app.documents.length; i++) {
app.activeDocument = app.documents[i];
var doc = app.activeDocument;
var docPath = doc.path;
var docName = doc.name;
doc.bitsPerChannel = BitsPerChannelType.EIGHT;
doc.flatten();
doc.saveAs(new File(docPath + '/' + docName), jpegOptions);
doc.close(SaveOptions.DONOTSAVECHANGES);
}
}
alert("All open files saved as JPEG files!");
/* Main Code Finish */
}
/* Start Open/Saved Document Error Check - Part B: Catch */
catch (err) {
alert("New images must be saved before running this script!");
}
}
/* Finish Open/Daved Document Error Check - Part B: Catch */
https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html