Skip to main content
Participant
March 15, 2024
Open for Voting

Save pictures with a unique (random) name

  • March 15, 2024
  • 6 replies
  • 635 views

When saving a picture, it shoud be possibele to save it with a unique (random) name. Then when making a save action, it won't overwrite the original file.

6 replies

Stephen Marsh
Community Expert
Community Expert
August 2, 2024
quote

Thank you for the script. I see I can specify the folder I want, but I can't see the way to save the image as a jpeg without dialogs. Any suggestion?


By @felixpq 

 

That is correct, the script was designed to offer an interactive save, defaulting to PSD.

 

The following code will save a JPEG to the open document path or ask for a save location if the source file is unsaved. Existing files with the same name will be overwritten, as pseudo-random doesn't guarantee a unique name.

 

#target photoshop

try {
    var outputPath = app.activeDocument.path.fsName;
} catch (e) {
    var outputPath = Folder.selectDialog("Unsaved file, select the output folder:");
}
var pseudoRandomDigits = (Math.random() * 1e10);
pseudoRandomDigits = Math.round(pseudoRandomDigits);
var docName = app.activeDocument.name.replace(/\.[^\.]+$/, '');
var saveOptions = new JPEGSaveOptions();
saveOptions.embedColorProfile = true;
saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
saveOptions.matte = MatteType.NONE;
saveOptions.quality = 10;
app.activeDocument.saveAs(File(outputPath + "/" + docName + "_" + pseudoRandomDigits + ".jpg"), saveOptions, true);
Participant
August 2, 2024

Thank you for the script. I see I can specify the folder I want, but I can't see the way to save the image as a jpeg without dialogs. Any suggestion?

Stephen Marsh
Community Expert
Community Expert
March 16, 2024
quote

this is a not2 my waist of my time aim starting in my earning Please help
me


By @no bde 

 

This doesn't make sense, please post in your native language and we'll use translation software to try to reply.

Stephen Marsh
Community Expert
Community Expert
March 16, 2024

The following code will bring up the Save a Copy dialog with the current filename with an added underscore and pseudo-random digits appended, i.e.:

 

Untitled-1_1963700736.psd

 

#target photoshop

try {
    var outputPath = app.activeDocument.path.fsName;
} catch (e) {
    var outputPath = Folder.selectDialog("Unsaved file, select the output folder:");
}
var pseudoRandomDigits = (Math.random() * 1e10);
pseudoRandomDigits = Math.round(pseudoRandomDigits);
var docName = app.activeDocument.name.replace(/\.[^\.]+$/, '');
var idsave = stringIDToTypeID("save");
var desc617 = new ActionDescriptor();
var idas = stringIDToTypeID("as");
var desc618 = new ActionDescriptor();
var idmaximizeCompatibility = stringIDToTypeID("maximizeCompatibility");
desc618.putBoolean(idmaximizeCompatibility, true);
var idphotoshopthreefiveFormat = stringIDToTypeID("photoshop35Format");
desc617.putObject(idas, idphotoshopthreefiveFormat, desc618);
var idin = stringIDToTypeID("in");
desc617.putPath(idin, new File(outputPath + "/" + docName + "_" + pseudoRandomDigits));
var idcopy = stringIDToTypeID("copy");
desc617.putBoolean(idcopy, true);
var idlowerCase = stringIDToTypeID("lowerCase");
desc617.putBoolean(idlowerCase, true);
executeAction(idsave, desc617, DialogModes.ALL);

 

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

Stephen Marsh
Community Expert
Community Expert
March 15, 2024

A random name could be scripted if you need this now.

 

I believe that Photoshop really needs the ability to add "variable tokens" into the filename. The tokens are not literal characters, they are metacharacters that the save naming routine would "translate" into literal characters. The token could also offer parameters, such as the number of digits required.

 

As an example, the filename entry could be:

 

My great digital painting_\\random-digits,6\\

 

And when saved as say a JPEG the result would be:

 

My great digital painting_542876.jpg

 

In this example, I have used illegal filename characters \\ as "reserved" special characters to designate the token. There could be a checkbox to enable tokens, or a preference setting. It would be helpful that there was a option in the GUI to select and insert the token rather than relying on the user to type it correctly.

 

I have added this a separate feature request:

https://community.adobe.com/t5/photoshop-ecosystem-ideas/add-variable-save-filename-tokens-to-save-export-windows/idi-p/14492831#M21143

 

Participant
March 16, 2024
this is a not2 my waist of my time aim starting in my earning Please help
me
D Fosse
Community Expert
Community Expert
March 15, 2024

That's what "Save As" is for.

 

Random names isn't going to get much support here.