Skip to main content
New Participant
October 28, 2019
Question

Generate Image Assets - Append document name as a prefix?

  • October 28, 2019
  • 6 replies
  • 3080 views

Hello!

 

I would like to be able to append the document name to the exported layer name when I use Generate>Image Assets in photoshop. 

 

As a simple example:

There are multiple documents (medal.psd, events.psd, marathon.psd .... etc.) with the same layer names, gold.png and silver.png. The layers have the additional @4x, @15739213, 400x400 suffixes depending on what parameters are needed. 

Normally all that gets spit out is: 

Medal

  • gold.png, gold-95.png, gold-original.png
  • silver.png, silver-95.png, silver-original.png

Events

  • gold.png, gold-95.png, gold-original.png
  • silver.png, silver-95.png, silver-original.png

Marathon

  • gold.png, gold-95.png, gold-original.png
  • silver.png, silver-95.png, silver-original.png

.... etc for another 50+ PSD files.

 

However, I need all these files to be uniquely named since they get dumped into 1 directory.
When it generates images I'd like for the document name to prefix the layers when the assets get generated.

  • medal_gold.png, medal_gold-95.png, medal_gold-original.png
  • medal_silver.png, medal_silver-95.png, medal_silver-original.png
  • events_gold.png, events_gold-95.png, events_gold-original.png
  • events_silver.png, events_silver-95.png, events_silver-original.png
  • marathon_gold.png, marathon_gold-95.png, marathon_gold-original.png
  • marathon_silver.png, marathon_silver-95.png, marathon_silver-original.png
  • ...etc

Is there something I can modify in the Generate>Image Assets plugin to get the document name prefixed in there? I am aware of the batching abilities in Bridge but it is still "manual" to go in and append the prefixes when Generate>Image Assets is so automated it's just missing the prefix ability.

 

Thank you!

This topic has been closed for replies.

6 replies

Chuck Uebele
Community Expert
October 29, 2019

Check out this link, there are several scripts that will do that, some with UIs to prepend or append the layers' names.

https://graphicdesign.stackexchange.com/questions/36955/how-do-i-rename-multiple-selected-layers-in-photoshop-cc

Stephen Marsh
Community Expert
October 29, 2019

I'm new to scripting, so I'm not 100% happy with the following code (all hidden layers unintentionally become visible), however, it is the best that I can do at the moment.

 

EDIT: I have revised the code originally posted and added this new version to also include an optional filename extension.

 

 

 

#target photoshop

// Add filename prefix and option extension suffix to layers
// Note: All hidden layers will unintentionally be set to visible, sorry!

if (app.documents.length > 0) {
    var docRef = activeDocument;
    var layerNum = docRef.layers.length;

    var layerNameExt = prompt('Optional: Add ".png" or ".jpg" extension for Generator/Image Assets (or leave blank)', '');

    // Force the selection of a layer to work-around an error if no layer is active
    app.activeDocument.activeLayer = app.activeDocument.layers[1];

    for (var i = 0; i < layerNum; i++) {
        docRef.activeLayer = docRef.layers[i];
        if (!docRef.activeLayer.isBackgroundLayer) {
            try {
                reNameLayer()
            } catch (e) {}
        }
    }
} else {
    alert('There are no open files!')
};

function reNameLayer() {
    var docRef = app.activeDocument
    var docName = app.activeDocument.name.replace(/\.[^\.]+$/, '');
    var layerName = app.activeDocument.activeLayer.name;
    var docLayerName = (docName + '_' + layerName + layerNameExt)
    
    // Use a kludge to work around the cancel prompt null return...
    var kludge = docLayerName.replace(/null+$/i, '');
    docRef.activeLayer.name = kludge;
}

 

 

 

 

 

 

 

 

 

 

New Participant
October 29, 2019

oh wow thank you for trying to script it out! Just to clarify, do I add your script to the existing "generate.jsx" file?

Stephen Marsh
Community Expert
October 29, 2019

Not that I am aware of, but I have not tried (if you do, back it up first in-case it breaks!)...

 

You would run my script first, which would rename all layers. Generator would then automatically do it's stuff!

 

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

Stephen Marsh
Community Expert
October 28, 2019

Here is a single layer renamer, it is a work in progress (I need to add more code to rename all layers):

 

 

#target photoshop
var doc = app.activeDocument
var docName = app.activeDocument.name.replace(/\.[^\.]+$/, '');
var layerName = app.activeDocument.activeLayer.name;
var docLayerName = (docName + '_' + layerName)
doc.activeLayer.name = docLayerName;

 

Michael Bullo
Community Expert
October 28, 2019

Adobe Bridge allows for multiple files to be quickly renamed in numerous ways. Look for Batch Rename within the Tools menu.

 

This YouTube tutorial may help...
https://www.youtube.com/watch?v=N7iIPcoGfjg

 

I appreciate that this option doesn't solve your original question but it is faster than any manual changes and doesn't require scripting.

New Participant
October 28, 2019

Hi!


Yes I am aware of the batch renaming, but it does not really help when the prefix needs to change every 2 files..... the situation I am in has drastically different prefixes, not just numerically increasing prefixes so it would be better if the document name prefixed the image generated name. 

Stephen Marsh
Community Expert
October 28, 2019

I believe that a script could be written to prepend the filename before all of the layer-names.

New Participant
October 28, 2019

Do I need to post this into the scripting forum to see if someone can help do that?

Stephen Marsh
Community Expert
October 28, 2019

There is no longer a dedicated scripting sub-forum, however if you can edit your original post and add a scripting tag that may help.

 

 I’ll be looking into a simple script shortly, stay tuned!