Skip to main content
New Participant
February 8, 2025
Answered

Random name when saving the “Save As” dialog box

  • February 8, 2025
  • 1 reply
  • 526 views

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?

Correct answer Stephen Marsh

@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('');
}

 

 


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;
}

 

1 reply

Stephen Marsh
Community Expert
February 8, 2025

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?

New Participant
February 8, 2025

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.

 

Stephen Marsh
Community Expert
February 8, 2025

@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('');
}