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

Random name when saving the “Save As” dialog box

New Here ,
Feb 07, 2025 Feb 07, 2025

Copy link to clipboard

Copied

Greetings! I’ve searched through all available online resources and couldn’t find a solution to my problem. Due to the nature of my work, I need to process and save numerous images daily. I would like the "Save As" dialog to automatically generate a random file name (any combination of letters, numbers, or symbols).

 

Is there a plugin or script that can solve this issue?

TOPICS
Windows

Views

215
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 1 Correct answer

Community Expert , Feb 08, 2025 Feb 08, 2025

Here is a 1.1 version, which uses an "arguably better" random generator.

 

/*
Save Dialog with Random Name.jsx
Stephen Marsh
v1.1 - 8th February 2025
https://community.adobe.com/t5/photoshop-ecosystem-discussions/random-name-when-saving-the-save-as-dialog-box/td-p/15140230
*/

#target photoshop

try {
    // Use the previously saved path
    var docPath = app.activeDocument.path;
} catch (e) {
    // If unsaved, select the desktop
    var docPath = new Folder("~/Desktop");
}

try {
    var s2t =
...

Votes

Translate
Adobe
Community Expert ,
Feb 08, 2025 Feb 08, 2025

Copy link to clipboard

Copied

Yes, this is possible via scripting. Do you really need the "random" filename presented in the Save As dialog? Do you need the flexibility to change the file format and options? Or do you always save in a specific file format and options where the save can be performed without having a dialog window?

Votes

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 ,
Feb 08, 2025 Feb 08, 2025

Copy link to clipboard

Copied

The "Save As" dialog should always be displayed since different folders and save paths are used.

 

I need the file to be automatically assigned a random name regardless of its format.

 

For example, if I open a file named 123.psd, when selecting "Save As", the name 123 should be replaced with a random one. Moreover, each time I save the file, a new random name should be generated. That is, if I press "Save As" three times (meaning I want to save the file three times), each time a different random name should be suggested.

 

Any file format should be supported, including .psd, .png, .jpg, .webp, and so on.

 

Votes

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 ,
Feb 08, 2025 Feb 08, 2025

Copy link to clipboard

Copied

@Vengr252525 

 

Here is an example using a UUID "pseudo-random" generator. The longer the name, the more chance that it will not clash with an existing file name or previously generated UUID from a prior script run (random doesn’t guarantee uniqueness).

 

/*
Save Dialog with Random Name.jsx
Stephen Marsh
v1.0 - 8th February 2025
https://community.adobe.com/t5/photoshop-ecosystem-discussions/random-name-when-saving-the-save-as-dialog-box/td-p/15140230
*/

#target photoshop

try {
    // Use the previously saved path
    var docPath = app.activeDocument.path;
} catch (e) {
    // If unsaved, select the desktop
    var docPath = new Folder("~/Desktop");
}

try {
    var s2t = function (s) {
        return app.stringIDToTypeID(s);
    };
    var descriptor = new ActionDescriptor();
    var descriptor2 = new ActionDescriptor();
    descriptor2.putBoolean(s2t("maximizeCompatibility"), true);
    descriptor.putObject(s2t("as"), s2t("photoshop35Format"), descriptor2);
    descriptor.putPath(s2t("in"), new File(docPath + "/" + uuid() + ".psd"));
    descriptor.putBoolean(s2t("copy"), false);
    descriptor.putBoolean(s2t("lowerCase"), true);
    executeAction(s2t("save"), descriptor, DialogModes.ALL);
} catch (e) { }

function uuid() {
    /* https://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid */
    var chars = '0123456789abcdef'.split('');

    var uuid = [],
        rnd = Math.random,
        r;
    uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
    uuid[14] = '4'; // version 4

    for (var i = 0; i < 36; i++) {
        if (!uuid[i]) {
            r = 0 | rnd() * 16;

            uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r & 0xf];
        }
    }

    return uuid.join('');
}

 

 

Votes

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 ,
Feb 08, 2025 Feb 08, 2025

Copy link to clipboard

Copied

I created a file called Random Name.jsx with your code and placed it in the following path:
C:\Program Files (x86)\Photoshop CS6\Presets\Scripts.

Then, in Photoshop, I added the script through the Event Manager, selecting "Document Export".

After restarting Photoshop, I opened the image 55.png, but when I clicked File → Save As, the file name remained unchanged—it was not replaced with a random name.

 

Then I tried selecting "Everything" in the Event Manager, and when opening the image 123.png, it prompted me to save it as 123 copy.png.

 

What am I doing wrong?  

P.s. me version : Adobe Photoshop CS6

Votes

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 ,
Feb 08, 2025 Feb 08, 2025

