Skip to main content
bobd7221748
Inspiring
June 22, 2022
해결됨

Rename Photoshop layer to a unique name

  • June 22, 2022
  • 1 답변
  • 1627 조회

Hello,

 

I'm looking for a script that can rename a selected layer to a unique name. Anything that can be used for the 'unique' part is suitable - for example something like, yyyy-mm-dd-HH:mm (don't know whether time is doable or not), or if layers have a unique ID which could be appended to the layer name?

 

Reason being, I want to save said layer through an action into a folder, which is then picked up by an online automation service (make.com). Basically, I don't want overwrites into the folder from multiple computers saving into it at a time.

 

I appreciate I could use something like Image Processer / Pro, but that applies the save to all files open, which isn't ideal.

 

Hope this makes sense and someone can help! Thanks in advance!

이 주제는 답변이 닫혔습니다.
최고의 답변: Stephen Marsh

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;

 

1 답변

Stephen Marsh
Community Expert
Community Expert
June 22, 2022

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;

 

bobd7221748
bobd7221748작성자
Inspiring
June 22, 2022

You Sir, are a star!

This is perfect, thank you so much and thank you for taking the time to share various options - and so quickly!

I think I'll use the time variation, as it almost certainly won't repeat with factoring down to the second, and the added utility of the time made against the file could be useful. Thanks again!!

Stephen Marsh
Community Expert
Community Expert
June 22, 2022

Your welcome. Timing was lucky, the quick reply was due to having existing code for all of these options that just needed a few tweaks to work with layer naming.

 

If you don't like one long string of digits, I commented out the option to break up the long date/time string into human readable chunks separated by hyphens and underscore.

 

How will your action save the active layer name as the filename?