Skip to main content
bobd7221748
Inspiring
June 25, 2022
Answered

Rename layers with custom name & sequence number

  • June 25, 2022
  • 4 replies
  • 6332 views

Hi Everyone,

 

I'm looking for a script that will rename selected layers (with a predefined name within the script), followed by a sequence number. I don't want a dialogue box popping up, as that would halt the action I intend to use it in.

 

For example:

 

With the layers...

Layer 0

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 this makes sense? Ideally, but not a necessity, the script would be capable of identifying existing layers with the given name and would fill around any missing in the sequence.

 

So with the following layers..

 

Custom Name 1

Custom Name 3

Custom Name 4

Custom Name 5

Layer 0

Layer copy

 

Running the script on the last two would result in:

 

Custom Name 1

Custom Name 3

Custom Name 4

Custom Name 5

Custom Name 2

Custom Name 6

 

Any suggestions would be hugely appreciated - thanks in advance!

 

 

 

This topic has been closed for replies.
Correct answer bobd7221748

I managed to find sort this on my own in the end. I already had a script for renaming layers like this, however it presented a dialogue, which I didn't want. So I've edited that script to go straight through with changing the layer names. The original author of the script is Trevor Morris (trevor@morris-photographics.com).

 

Below is his original script, which gives a dialogue for what to re-name the layers to:

// Rename Layers - Adobe Photoshop Script
// Requirements: Adobe Photoshop CS2, or higher
// Description: renames and numbers all layers in the active document (using the supplied name pattern)
// Version: 1.8.1, 5/June/2010
// Author: Trevor Morris (trevor@morris-photographics.com)
// Website: http://morris-photographics.com/
// ============================================================================
// Installation:
// 1. Place script in 'C:\Program Files\Adobe\Adobe Photoshop CS#\Presets\Scripts\'
// 2. Restart Photoshop
// 3. Choose File > Scripts > Rename Layers
// ============================================================================

// enable double-clicking from Mac Finder or Windows Explorer
// this command only works in Photoshop CS2 and higher
#target photoshop

// bring application forward for double-click events
app.bringToFront();

///////////////////////////////////////////////////////////////////////////////
// main - main function
///////////////////////////////////////////////////////////////////////////////
function main() {
	// user settings
	var prefs = new Object();
	prefs.countFrom     = 1;   // number to start counting from (default: 1)
	prefs.zeroPadding   = 3;   // number of digits to use for the layer number (defaul: 3)
	prefs.nameSeparator = ' '; // character to insert between the layer name and number (default: ' ')
	prefs.topToBottom   = true; // rename layers top to bottom (true) or bottom to top (false)

	// prompt for layer name
	prefs.layerPattern = prompt('Enter the rename pattern to be used for all layers.\n' +
		'For example, enter "Layer" to rename layers as "Layer 01", "Layer 02", etc.', 'Layer');

	// rename layers
	if (prefs.layerPattern) {
		renameLayers(activeDocument, prefs);
	}
}

///////////////////////////////////////////////////////////////////////////////
// renameLayers - rename layers, top to bottom, or bottom to top
///////////////////////////////////////////////////////////////////////////////
function renameLayers(ref, prefs) {
	// declare local variables
	var len = ref.layers.length;

	// rename layers top to bottom
	if (prefs.topToBottom) {
		for (var i = 0; i < len; i++) {
			rename();
		}
	}
	// rename layers bottom to top
	else {
		for (var i = len - 1; i >= 0; i--) {
			rename();
		}
	}

	// rename - rename layer
	function rename() {
		var layer = ref.layers[i];
		var vis = layer.visible;

		// check for groups
		if (layer.typename == 'LayerSet') {
			renameLayers(layer, prefs);
		}
		// rename layer
		else {
			layer.name = prefs.layerPattern + prefs.nameSeparator +
				(prefs.countFrom + Math.pow(10, prefs.zeroPadding)).toString().substr(1);
			if (!vis) {
				layer.visible = false;
			}
			prefs.countFrom++;
		}
	}
}

///////////////////////////////////////////////////////////////////////////////
// isCorrectVersion - check for Adobe Photoshop CS2 (v9) or higher
///////////////////////////////////////////////////////////////////////////////
function isCorrectVersion() {
	if (parseInt(version, 10) >= 9) {
		return true;
	}
	else {
		alert('This script requires Adobe Photoshop CS2 or higher.', 'Wrong Version', false);
		return false;
	}
}

