I've been waiting for your reply, however, I'll post what I created a couple of days ago when you first posted:
/*
Save All Visible Top-level Layers as JPEG.jsx
v1.0 - 14th June 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/i-want-to-export-visible-layers-from-a-psd-file-using-jsx-as-jpeg/td-p/14681365
*/
#target photoshop
if (app.documents.length) {
(function () {
// Select the output folder
var outputFolder = Folder.selectDialog("Select the folder to save the visible layers as JPEG files to:");
if (outputFolder === null) {
alert('Script cancelled!');
return;
}
// Set the doc and layer variables
var doc = app.activeDocument;
var docName = doc.name.replace(/\.[^\.]+$/, ''); // Remove the extension
var theLayers = doc.layers; // or artLayers to exclude layerSets
var theLayerStates = []; // Set an empty array to hold the original visibility states of the layers
// Set the counter
var counter = 0;
// Hide the Photoshop panels
app.togglePalettes();
// Store the original visibility states and hide all layers
for (var i = 0; i < theLayers.length; i++) {
theLayerStates[i] = theLayers[i].visible;
theLayers[i].visible = false;
}
// Loop through all the root/top-level layers
for (var j = 0; j < theLayers.length; j++) {
var theLayer = theLayers[j];
// Check if the layer was originally visible
if (theLayerStates[j]) {
// Make only the current layer visible
//alert(theLayer.name);
theLayer.visible = true;
// Duplicate the document
var tempDoc = doc.duplicate();
// Ensure that the file conforms to JPEG spec
tempDoc.flatten();
tempDoc.bitsPerChannel = BitsPerChannelType.EIGHT;
tempDoc.channels.removeAll();
// Remove unnecessary metadata - //comment out if needed
removeDocumentAncestorsMetadata();
removeCRSmetadata();
removeXMPmetadata();
// Save as JPEG 10 using the doc name and sanitised layer name
var layerName = theLayer.name.replace(/\.[^\.]+$/, '').replace(/[:\/\\*\?\"\<\>\|\\\r\\\n.]/g, "");
var jpegFile = new File(outputFolder + "/" + docName + "_" + layerName + ".jpg");
var jpegOptions = new JPEGSaveOptions();
jpegOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpegOptions.embedColorProfile = true;
jpegOptions.matte = MatteType.NONE;
jpegOptions.quality = 10;
tempDoc.saveAs(jpegFile, jpegOptions, true, Extension.LOWERCASE);
//alert("Layer name: '" + layerName + "' saved!");
// Close the temporary document without saving
tempDoc.close(SaveOptions.DONOTSAVECHANGES);
// Hide the layer again
theLayer.visible = false;
// Increment the counter
counter++;
}
}
// Restore the original visibility states
for (var k = 0; k < theLayers.length; k++) {
theLayers[k].visible = theLayerStates[k];
}
// End of script notification
app.beep();
alert('Script completed!' + '\r' + counter + ' visible top-level layers saved as JPEG files to:' + '\r' + outputFolder);
// Restore the Photoshop panels
app.togglePalettes();
// Functions
function removeXMPmetadata() {
//https://community.adobe.com/t5/photoshop/script-to-remove-all-meta-data-from-the-photo/td-p/10400906
if (!documents.length) return;
if (ExternalObject.AdobeXMPScript === undefined) ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
var xmp = new XMPMeta(activeDocument.xmpMetadata.rawData);
XMPUtils.removeProperties(xmp, "", "", XMPConst.REMOVE_ALL_PROPERTIES);
app.activeDocument.xmpMetadata.rawData = xmp.serialize();
}
function removeCRSmetadata() {
//https://community.adobe.com/t5/photoshop/remove-crs-metadata/td-p/10306935
if (!documents.length) return;
if (ExternalObject.AdobeXMPScript === undefined) ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
var xmp = new XMPMeta(app.activeDocument.xmpMetadata.rawData);
XMPUtils.removeProperties(xmp, XMPConst.NS_CAMERA_RAW, "", XMPConst.REMOVE_ALL_PROPERTIES);
app.activeDocument.xmpMetadata.rawData = xmp.serialize();
}
function removeDocumentAncestorsMetadata() {
//https://community.adobe.com/t5/photoshop-ecosystem-discussions/photoshop-saving-issue-files-too-large/td-p/7279443/page/3
if (!documents.length) return;
whatApp = String(app.name); //String version of the app name
if (whatApp.search("Photoshop") > 0) { //Check for photoshop specifically, or this will cause errors
if (ExternalObject.AdobeXMPScript === undefined) ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
var xmp = new XMPMeta(activeDocument.xmpMetadata.rawData);
// Begone foul Document Ancestors!
xmp.deleteProperty(XMPConst.NS_PHOTOSHOP, "DocumentAncestors");
app.activeDocument.xmpMetadata.rawData = xmp.serialize();
}
}
})();
} else {
alert('A document must be open to run this script!');
}
I presumed that you wanted each separate visible layer as a separate file, however, you might have meant something else.