Rename layers with custom name & sequence number

Explorer ,
Jun 25, 2022 Jun 25, 2022

Copy link to clipboard

Copied

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!

 

 

 

TOPICS
Actions and scripting , Windows

Views

74

Likes

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

Explorer , Jun 28, 2022 Jun 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 highe
...

Likes

Translate

Translate
Adobe Community Professional ,
Jun 26, 2022 Jun 26, 2022

Copy link to clipboard

Copied

Hi, what do you like is very customized script but maybe you can start from here:

https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-batch-rename-layers-with-se...

 

Likes

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
Adobe Community Professional ,
Jun 26, 2022 Jun 26, 2022

Copy link to clipboard

Copied

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#M...

Likes

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

Copy link to clipboard

Copied

My last question was about creating a custom named layer with a random string afterwards, which was perfectly answer by @Stephen_A_Marshhttps://community.adobe.com/t5/photoshop-ecosystem-discussions/rename-photoshop-layer-to-a-unique-na...

 

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!

 

Likes

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

Copy link to clipboard

Copied

LATEST

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.

Likes

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