///////////////////////////////////////////////////////////////////////////////
// isOpenDocs - ensure at least one document is open
///////////////////////////////////////////////////////////////////////////////
function isOpenDocs() {
	if (documents.length) {
		return true;
	}
	else {
		alert('There are no documents open.', 'No Documents Open', false);
		return false;
	}
}

///////////////////////////////////////////////////////////////////////////////
// showError - display error message if something goes wrong
///////////////////////////////////////////////////////////////////////////////
function showError(err) {
	if (confirm('An unknown error has occurred.\n' +
		'Would you like to see more information?', true, 'Unknown Error')) {
			alert(err + ': on line ' + err.line, 'Script Error', true);
	}
}


///////////////////////////////////////////////////////////////////////////////
// test initial conditions prior to running main function
///////////////////////////////////////////////////////////////////////////////
if (isCorrectVersion() && isOpenDocs()) {
	try {
		// suspend history for CS3 (v10) or higher
		if (parseInt(version, 10) >= 10) {
			activeDocument.suspendHistory('Rename Layers', 'main()');
		}
		// just run main for CS2 (v9)
		else {
			main();
		}
	}
	catch(e) {
		if (e.number != 8007) { // don't report error on user cancel
			showError(e);
		}
	}
}

Then, I altered the script at line 33, whereby I could add in my custom layer name:

// Rename Layers - Adobe Photoshop Script
// Requirements: Adobe Photoshop CS2, or higher
// Description: renames and numbers all layers in the active document (using the supplied name pattern)
// Version: 1.8.1, 5/June/2010
// Author: Trevor Morris (trevor@morris-photographics.com)
// Website: http://morris-photographics.com/
// ============================================================================
// Installation:
// 1. Place script in 'C:\Program Files\Adobe\Adobe Photoshop CS#\Presets\Scripts\'
// 2. Restart Photoshop
// 3. Choose File > Scripts > Rename Layers
// ============================================================================

// enable double-clicking from Mac Finder or Windows Explorer
// this command only works in Photoshop CS2 and higher
#target photoshop

// bring application forward for double-click events
app.bringToFront();

///////////////////////////////////////////////////////////////////////////////
// main - main function
///////////////////////////////////////////////////////////////////////////////
function main() {
	// user settings
	var prefs = new Object();
	prefs.countFrom     = 1;   // number to start counting from (default: 1)
	prefs.zeroPadding   = 2;   // number of digits to use for the layer number (defaul: 3)
	prefs.nameSeparator = ' '; // character to insert between the layer name and number (default: ' ')
	prefs.topToBottom   = true; // rename layers top to bottom (true) or bottom to top (false)

	// prompt for layer name
	prefs.layerPattern = "CUSTOM LAYER NAME HERE"

	// rename layers
	if (prefs.layerPattern) {
		renameLayers(activeDocument, prefs);
	}
}

///////////////////////////////////////////////////////////////////////////////
// renameLayers - rename layers, top to bottom, or bottom to top
///////////////////////////////////////////////////////////////////////////////
function renameLayers(ref, prefs) {
	// declare local variables
	var len = ref.layers.length;

	// rename layers top to bottom
	if (prefs.topToBottom) {
		for (var i = 0; i < len; i++) {
			rename();
		}
	}
	// rename layers bottom to top
	else {
		for (var i = len - 1; i >= 0; i--) {
			rename();
		}
	}

	// rename - rename layer
	function rename() {
		var layer = ref.layers[i];
		var vis = layer.visible;

		// check for groups
		if (layer.typename == 'LayerSet') {
			renameLayers(layer, prefs);
		}
		// rename layer
		else {
			layer.name = prefs.layerPattern + prefs.nameSeparator +
				(prefs.countFrom + Math.pow(10, prefs.zeroPadding)).toString().substr(1);
			if (!vis) {
				layer.visible = false;
			}
			prefs.countFrom++;
		}
	}
}

