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

Generate Image Assets - Append document name as a prefix?

New Here ,
Oct 28, 2019 Oct 28, 2019

Copy link to clipboard

Copied

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, @2X, 400x400 suffixes depending on what parameters are needed. 

PSD-examples.png

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!

TOPICS
Actions and scripting

Views

1.8K

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
Adobe
Community Expert ,
Oct 28, 2019 Oct 28, 2019

Copy link to clipboard

Copied

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 ,
Oct 28, 2019 Oct 28, 2019

Copy link to clipboard

Copied

I believe that a script could be written to prepend the filename before all of the layer-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
New Here ,
Oct 28, 2019 Oct 28, 2019

Copy link to clipboard

Copied

Do I need to post this into the scripting forum to see if someone can help do 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
Community Expert ,
Oct 28, 2019 Oct 28, 2019

Copy link to clipboard

Copied

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!

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 ,
Oct 28, 2019 Oct 28, 2019

Copy link to clipboard

Copied

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.

Renaming multiple files can be an extremely tedious task. Bridge can dramatically simplify and automate this process. Details below... Patreon: https://www.patreon.com/MichaelBullo Renaming methods include appending text, number sequences, incorporating Metadata and complex string substitutions ...

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
New Here ,
Oct 28, 2019 Oct 28, 2019

Copy link to clipboard

Copied

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. 

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 ,
Oct 28, 2019 Oct 28, 2019

Copy link to clipboard

Copied

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;

 

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 ,
Oct 28, 2019 Oct 28, 2019

Copy link to clipboard

Copied

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;
}

 

 

 

 

 

 

 

 

 

 

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
New Here ,
Oct 28, 2019 Oct 28, 2019

Copy link to clipboard

Copied

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

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 ,
Oct 28, 2019 Oct 28, 2019

Copy link to clipboard

Copied

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

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 ,
Oct 29, 2019 Oct 29, 2019

Copy link to clipboard

Copied

Here is a script to "undo" or remove the filename prefix and optional extension suffix from the layer names.

 

 

#target photoshop

// Remove filename prefix and optional extension from layers
// Note: All hidden layers will unintentionally be set to visible, sorry!

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

    // 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 reNamer = app.activeDocument.activeLayer.name.replace(/^.+_|\.[^\.]+$/g, '');
    app.activeDocument.activeLayer.name = reNamer;
}

 

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
New Here ,
Oct 29, 2019 Oct 29, 2019

Copy link to clipboard

Copied

LATEST

Hi Stephen,

 

Thank you for the effort, I do appreciate it. I tried it out and it works as a simple doc name + layer name script, but I think the solution to this issue would need an edit to the plugin directly so that it would append the prefix only upon export. In typical use cases for Generate>Image Assets there are additional parameters (sizing/folders, etc) in the layer name that would interfere with the layer naming that this current script does. 

 

I took some screenshots to illustrate the issue:

Example output after running the script on a PSD with no folders but has Generate/Image Asset parameters below. It functions as a simple renamer but the plugin grabs the layer name at the end so it does not address the issue directly.psd-no-folders.png

 

Example output after running the script on a PSD with folders but has Generate/Image Asset parameters below. Unfortunately it just renames the folders and skips the layers in the folders. 

psd-with-folders.png

 

Hope that helps illustrate the complexity, and I do appreciate the script for other uses!

 

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 ,
Oct 28, 2019 Oct 28, 2019

Copy link to clipboard

Copied

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-...

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