Skip to main content
Participating Frequently
May 21, 2024
Answered

Layer name used to save file with Action or Script

  • May 21, 2024
  • 3 replies
  • 1465 views

Hi there, 

I’m new to posting in the community. 
I’m hoping for some advice or assistance in setting up an action or a script to do the following.

I have a PSD template that I have set up an action to drop a PNG file into the bottom layer. 

What I need is an automation of some sort that inserts the name of the Layer that I just dropped into the template to be added to the front of the PSD file name and saved as a jpeg for web. 

I have just got my head around Actions, and Scripts are quite advanced for me.  🙂

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

I forgot to mention, that the other way to do this is to use Layer Comps, saving each comp with the name you require, then use the default Export Layer Comps to Files script.

 

https://helpx.adobe.com/au/photoshop/using/layer-comps.html

 

3 replies

Stephen Marsh
Community Expert
Community Expert
May 23, 2024

@AEL-13 - So how did things go?

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
May 21, 2024

I forgot to mention, that the other way to do this is to use Layer Comps, saving each comp with the name you require, then use the default Export Layer Comps to Files script.

 

https://helpx.adobe.com/au/photoshop/using/layer-comps.html

 

Stephen Marsh
Community Expert
Community Expert
May 21, 2024

You'll need a script for the final save JPEG step, as including a layer name in the final file name is beyond actions or batch commands.

 

This could be a custom script, or an all purpose, generic layer saving script.

 

It might be better to have the whole action re-created as a script, rather than having a hybrid action that relies on a script? That is of course up to you, I'm happy to help, but I would need to see a screenshot of the action steps with each step expanded/disclosed and or have access to the action if it is too complicated to decipher from the screenshot.

 

When you "drop in" (how – File > Place or something else?) the PNG file into the template, what is the name of the layer? Is it the filename of the PNG? Can you post a screenshot of the layers panel?

 

 

AEL-13Author
Participating Frequently
May 21, 2024

Hey Stephen, 
Thanks for replying. 

Here is a Screen Shot. 

The layer called Abstract Art 53 is the layer that I have dropped in. 
I play an action that turns each folder on and off and saves it in jpg for web with the Abstract Art - 53 (or other files I drop in, in it's place) behind the folder layers. 

What I am trying to achieve is to insert the Abstract - 53 layer name in front of the PSD files name when i run the action I mentioned above for saving the jpg. 

DId that make sense?

 

Stephen Marsh
Community Expert
Community Expert
May 21, 2024

Let me know if you need any more to explain it. 
I tried to send the action file on this forum, but it doesn't allow me to attach the file type.


@AEL-13 

 

Try the following script. You will need to record it as an action step, replacing any Save As/Export - Save for Web (Legacy) steps in the action.

 

/*
Export Save for Web Named After Visible Top-Level Layers.jsx
v1.0 - 21st May 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/layer-name-used-to-save-file-with-action-or-script/m-p/14630542
Info: Action helper script used to replace an action save/export step to include a custom filename using the visible root/top-level layer names + doc name
*/

#target photoshop

try {

    // Check that the file has been previously saved
    app.activeDocument.path;

    // Get the current doc
    var doc = app.activeDocument;

    // Get the doc path
    var docPath = doc.path;

    // Get the doc name without the extension
    var docName = doc.name.replace(/\.[^\.]+$/, '');

    // Get all of the root/top-level layers
    var layers = doc.layers;

    // Initialize strings to store the layer names
    var visibleLayerGroups = '';
    var visibleLayers = '';

    // Loop over the layers
    for (var i = 0; i < layers.length; i++) {
        // Get the current layer
        var layer = layers[i];

        // Check if the layer is a group
        if (layer.typename === 'LayerSet') {
            // Check if the group is visible
            if (layer.visible) {
                // Append the group name to the string
                visibleLayerGroups += layer.name;

                /*
                // Loop over the layers in the group
                var groupLayers = layer.layers;
                for (var j = 0; j < groupLayers.length; j++) {
                    var groupLayer = groupLayers[j];
    
                    // Check if the layer in the group is visible
                    if (groupLayer.visible) {
                        // Append the layer name to the string
                        visibleLayers += groupLayer.name + ' - ';
                    }
                }
                */
            }

        } else {
            // Check if the layer is visible
            if (layer.visible) {
                // Append the layer name to the string
                visibleLayers += layer.name;
            }
        }
    }

    // Export the visible layer groups and layers
    if (visibleLayerGroups !== "") {
        var layerNames = visibleLayers + ' - ' + visibleLayerGroups;
        var saveFileJPEG = new File(docPath + '/' + layerNames + ' - ' + docName + '.jpg');
        SaveForWeb(saveFileJPEG);
    
    } else {
        alert('Attention: No layers are visible!');
    }

} catch (e) {
    alert('Error!' + '\r' + e + ', Line: ' + e.line);
}

// JPEG S4W Options
function SaveForWeb(saveFileJPEG) {
    var sfwOptions = new ExportOptionsSaveForWeb();
    sfwOptions.format = SaveDocumentType.JPEG;
    sfwOptions.includeProfile = true;
    sfwOptions.interlaced = 0;
    sfwOptions.optimized = true;
    sfwOptions.quality = 75;
    app.activeDocument.exportDocument(saveFileJPEG, ExportType.SAVEFORWEB, sfwOptions);
}

 

Info on saving and running scripts here:

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html