///////////////////////////////////////////////////////////////////////////////
// isCorrectVersion - check for Adobe Photoshop CS2 (v9) or higher
///////////////////////////////////////////////////////////////////////////////
function isCorrectVersion() {
	if (parseInt(version, 10) >= 9) {
		return true;
	}
	else {
		alert('This script requires Adobe Photoshop CS2 or higher.', 'Wrong Version', false);
		return false;
	}
}

///////////////////////////////////////////////////////////////////////////////
// isOpenDocs - ensure at least one document is open
///////////////////////////////////////////////////////////////////////////////
function isOpenDocs() {
	if (documents.length) {
		return true;
	}
	else {
		alert('There are no documents open.', 'No Documents Open', false);
		return false;
	}
}

///////////////////////////////////////////////////////////////////////////////
// showError - display error message if something goes wrong
///////////////////////////////////////////////////////////////////////////////
function showError(err) {
	if (confirm('An unknown error has occurred.\n' +
		'Would you like to see more information?', true, 'Unknown Error')) {
			alert(err + ': on line ' + err.line, 'Script Error', true);
	}
}


///////////////////////////////////////////////////////////////////////////////
// test initial conditions prior to running main function
///////////////////////////////////////////////////////////////////////////////
if (isCorrectVersion() && isOpenDocs()) {
	try {
		// suspend history for CS3 (v10) or higher
		if (parseInt(version, 10) >= 10) {
			activeDocument.suspendHistory('Rename Layers', 'main()');
		}
		// just run main for CS2 (v9)
		else {
			main();
		}
	}
	catch(e) {
		if (e.number != 8007) { // don't report error on user cancel
			showError(e);
		}
	}
}

 

Hopefully this can help some other searches down the line! Thanks for all the suggestions otherwise.

4 replies

bobd7221748
bobd7221748AuthorCorrect answer
Inspiring
June 28, 2022

I managed to find sort this on my own in the end. I already had a script for renaming layers like this, however it presented a dialogue, which I didn't want. So I've edited that script to go straight through with changing the layer names. The original author of the script is Trevor Morris (trevor@morris-photographics.com).

 

Below is his original script, which gives a dialogue for what to re-name the layers to:

// Rename Layers - Adobe Photoshop Script
// Requirements: Adobe Photoshop CS2, or higher
// Description: renames and numbers all layers in the active document (using the supplied name pattern)
// Version: 1.8.1, 5/June/2010
// Author: Trevor Morris (trevor@morris-photographics.com)
// Website: http://morris-photographics.com/
// ============================================================================
// Installation:
// 1. Place script in 'C:\Program Files\Adobe\Adobe Photoshop CS#\Presets\Scripts\'
// 2. Restart Photoshop
// 3. Choose File > Scripts > Rename Layers
// ============================================================================

// enable double-clicking from Mac Finder or Windows Explorer
// this command only works in Photoshop CS2 and higher
#target photoshop

// bring application forward for double-click events
app.bringToFront();

///////////////////////////////////////////////////////////////////////////////
// main - main function
///////////////////////////////////////////////////////////////////////////////
function main() {
	// user settings
	var prefs = new Object();
	prefs.countFrom     = 1;   // number to start counting from (default: 1)
	prefs.zeroPadding   = 3;   // number of digits to use for the layer number (defaul: 3)
	prefs.nameSeparator = ' '; // character to insert between the layer name and number (default: ' ')
	prefs.topToBottom   = true; // rename layers top to bottom (true) or bottom to top (false)

	// prompt for layer name
	prefs.layerPattern = prompt('Enter the rename pattern to be used for all layers.\n' +
		'For example, enter "Layer" to rename layers as "Layer 01", "Layer 02", etc.', 'Layer');

	// rename layers
	if (prefs.layerPattern) {
		renameLayers(activeDocument, prefs);
	}
}

