Note: Random does not equate to unique, however, the more variables added the greater the chance that it will be unique.
No checks are performed to ensure that a layer is active or that a Background image layer is selected.
This script will add a space/hyphen/space + random 20 digits to the end of the active layer.
var layerName = activeDocument.activeLayer.name;
var randomDigits = (Math.random() * 1e20);
activeDocument.activeLayer.name = layerName + " - " + randomDigits;
This one uses a random mix of 20 lowercase, uppercase and digits:
var layerName = activeDocument.activeLayer.name;
activeDocument.activeLayer.name = layerName + " - " + readableRandomStringMaker(20);
function readableRandomStringMaker(length) {
for (var s = ''; s.length < length; s += 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'.charAt(Math.random() * 62 | 0));
return s;
}
This example creates a mock GUID added at the end of the current layer name:
var layerName = activeDocument.activeLayer.name;
activeDocument.activeLayer.name = layerName + " - " + generateGUID();
function generateGUID() {
// Mock GUID generator
/* http://blog.shkedy.com/2007/01/createing-guids-with-client-side.html */
/* https://en.wikipedia.org/wiki/Universally_unique_identifier */
var result, i, j;
result = '';
for (j = 0; j < 32; j++) {
if (j == 8 || j == 12 || j == 16 || j == 20)
result = result + '-';
i = Math.floor(Math.random() * 16).toString(16).toUpperCase();
result = result + i;
}
return result;
}
Edit: This final one uses date and time (DDMMYYYYTTTTTT):
/* Time stuff */
var myDate = new Date();
myDate.toLocaleString('en-GB');
var myDateString = myDate.toString();
var time = myDateString.replace(/(.+\d{4} )(.+)( .+)/, '$2').replace(/:/g, '');
var legibleTime = time.replace(/(\d{2})(\d{2})(\d{2})/, '$1-$2-$3');
/* Date stuff based on:
https://www.w3resource.com/javascript-exercises/javascript-basic-exercise-3.php
*/
var todaysDateTime = new Date();
var dd = todaysDateTime.getDate();
var mm = todaysDateTime.getMonth() + 1;
var yyyy = todaysDateTime.getFullYear();
if (dd < 10) {
dd = '0' + dd;
}
if (mm < 10) {
mm = '0' + mm;
}
var layerName = activeDocument.activeLayer.name;
activeDocument.activeLayer.name = layerName + " - " + dd + mm + yyyy + time;
//activeDocument.activeLayer.name = layerName + " - " + dd + '-' + mm + '-' + yyyy + '_' + legibleTime;