Skip to main content
Known Participant
June 22, 2022
Answered

Script to Add Text to Unique Filenames

  • June 22, 2022
  • 4 replies
  • 582 views

Right now, I can only find how to replace parts of a filename, but I need to be able to:

add an extension at the end of the filename

add the image size and folder at the beginning of the filename

 

Example:

filename: ufskjnf9430

filename: hieufh9034

filename: fhisod4892

to 

2400x2400 SQ 2400x2400/ufskjnf9430.png

2400x2400 SQ 2400x2400/hieufh9034.png

2400x2400 SQ 2400x2400/fhisod4892.png

 

Every time the filename will be unique, so there is nothing to grab and replace.

I just need a script to add to the filename, and I can't figure it out!

 

Anyone know how to do this??

Correct answer Stephen Marsh

@Anne24506383rxky wrote:

Every time the filename will be unique, so there is nothing to grab and replace.


 

Find/replace via a regular expression capture group can be used to find the entire layer (variable) name and then add to it into the replacement with static text:

 

var newLayerName = activeDocument.activeLayer.name.replace(/(.+)/, '2400x2400 SQ 2400x2400/$1.png');
alert(newLayerName);

 

 

 

4 replies

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
June 24, 2022

@Anne24506383rxky wrote:

Every time the filename will be unique, so there is nothing to grab and replace.


 

Find/replace via a regular expression capture group can be used to find the entire layer (variable) name and then add to it into the replacement with static text:

 

var newLayerName = activeDocument.activeLayer.name.replace(/(.+)/, '2400x2400 SQ 2400x2400/$1.png');
alert(newLayerName);

 

 

 

Known Participant
June 24, 2022

This worked perfectly!  How would I get it to apply to all images in a group/folder.

 

var origLayerName = activeDocument.activeLayer.name.replace(/\.[^\.]+$/, '');
alert(origLayerName);

var newLayerName = "2400x2400 SQ 2400x2400/" + origLayerName + ".png";
activeDocument.activeLayer.name = newLayerName;
alert(activeDocument.activeLayer.name);

 

The reason for not batch naming, is that we are using artboards to create different crops for all of the images and then generate assets into their respective cropped folders. It's the difference between 1 hour of work and 8 hours of work over and over again. It prevents having to open every file, crop it, save it, and have a lot of storage for these different crops. And in this case, someone who doesn't like the image placement can open the psd and adjust it and generate without much help. They wouldn't need me to redo it over and over until their liking.

Known Participant
June 24, 2022

This is resolved! Thank you for your help!

Known Participant
June 23, 2022

Ok, I didn't explain myself very well =P  So I have a master .psd, which has linked images, lets just say 400 unique filenames inside the document. I will be generating assets from them. 

 

Right now, I can change all of the original filenames with an "X_" at the beginning and end, and then in the masterfile psd with all the filenames, I can use a script to change the "X_".  But since I can't alter the original names, so this won't work. And if I change the original filenames back for delivery, it will break the link. Whatever I change the linked filename to in the masterfile psd will not change the original filename but will keep a link. So the list of filenames above are not separate files, they are linked images in a masterfile psd used to generate assets, and once added, I am trying to figure out how to add to the filename as a layer, not a document.

 

Hope this makes sense! =/

Zesty_wanderlust15A7
Known Participant
June 23, 2022

I indeed don't get it 😛 — but just in case you don't know, there are handy Multi-Rename Tools out there. Of course, this is for your existing or generated files, not a total automation with PS you might be seeking.

For PC, I use Total Commander (Ctrl+M) a lot.

Stephen Marsh
Community Expert
Community Expert
June 22, 2022

@Anne24506383rxky – I believe that your choices are to alter the filename when saving or to duplicate the open doc and rename it when duplicating.

 

var docName = app.activeDocument.name.split('.')[0];
var saveFilePSD = new File(new File('~/Desktop/' + 'Prefix_' + docName + '.psd'));
SavePSD(saveFilePSD);

// Setup PSD options
function SavePSD(saveFilePSD) {
psdSaveOptions = new PhotoshopSaveOptions();
psdSaveOptions.embedColorProfile = true;
psdSaveOptions.alphaChannels = true;
psdSaveOptions.layers = true;
psdSaveOptions.annotations = true;
psdSaveOptions.spotColors = true;
app.activeDocument.saveAs(saveFilePSD, psdSaveOptions, true, Extension.LOWERCASE);
}

 

 

var saveFilePSD = new File(new File('~/Desktop/' + docName + '_Suffix' + '.psd'));

 

Here is an example for duplicating with prefix:

 

var docName = app.activeDocument.name.replace(/\.[^\.]+$/, '');
app.activeDocument.duplicate("Prefix_" + docName, false);

 

Or with suffix:

 

var docName = app.activeDocument.name.replace(/\.[^\.]+$/, '');
app.activeDocument.duplicate(docName + "_Suffix", false);

 

 

Hint: You should also search the web for a list of invalid filename characters, such as slashes / \ ...

 

Known Participant
June 22, 2022

This is for photoshop layer filenames for a linked image (with no extension)