///////////////////////////////////////////////////////////////////////////////
// renameLayers - rename layers, top to bottom, or bottom to top
///////////////////////////////////////////////////////////////////////////////
function renameLayers(ref, prefs) {
	// declare local variables
	var len = ref.layers.length;

	// rename layers top to bottom
	if (prefs.topToBottom) {
		for (var i = 0; i < len; i++) {
			rename();
		}
	}
	// rename layers bottom to top
	else {
		for (var i = len - 1; i >= 0; i--) {
			rename();
		}
	}

	// rename - rename layer
	function rename() {
		var layer = ref.layers[i];
		var vis = layer.visible;

		// check for groups
		if (layer.typename == 'LayerSet') {
			renameLayers(layer, prefs);
		}
		// rename layer
		else {
			layer.name = prefs.layerPattern + prefs.nameSeparator +
				(prefs.countFrom + Math.pow(10, prefs.zeroPadding)).toString().substr(1);
			if (!vis) {
				layer.visible = false;
			}
			prefs.countFrom++;
		}
	}
}

///////////////////////////////////////////////////////////////////////////////
// isCorrectVersion - check for Adobe Photoshop CS2 (v9) or higher
///////////////////////////////////////////////////////////////////////////////
function isCorrectVersion() {
	if (parseInt(version, 10) >= 9) {
		return true;
	}
	else {
		alert('This script requires Adobe Photoshop CS2 or higher.', 'Wrong Version', false);
		return false;
	}
}

///////////////////////////////////////////////////////////////////////////////
// isOpenDocs - ensure at least one document is open
///////////////////////////////////////////////////////////////////////////////
function isOpenDocs() {
	if (documents.length) {
		return true;
	}
	else {
		alert('There are no documents open.', 'No Documents Open', false);
		return false;
	}
}

///////////////////////////////////////////////////////////////////////////////
// showError - display error message if something goes wrong
///////////////////////////////////////////////////////////////////////////////
function showError(err) {
	if (confirm('An unknown error has occurred.\n' +
		'Would you like to see more information?', true, 'Unknown Error')) {
			alert(err + ': on line ' + err.line, 'Script Error', true);
	}
}


///////////////////////////////////////////////////////////////////////////////
// test initial conditions prior to running main function
///////////////////////////////////////////////////////////////////////////////
if (isCorrectVersion() && isOpenDocs()) {
	try {
		// suspend history for CS3 (v10) or higher
		if (parseInt(version, 10) >= 10) {
			activeDocument.suspendHistory('Rename Layers', 'main()');
		}
		// just run main for CS2 (v9)
		else {
			main();
		}
	}
	catch(e) {
		if (e.number != 8007) { // don't report error on user cancel
			showError(e);
		}
	}
}

Then, I altered the script at line 33, whereby I could add in my custom layer name:

// Rename Layers - Adobe Photoshop Script
// Requirements: Adobe Photoshop CS2, or higher
// Description: renames and numbers all layers in the active document (using the supplied name pattern)
// Version: 1.8.1, 5/June/2010
// Author: Trevor Morris (trevor@morris-photographics.com)
// Website: http://morris-photographics.com/
// ============================================================================
// Installation:
// 1. Place script in 'C:\Program Files\Adobe\Adobe Photoshop CS#\Presets\Scripts\'
// 2. Restart Photoshop
// 3. Choose File > Scripts > Rename Layers
// ============================================================================

// enable double-clicking from Mac Finder or Windows Explorer
// this command only works in Photoshop CS2 and higher
#target photoshop

// bring application forward for double-click events
app.bringToFront();

///////////////////////////////////////////////////////////////////////////////
// main - main function
///////////////////////////////////////////////////////////////////////////////
function main() {
	// user settings
	var prefs = new Object();
	prefs.countFrom     = 1;   // number to start counting from (default: 1)
	prefs.zeroPadding   = 2;   // number of digits to use for the layer number (defaul: 3)
	prefs.nameSeparator = ' '; // character to insert between the layer name and number (default: ' ')
	prefs.topToBottom   = true; // rename layers top to bottom (true) or bottom to top (false)

	// prompt for layer name
	prefs.layerPattern = "CUSTOM LAYER NAME HERE"

	// rename layers
	if (prefs.layerPattern) {
		renameLayers(activeDocument, prefs);
	}
}

