• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Explorer ,
Mar 01, 2023 Mar 01, 2023

Copy link to clipboard

Copied

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

TOPICS
Actions and scripting , macOS

Views

3.5K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 01, 2023 Mar 01, 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 JPEG using 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
*/

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

Votes

Translate

Translate
Adobe
Community Expert ,
Mar 01, 2023 Mar 01, 2023

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 01, 2023 Mar 01, 2023

Copy link to clipboard

Copied

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

 

Screenshot 2023-03-01 at 12.55.04.png

 

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 01, 2023 Mar 01, 2023

Copy link to clipboard

Copied

PSD file attached.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 01, 2023 Mar 01, 2023

Copy link to clipboard

Copied

Make sure your filenames don't get too long (around 256 characters cross-platform) as that can cause problems, especially with long folder path names.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 01, 2023 Mar 01, 2023

Copy link to clipboard

Copied

@Lumigraphics – good point!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 01, 2023 Mar 01, 2023

Copy link to clipboard

Copied

@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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 02, 2023 Mar 02, 2023

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 02, 2023 Mar 02, 2023

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 02, 2023 Mar 02, 2023

Copy link to clipboard

Copied

No worries Stephen, you've already been a massive help, don't mind waiting that bit extra.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 03, 2023 Mar 03, 2023

Copy link to clipboard

Copied

quote

No worries Stephen, you've already been a massive help, don't mind waiting that bit extra.


By @StevenDH

 

You're welcome, I have updated the code to a 1.2 version to only rename using visible layers.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 06, 2023 Mar 06, 2023

Copy link to clipboard

Copied

Thank you Stephen, I've just tested the code and it works perfectly. Thank you for your help on this.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 06, 2023 Mar 06, 2023

Copy link to clipboard

Copied

LATEST

You're welcome, I'm happy to help.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 01, 2023 Mar 01, 2023

Copy link to clipboard

Copied

@StevenDH 

 

Please try the following first draft script:

 

/*
Save JPEG using 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.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 01, 2023 Mar 01, 2023

Copy link to clipboard

Copied

if(File(jpgSave).exists){
    //ask to replace
    }
else{
    //save file code
    }

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 01, 2023 Mar 01, 2023

Copy link to clipboard

Copied

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 JPEG using 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
*/

// 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))
        // throw alert("Script cancelled!");
        throw null;
}

// 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);

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines