Script to Add Text to Unique Filenames

Explorer ,
Jun 22, 2022 Jun 22, 2022

Copy link to clipboard

Copied

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

TOPICS
Actions and scripting , macOS

Views

100

Likes

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

correct answers 2 Correct answers

Adobe Community Professional , Jun 23, 2022 Jun 23, 2022

@Anne24506383rxky wrote:

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. 

 

So is this what you are after then?

 

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

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

Likes

Translate

Translate
Adobe Community Professional , Jun 23, 2022 Jun 23, 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);

 

 

 

Likes

Translate

Translate
Explorer ,
Jun 22, 2022 Jun 22, 2022

Copy link to clipboard

Copied

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

Likes

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 Professional ,
Jun 22, 2022 Jun 22, 2022

Copy link to clipboard

Copied

@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 / \ ...

 

Likes

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
Explorer ,
Jun 23, 2022 Jun 23, 2022

Copy link to clipboard

Copied

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! =/

Likes

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
Enthusiast ,
Jun 23, 2022 Jun 23, 2022

Copy link to clipboard

Copied

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.

Likes

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 Professional ,
Jun 23, 2022 Jun 23, 2022

Copy link to clipboard

Copied

@Anne24506383rxky 

 

As @Signfeld notes, using a program to batch rename the output files would be more pragmatic. Adobe Bridge has a good Batch Rename tool.

 

https://prepression.blogspot.com/search/label/Batch%20Rename

 

Likes

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 Professional ,
Jun 23, 2022 Jun 23, 2022

Copy link to clipboard

Copied


@Anne24506383rxky wrote:

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. 

 

So is this what you are after then?

 

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

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

activeDocument.activeLayer.name = origLayerName;
alert(activeDocument.activeLayer.name);

 

 

Likes

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 Professional ,
Jun 23, 2022 Jun 23, 2022

Copy link to clipboard

Copied


@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);

 

 

 

Likes

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
Explorer ,
Jun 24, 2022 Jun 24, 2022

Copy link to clipboard

Copied

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.

Likes

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
Explorer ,
Jun 24, 2022 Jun 24, 2022

Copy link to clipboard

Copied

This is resolved! Thank you for your help!

Likes

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 Professional ,
Jun 24, 2022 Jun 24, 2022

Copy link to clipboard

Copied

LATEST

@Anne24506383rxky wrote:

This is resolved! Thank you for your help!



Glad to help. Please mark the answer/s that helped you as the correct answer to close off the topic.

Likes

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