Skip to main content
Known Participant
March 1, 2023
Answered

Export JPG and combining the visual layer names to create the file name.

  • March 1, 2023
  • 4 replies
  • 4331 views

Hello everyone.

 

I'm just wondering if there's a script out there that will allow me to export a JPG and automatically name that JPG the combination of all the visible layer names. I'm producing multiple product images that utilise a series of PNGs overlaid each other to create a final image. Each variant needs to be named that of the layer images to help me identify which is which.

 

Any help is greatly appreciated; got a few thousand images to export like this so a script to do the above will make it easier to wrap my head around the variant names.

 

Thank you,

Steven

This topic has been closed for replies.
Correct answer Stephen Marsh

This 1.1 version adds a prompt to replace an existing file or to cancel.

 

EDIT – I have updated the code to a v1.2 which only uses the visible layer names:

 

/*
Save single JPEG using all visible layer names.jsx
v1.2, 4th March 2023, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/export-jpg-and-combining-the-visual-layer-names-to-create-the-file-name/td-p/13617072
*/

#target photoshop

(function () {

    // Set the doc and layer variables
    var doc = app.activeDocument;
    var docPath = doc.path;
    var layers = doc.layers;

    // Create an empty array to hold the layer names
    var layerNames = [];

    // Backwards loop over layers and push the layer names to the array variable
    for (var i = layers.length - 1; i >= 0; i--) {
        if (layers[i].visible === true) {
            layerNames.push(layers[i].name);
        }
    }

    // Join the layer names with an underscore separator
    var joinLayerNames = layerNames.join("_");

    // RegEx replace word spaces for underscores
    var fileName = joinLayerNames.replace(/ /g, '_');
    var jpgSave = new File(docPath + "/" + fileName + ".jpg");

    // Check for existing file
    if (jpgSave.exists) {
        // true = 'No' as default active button
        if (!confirm("File exists, overwrite: Yes or No?", true))
            return;
    }

    // Setup the save options
    var jpgOptns = new JPEGSaveOptions();
    jpgOptns.formatOptions = FormatOptions.STANDARDBASELINE;
    jpgOptns.embedColorProfile = true;
    jpgOptns.matte = MatteType.NONE;
    jpgOptns.quality = 10;
    doc.saveAs(jpgSave, jpgOptns, true, Extension.LOWERCASE);

}());

 

 

4 replies

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
March 1, 2023

This 1.1 version adds a prompt to replace an existing file or to cancel.

 

EDIT – I have updated the code to a v1.2 which only uses the visible layer names:

 

/*
Save single JPEG using all visible layer names.jsx
v1.2, 4th March 2023, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/export-jpg-and-combining-the-visual-layer-names-to-create-the-file-name/td-p/13617072
*/

#target photoshop

(function () {

    // Set the doc and layer variables
    var doc = app.activeDocument;
    var docPath = doc.path;
    var layers = doc.layers;

    // Create an empty array to hold the layer names
    var layerNames = [];

    // Backwards loop over layers and push the layer names to the array variable
    for (var i = layers.length - 1; i >= 0; i--) {
        if (layers[i].visible === true) {
            layerNames.push(layers[i].name);
        }
    }

    // Join the layer names with an underscore separator
    var joinLayerNames = layerNames.join("_");

    // RegEx replace word spaces for underscores
    var fileName = joinLayerNames.replace(/ /g, '_');
    var jpgSave = new File(docPath + "/" + fileName + ".jpg");

    // Check for existing file
    if (jpgSave.exists) {
        // true = 'No' as default active button
        if (!confirm("File exists, overwrite: Yes or No?", true))
            return;
    }

    // Setup the save options
    var jpgOptns = new JPEGSaveOptions();
    jpgOptns.formatOptions = FormatOptions.STANDARDBASELINE;
    jpgOptns.embedColorProfile = true;
    jpgOptns.matte = MatteType.NONE;
    jpgOptns.quality = 10;
    doc.saveAs(jpgSave, jpgOptns, true, Extension.LOWERCASE);

}());

 

 

