Skip to main content
Participating Frequently
March 28, 2024
Answered

I want to save as jpg with the same name as the selected layer.

  • March 28, 2024
  • 1 reply
  • 1215 views

Hi all,

 

I want to save as jpg with the same name as the selected layer.

The process is like this. I need to manually select a layer then run a script/action/whatever to save the image file with jpeg quality 12 in the same folder as the PSD file. The name of the file should be the same as the layer name I have manually selected.

 

Thanks in advance!

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

This is working like a charm Stephen! Thank you so much! Where could I add the name of the main file as a prefix? So if the main file with all the layers is called "MainPants.psd" and I run the script then I should get MainPants37.jpg (so without the extension PSD). Thanks again!


I have updated the original code to a new 1.1 version.

 

As per your example, there is no space between the docName and layerName:

 

var jpgSave = new File(docPath + "/" + docName + "" + layerName + ".jpg");

 

I have left a placeholder there to add a space or another separator character if needed, in the example below an _ underscore character:

 

var jpgSave = new File(docPath + "/" + docName + "_" + layerName + ".jpg");

 

1 reply

Stephen Marsh
Community Expert
Community Expert
March 28, 2024

@Dusan331314882hci 

 

I presume that you need to toggle off the visibility of all other layers, so that only the active layer content is saved as a JPEG? Then set the layer visibility back to how it was beforehand...

Participating Frequently
March 28, 2024

I need to turn on the visibility of that layer, save the file with the name of that selected layer.

Then turn manually some layers off and some other layers on. Then select again a layer to get the file name for the save and so on.

Participating Frequently
March 28, 2024
quote

I need to turn on the visibility of that layer, save the file with the name of that selected layer.

Then turn manually some layers off and some other layers on. Then select again a layer to get the file name for the save and so on.


By @Dusan331314882hci

 

Try this script:

 

/*
Save JPEG Using Active Layer Name.jsx
v1.1, 29th March 2024, Stephen Marsh
Based on:
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 path variables
    var doc = app.activeDocument;
    var docName = doc.name.replace(/\.[^\.]+$/, '');
    try {
        var docPath = doc.path.fsName;
    } catch (e) {
        var docPath = Folder.selectDialog("Unsaved base file, select the output folder:");
    }

    // Clean the layer name, place characters to be retained inside the [] brackets...
    var layerName = doc.activeLayer.name.replace(/[^a-z0-9 -_]/gi, '').replace(/^ +| +$/g, '');
    var jpgSave = new File(docPath + "/" + docName + "" + layerName + ".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 = 12;
    doc.saveAs(jpgSave, jpgOptns, true, Extension.LOWERCASE);
}());

 

Note:

 

Layers can have special characters that are illegal when used as a filename.

 

In the code you will find the following section, simply add any special characters that you wish to retain inside the [ ] square brackets:

 

[^a-z0-9 -_]

 

The code currently retains alpha-numeric characters, digits, word spaces, hyphens and underscore characters.

 

If you wished to add a $ sign, then you would do so similar to this:

 

[^a-z0-9 -_$]

 

Keep in mind that Photoshop may still remove illegal characters anyway.

 


This is working like a charm Stephen! Thank you so much! Where could I add the name of the main file as a prefix? So if the main file with all the layers is called "MainPants.psd" and I run the script then I should get MainPants37.jpg (so without the extension PSD). Thanks again!