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