Copy link to clipboard

Copied

Why did you add it to the Script Events Manager? The script would be executed directly after the native save event finished, it wouldn't replace the native save.

 

Please test it without using the SEM.

 

The script was intended to be manually run as needed from File > Scripts, not triggered by an event. Installed scripts can have a custom keyboard shortcut applied to them (not sure about CS6). Or an action can record the script and use an F-key shortcut.

 

The script was created and tested in v2021, I can't test or guarantee it works in CS6 due to using action manager code from a later version.

 

I have updated the previous code with a false rather than true to default to Save As rather than Save a Copy:

 

descriptor.putBoolean(s2t("copy"), false);

 

 

Votes

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 ,
Feb 08, 2025 Feb 08, 2025

Copy link to clipboard

Copied

Here is a 1.1 version, which uses an "arguably better" random generator.

 

/*
Save Dialog with Random Name.jsx
Stephen Marsh
v1.1 - 8th February 2025
https://community.adobe.com/t5/photoshop-ecosystem-discussions/random-name-when-saving-the-save-as-dialog-box/td-p/15140230
*/

#target photoshop

try {
    // Use the previously saved path
    var docPath = app.activeDocument.path;
} catch (e) {
    // If unsaved, select the desktop
    var docPath = new Folder("~/Desktop");
}

try {
    var s2t = function (s) {
        return app.stringIDToTypeID(s);
    };
    var descriptor = new ActionDescriptor();
    var descriptor2 = new ActionDescriptor();
    descriptor2.putBoolean(s2t("maximizeCompatibility"), true);
    descriptor.putObject(s2t("as"), s2t("photoshop35Format"), descriptor2);
    descriptor.putPath(s2t("in"), new File(docPath + "/" + uuid() + ".psd"));
    descriptor.putBoolean(s2t("copy"), false);
    descriptor.putBoolean(s2t("lowerCase"), true);
    executeAction(s2t("save"), descriptor, DialogModes.ALL);
} catch (e) { }


function uuid() {
    // Generate a random UUID based on the current date and time to help ensure uniqueness
    // Digits from the milliseconds, seconds, minutes, hours, day, month, and year are randomly used in that order of preference
    // This ensures a more random, unique name is generated each time the script is run
    // Note: This is not a cryptographically secure UUID generator, but it should be fine for this purpose
    var now = new Date();
    var digits = ("" + now.getMilliseconds() + now.getSeconds() + now.getMinutes() + now.getHours() + now.getDate() + (now.getMonth() + 1) + now.getFullYear()).split("");
    var chars = "0123456789abcdef".split("");
    var uuidTemplate = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";

    function getRandomChar() {
        return chars[Math.floor(Math.random() * chars.length)];
    }

    function getRandomDigitOrChar() {
        if (digits.length > 0) {
            // Use digit or char with some randomness to ensure better mix
            return Math.random() > 0.5 ? digits.shift() : getRandomChar();
        } else {
            return getRandomChar();
        }
    }

    var uuid = "";
    for (var i = 0; i < uuidTemplate.length; i++) {
        var c = uuidTemplate.charAt(i);
        if (c === "x") {
            uuid += getRandomDigitOrChar();
        } else if (c === "y") {
            uuid += (8 + Math.floor(Math.random() * 4)).toString(16);
        } else {
            uuid += c;
        }
    }

    return uuid;
}

 

Votes

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 ,
Feb 08, 2025 Feb 08, 2025

Copy link to clipboard

Copied

Thank you for your replies.

I used this code and it works too. Your code sounds better but it doesn't offer new names every time I click on it

 

My code:

 

#target photoshop

if (app.documents.length > 0) {
var doc = app.activeDocument;

// Function to generate a random name
function generateRandomName(length) {
var chars = “abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789;=-_”
var name = “-”
for (var i = 0; i < length; i++) {
name += chars.charAt(Math.floor(Math.random() * chars.length))
}
return name;
}

// Generating a random name
var randomName = generateRandomName(12);
var saveFile = new File(doc.path + “/” + randomName + “.png”); // Save in PNG format

// Save options
var pngOptions = new PNGSaveOptions();

// Save the file with a new random name
doc.saveAs(saveFile, pngOptions, true, Extension.LOWERCASE);
alert("File saved as: ” + saveFile.name);
} else {
alert(“No open document!”);
}

Translated with www.DeepL.com/Translator (free version)

Votes

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 ,
Feb 08, 2025 Feb 08, 2025

Copy link to clipboard

Copied

LATEST
quote

...but it doesn't offer new names every time I click on it

 

@Vengr252525 

 

It does in modern versions of Photoshop where it was tested in v2021 and v2024.

 

random-uuid.gif

 

Votes

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