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

Rename Photoshop layer to a unique name

Explorer ,
Jun 22, 2022 Jun 22, 2022

Copy link to clipboard

Copied

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!

TOPICS
Actions and scripting , Windows

Views

685

Translate

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 , Jun 22, 2022 Jun 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

...

Votes

Translate

Translate
Adobe
Community Expert ,
Jun 22, 2022 Jun 22, 2022

Copy link to clipboard

Copied

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;

 

Votes

Translate

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
Explorer ,
Jun 22, 2022 Jun 22, 2022

Copy link to clipboard

Copied

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!!

Votes

Translate

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 ,
Jun 22, 2022 Jun 22, 2022

Copy link to clipboard

Copied

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?

Votes

Translate

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
Explorer ,
Jun 22, 2022 Jun 22, 2022

Copy link to clipboard

Copied

Rightio - I'll consider adding in underscores. I might also add a particular word at the end too.

 

Well, it could probably be done with a script, but my work around would be to make the active layer a smart object and then enter that smart object. The filename will then be the layer name. Also, it nicely crops the canvas down to the content too, which is what I would like to happen anyway. When I save and close the file, I'll then add some history backtracking to get the layer back to how it was pre-clicking the action.

 

Unless you can observe any inefficiencies/ potential problems with this approach?

It wont be the case that i'm processing hundreds of layers at a time, so it doesn't have to be perfectly streamlined - the main thing I want is a simple one click action to do the job.

Votes

Translate

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 ,
Jun 22, 2022 Jun 22, 2022

Copy link to clipboard

Copied

It's already there as a placeholder... Change:

 

activeDocument.activeLayer.name = layerName + " - " + dd + mm + yyyy + time;
//activeDocument.activeLayer.name = layerName + " - " + dd + '-' + mm + '-' + yyyy + '_' + legibleTime;

 

To:

 

//activeDocument.activeLayer.name = layerName + " - " + dd + mm + yyyy + time;
activeDocument.activeLayer.name = layerName + " - " + dd + '-' + mm + '-' + yyyy + '_' + legibleTime;

Votes

Translate

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
Explorer ,
Jun 22, 2022 Jun 22, 2022

Copy link to clipboard

Copied

Ohh, yes - thanks again! I'm excited to get playing with my automation now!

Votes

Translate

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
Explorer ,
Jun 24, 2022 Jun 24, 2022

Copy link to clipboard

Copied

Is there a simple way to adapt this script to add a sequence number instead of the random element? 

Giving the following result:

Motif Layer 1

Motif Layer 2

Motif Layer 3

...

Motif Layer 25

etc...

 

This would really help me in cutting out a load of actions which I presently use instead!

Votes

Translate

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 ,
Jun 24, 2022 Jun 24, 2022

Copy link to clipboard

Copied


@bobd7221748 wrote:

Is there a simple way to adapt this script to add a sequence number instead of the random element? 


 

Only within the active Photoshop session until quitting? Or picking up on the next incremental number after exiting and restarting Photoshop?

Votes

Translate

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
Explorer ,
Jun 24, 2022 Jun 24, 2022

Copy link to clipboard

Copied

Only within the active script session itself. So if I run the script with 5 layers selected, I get 1-5. Then if I run the script again a moment later on 3 other layers, it starts over and I get 1-3 again. If that makes sense... 

Votes

Translate

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
Explorer ,
Jun 24, 2022 Jun 24, 2022

Copy link to clipboard

Copied

Actually, my previous explaination wasn't strictly what I want.

Hopefully below explains better. If I had the following:

 

Layer 

Layer copy

Layer 2

 

Running the script would then achieve:

 

Custom Name 1

Custom Name 2

Custom Name 3

 

Then, if 2 more layers were added in, running the script on those would carry on the sequence:

 

Custom Name 1

Custom Name 2

Custom Name 3

Layer 0

Layer 0 copy

 

Would then become...

 

Custom Name 1

Custom Name 2

Custom Name 3

Custom Name 4

Custom Name 5

 

Hopefully that makes more sense? If this is deviating too much from the original question, perhaps I should make a new post...

Votes

Translate

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 ,
Jun 25, 2022 Jun 25, 2022

Copy link to clipboard

Copied

I am stuck with the counter resetting after the first run.

 

Perhaps somebody else can take over and post code?

Votes

Translate

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
Explorer ,
Jun 25, 2022 Jun 25, 2022

Copy link to clipboard

Copied

LATEST

No problem, I very much appreciate you taking the effort to have a go. If you have anything that comes close you could share which you've so far tried, I've created a new thread:

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/rename-layers-with-custom-name-amp-se...

 

Thanks again!

 

Votes

Translate

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