Skip to main content
egork11413722
Participating Frequently
October 14, 2019
Answered

Automate file naming according to Group name in Photoshop

  • October 14, 2019
  • 8 replies
  • 3426 views

Greetings.
I'm looking for a way to automate file naming according to layer group name.

I have a PSD file with several layer groups in it. The task is to export several JPG versions of this PSD file, with each version including a specific layer group content only. And each JPG must be named accordingly to it's layer group.

For example:
The PSD name is 2020.01.01 Artist (US Tour).psd.
It contains three layer groups named:
2020.01.02 Artist (New York)
2020.01.03 Artist (Boston)
2020.01.04 Artist (Philadelphia)

I need every layer group to be exported as JPG, and these JPG files must be automatically named after their layer groups.

Is there a script that can automate this process?
Thanks a lot.

 

{Thread renamed by moderator}

This topic has been closed for replies.
Correct answer Chuck Uebele

Okay, this script will export all the top level groups, into the same folder, and use the group name as the file name. It will leave the visibility on of any layer not in one of the groups.

#target photoshop
var jpgOptions = new JPEGSaveOptions();
jpgOptions.quality = 8;
var doc = activeDocument;
var dPath = doc.path
var gArray = new Array();

for(var i=0;i<doc.layers.length;i++){
    doc.activeLayer = doc.layers[i];
    if(doc.layers[i].typename == 'LayerSet'){
        gArray.push(doc.layers[i]);
        doc.layers[i].visible = false;
        }        
    }

for(var i=0;i<gArray.length;i++){
    gArray[i].visible = true;
    var gName = gArray[i].name;
    $.writeln(gName)
    doc.saveAs (new File(dPath +'/' + gName + '.jpg'), jpgOptions,true);
    gArray[i].visible = false;
    };

for(var i=0;i<gArray.length;i++){
    gArray[i].visible = true;
    };

8 replies

Chuck Uebele
Community Expert
Community Expert
October 14, 2019

I'm not sure where the download folder is on the Mac, but the "`" should still point to the user's folder.

Stephen Marsh
Community Expert
Community Expert
October 14, 2019

I have not tested, however that looks right Chuck as ('~/Desktop/‘) works and both folders are in the users home folder. So it should all come down to permissions.

 

EDIT: Yes it works!

Chuck Uebele
Community Expert
Community Expert
October 14, 2019

If you're on Win, this is the path to get to the download folder. Currently the dPath just gets the path for the current file. The "`" will point to the current users folder.

var dPath = new Folder('~/Downloads/');
Chuck Uebele
Community Expert
Community Expert
October 14, 2019

Are you on Win or Mac?

egork11413722
Participating Frequently
October 14, 2019
I'm on Mac
Chuck Uebele
Community Expert
Community Expert
October 14, 2019

Okay, this script will exclude changing the group Backgroug Group. Just keep that name the same in all your files!

#target photoshop
var jpgOptions = new JPEGSaveOptions();
jpgOptions.quality = 8;
var doc = activeDocument;
var dPath = doc.path
var gArray = new Array();

for(var i=0;i<doc.layers.length;i++){
    doc.activeLayer = doc.layers[i];
    if(doc.layers[i].typename == 'LayerSet' && doc.layers[i].name.toLowerCase() != 'background group'){
        gArray.push(doc.layers[i]);
        doc.layers[i].visible = false;
        }        
    }

for(var i=0;i<gArray.length;i++){
    gArray[i].visible = true;
    var gName = gArray[i].name;
    $.writeln(gName)
    doc.saveAs (new File(dPath +'/' + gName + '.jpg'), jpgOptions,true);
    gArray[i].visible = false;
    };

for(var i=0;i<gArray.length;i++){
    gArray[i].visible = true;
    };

 

egork11413722
Participating Frequently
October 14, 2019
Works perfectly, thank you so much! And one last thing. How can I change the exporting directory to "Downloads" folder? (Or any other path). Was playing with " dPath +'/' " but it didn't work as I'm not familiar with scripting. Thanks again
Chuck Uebele
Community Expert
Chuck UebeleCommunity ExpertCorrect answer
Community Expert
October 14, 2019

Okay, this script will export all the top level groups, into the same folder, and use the group name as the file name. It will leave the visibility on of any layer not in one of the groups.

#target photoshop
var jpgOptions = new JPEGSaveOptions();
jpgOptions.quality = 8;
var doc = activeDocument;
var dPath = doc.path
var gArray = new Array();

for(var i=0;i<doc.layers.length;i++){
    doc.activeLayer = doc.layers[i];
    if(doc.layers[i].typename == 'LayerSet'){
        gArray.push(doc.layers[i]);
        doc.layers[i].visible = false;
        }        
    }

for(var i=0;i<gArray.length;i++){
    gArray[i].visible = true;
    var gName = gArray[i].name;
    $.writeln(gName)
    doc.saveAs (new File(dPath +'/' + gName + '.jpg'), jpgOptions,true);
    gArray[i].visible = false;
    };

for(var i=0;i<gArray.length;i++){
    gArray[i].visible = true;
    };
egork11413722
Participating Frequently
October 14, 2019

That was quick! Your script works and it's amazing, thanks a lot!

Is there a chance to upgrade it a bit? There's a background layer group with poster art and 3 layer groups with text info and logos. How can we automate the export of JPG posters keeping the background layer group the same but applying different layer groups with show info? As a result I need to have 3 different JPGs exported (one per each city), and every of them must have the same background.

I attached a PSD file so you can test it. Thanks a lot again!

Chuck Uebele
Community Expert
Community Expert
October 14, 2019

Do you have a background layer that has to be visible and exported with all the groups?

egork11413722
Participating Frequently
October 14, 2019
Yes, there is a common background layer that has to be exported with every group. For example: the background layer is a blank poster, and every group contains text info and png logos for every date of the tour.
Chuck Uebele
Community Expert
Community Expert
October 14, 2019

For each group, are all the layers that you need exported visible, or do they need to be turned on for exporting with the group?

Also, are all the groups that you want to export on the top level - not in another layerset?

egork11413722
Participating Frequently
October 14, 2019

All the layers within every group are visible. And all the groups are on the top level.

Sahil.Chawla
Adobe Employee
Adobe Employee
October 14, 2019

Hi There,

Welcome to the Adobe Community!

As you're looking to automate file naming according to layer group name, please have a look at a similar discussion here and see if it helps: Export Groups to Files?


You may also have a look at this and let us know how it goes: Exporting Groups (Folders) to Files in Photoshop

 

Regards,
Sahil

 

Kukurykus
Legend
October 14, 2019

You linked us to a thread started in 2009? How that is possible if new forum stores topics no older than from after 2016. Is it because later replies to that thread were posted in in February of 2019? So I mean if not the fact of refreshing topic after 10 years that one would be lost (like others old-current users reported, or just archived, but where if I may know it)?

Chuck Uebele
Community Expert
Community Expert
October 14, 2019
I'm not sure, but not all old threads were archived, but I'd like to know also.