///////////////////////////////////////////////////////////////////////////////
// renameLayers - rename layers, top to bottom, or bottom to top
///////////////////////////////////////////////////////////////////////////////
function renameLayers(ref, prefs) {
	// declare local variables
	var len = ref.layers.length;

	// rename layers top to bottom
	if (prefs.topToBottom) {
		for (var i = 0; i < len; i++) {
			rename();
		}
	}
	// rename layers bottom to top
	else {
		for (var i = len - 1; i >= 0; i--) {
			rename();
		}
	}

	// rename - rename layer
	function rename() {
		var layer = ref.layers[i];
		var vis = layer.visible;

		// check for groups
		if (layer.typename == 'LayerSet') {
			renameLayers(layer, prefs);
		}
		// rename layer
		else {
			layer.name = prefs.layerPattern + prefs.nameSeparator +
				(prefs.countFrom + Math.pow(10, prefs.zeroPadding)).toString().substr(1);
			if (!vis) {
				layer.visible = false;
			}
			prefs.countFrom++;
		}
	}
}

///////////////////////////////////////////////////////////////////////////////
// isCorrectVersion - check for Adobe Photoshop CS2 (v9) or higher
///////////////////////////////////////////////////////////////////////////////
function isCorrectVersion() {
	if (parseInt(version, 10) >= 9) {
		return true;
	}
	else {
		alert('This script requires Adobe Photoshop CS2 or higher.', 'Wrong Version', false);
		return false;
	}
}

///////////////////////////////////////////////////////////////////////////////
// isOpenDocs - ensure at least one document is open
///////////////////////////////////////////////////////////////////////////////
function isOpenDocs() {
	if (documents.length) {
		return true;
	}
	else {
		alert('There are no documents open.', 'No Documents Open', false);
		return false;
	}
}

///////////////////////////////////////////////////////////////////////////////
// showError - display error message if something goes wrong
///////////////////////////////////////////////////////////////////////////////
function showError(err) {
	if (confirm('An unknown error has occurred.\n' +
		'Would you like to see more information?', true, 'Unknown Error')) {
			alert(err + ': on line ' + err.line, 'Script Error', true);
	}
}


///////////////////////////////////////////////////////////////////////////////
// test initial conditions prior to running main function
///////////////////////////////////////////////////////////////////////////////
if (isCorrectVersion() && isOpenDocs()) {
	try {
		// suspend history for CS3 (v10) or higher
		if (parseInt(version, 10) >= 10) {
			activeDocument.suspendHistory('Rename Layers', 'main()');
		}
		// just run main for CS2 (v9)
		else {
			main();
		}
	}
	catch(e) {
		if (e.number != 8007) { // don't report error on user cancel
			showError(e);
		}
	}
}

 

Hopefully this can help some other searches down the line! Thanks for all the suggestions otherwise.

Known Participant
January 26, 2023

only Selected layer  rename ?

 

Stephen Marsh
Community Expert
Community Expert
January 26, 2023

@nandu nag 

 

Try this:

 

https://raw.githubusercontent.com/Paul-Riggott/PS-Scripts/master/Layer%20Name%20Edit.jsx

 

Many of Paul Riggott’s other useful scripts can be found here:

 

https://github.com/Paul-Riggott/PS-Scripts

 

Edit:

 

You can change the following code from the light interface colour:

 

        var myBrush = g.newBrush(g.BrushType.SOLID_COLOR, [0.99, 0.99, 0.99, 1]);

 

To a dark interface colour:

 

        var myBrush = g.newBrush(g.BrushType.SOLID_COLOR, [0.2, 0.2, 0.2, 1]);

 

Other additions are of course possible, such as changing the default radio button or having default find/replace text or regular expression patterns etc.

 

bobd7221748
Inspiring
June 27, 2022

My last question was about creating a custom named layer with a random string afterwards, which was perfectly answer by @Stephen Marshhttps://community.adobe.com/t5/photoshop-ecosystem-discussions/rename-photoshop-layer-to-a-unique-name/m-p/13022213#M652214

 

If I could find a script that gets close to what I've asked, I could probably work with it - so I'm open to suggestions!

However, it is creating the sequence that appears to be the difficult bit.

If I could acheive this in a script, it would save me having a load of repetetive actions (just a different layer re-name in each action). I think it would be a useful script for any future searches too!

 

c.pfaffenbichler
Community Expert
Community Expert
June 26, 2022

What is giving you problems? 

I think there are plenty of examples of Scripts for performing some operation on the selected Layers, renaming them would not seem extremely difficult. 

 

edit: 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/resize-selected-layers/m-p/12726182#M620620

Community Expert
June 26, 2022