Stephen Marsh
Community Expert
Community Expert
March 1, 2023

@StevenDH 

 

Please try the following first draft script:

 

/*
Save single JPEG using all visible layer names.jsx
v1.0, 2nd March 2023, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/export-jpg-and-combining-the-visual-layer-names-to-create-the-file-name/td-p/13617072
*/

// Set the doc and layer variables
var doc = app.activeDocument;
var docPath = doc.path;
var layers = doc.layers;

// Create an empty array to hold the layer names
var layerNames = [];

// Backwards loop over layers and push the layer names to the array variable
for (var i = layers.length -1; i >= 0; i--) {
    layerNames.push(layers[i].name);
}

// Join the layer names with an underscore separator
var joinLayerNames = layerNames.join("_");

// RegEx replace word spaces for underscores
var fileName = joinLayerNames.replace(/ /g, '_');

// Setup the save options
var jpgSave = new File(docPath + "/" + fileName + ".jpg");
var jpgOptns = new JPEGSaveOptions();
jpgOptns.formatOptions = FormatOptions.STANDARDBASELINE;
jpgOptns.embedColorProfile = true;
jpgOptns.matte = MatteType.NONE;
jpgOptns.quality = 10;
doc.saveAs(jpgSave, jpgOptns, true, Extension.LOWERCASE);

 

Note:

Existing files will be overwritten without warning, a check and alert to replace the existing file can be added.

 

This script doesn't include any batch-saving code. You can record the script into an Action and then use the Automate > Batch command. Batch saving code could of course be added.

 

Legend
March 1, 2023
if(File(jpgSave).exists){
    //ask to replace
    }
else{
    //save file code
    }
Stephen Marsh
Community Expert
Community Expert
March 1, 2023

@StevenDH 

 

In your sample, all layers are visible.

 

The wording of your original post gave the impression that there may be some layers with their visibility off, which you didn't wish to include... Or did I read too much into that?

StevenDHAuthor
Known Participant
March 2, 2023

Good morning Stephen.

 

Apologies. If its possible to have the code only reference the visible layers, that would be very helpful. For example, to produce the product variants, multiple products will share the same kitchen worktop, so it'll be a case of keeping that layer in the file but hiding it when exporting other worktops.

 

I've just tried your 1.1 code and it works perfectly. Helpful from @Lumigraphics to have the script double check if the file exists; that'll tell me if I've already exported that particular variant.

 

If it's an easy tweak to have the script only look for the visible layers, that will help a lot. But the 1.1 code does exactly what I need it to, thank you.

Stephen Marsh
Community Expert
Community Expert
March 2, 2023

In theory it should be a simple addition of a conditional check for visibility.

 

Edit: something has just come up, so I'll have to come back in a few days as I won't have access to a computer.

Stephen Marsh
Community Expert
Community Expert
March 1, 2023

The script would probably need to be specifically written or adapted to suit your unique requirements.

 

Please post a cropped screenshot of the layers panel to illustrate, and also type out the final required filename that would be created from the layers panel content.

 

A layered PSD file with the layers would also be helpful to those who may volunteer their time and energy to your request, as otherwise, they would have to build a test file in addition to the coding.

StevenDHAuthor
Known Participant
March 1, 2023

Hello Stephen. No problem at all, I'll attach the file in question along with a screenshot added to this message.

 

 

With regards to the screenshot, the resulting file name would need to be background_Hacienda_Nordic_Blue_Snowy_Ibiza_642_lighting.jpg (ideally having the layer names read from bottom to top. The thing to keep in mind is that it may not always be just 5 layers, there are 6-7 on some other products so needs to look for all visible layers.

 

I'll definitely owe someone a coffee or 10 if they're able to write something up.

 

Thanks again.

StevenDHAuthor
Known Participant
March 1, 2023