Skip to main content
Participant
February 13, 2008
Question

PS3 script to batch rename layers

  • February 13, 2008
  • 11 replies
  • 14785 views
Sometimes, I make copies of layers (for example: copy little diamonds to align into a shape of the letter M). Each time I "alt+drag" the content in that layer (to make a copy of it), PS makes a copy and then appends the name of the layer to something like "diamond copy 3". After making a ton of copies (and because I'm anal about having actual names for each layer), I want each layer to read "diamond". Instead of manually renaming all these layers, is there a way to highlight the affected layers and choose to rename all layers according to what you want?
This topic has been closed for replies.

11 replies

MarkWalsh
Inspiring
February 28, 2008
You're in luck. I just finished writing this yesterday. :)

This will remove " copy", " copy 1"... etc from all open files. Let me know how it works out for you.

///////////////////////////////////////////////////////////////////////////////
// Script Name: RenameLayers.jsx
// Version: 1.0
// Date: 2/27/08
// Usage: Removes 'copy 1, copy 2, etc' from name of all layers
///////////////////////////////////////////////////////////////////////////////

#target photoshop
//$.localize = true;
var displayDialogMode = app.displayDialogs;

app.bringToFront();
app.bringToFront();
app.displayDialogs = DialogModes.NO;

processObjects(app.documents, processDocument,function () {alert("You have no documents open")})
app.displayDialogs = displayDialogMode;

///////////////////////////////////////////////////////////////////////////////
// Function: processObjects
// Usage: Runs theMainFunction on every object in list theObjects
// Input: List of objects (documents, layers, etc.), Function to run on each object, Function to run on parent if there are no object
// Return: value returned from function used
///////////////////////////////////////////////////////////////////////////////
function processObjects(theObjects, theMainFunction, theAlternateFunction) {
var returnValue = null;
if (theObjects.length ==0) {
if (!(theAlternateFunction == undefined) && !(theAlternateFunction == null) && !(theAlternateFunction == "")) {
returnValue = theAlternateFunction (theObjects.parent);
}
} else {
if (!(theMainFunction == undefined) && !(theMainFunction == null) && !(theMainFunction == "")) {
for (var i = theObjects.length -1; i> -1; i--) {
returnValue += theMainFunction (theObjects, i);
}
}
}
return returnValue;
}

///////////////////////////////////////////////////////////////////////////////
// Function: processDocument
// Usage: Processes layers of document with 'processLayer' function
// Input: Document object, Document object index
// Return: none
///////////////////////////////////////////////////////////////////////////////
function processDocument(objectRef, i) {
app.activeDocument = objectRef
processObjects(objectRef.layers, processLayer, null);
}

///////////////////////////////////////////////////////////////////////////////
// Function: processLayer
// Usage: Removes 'copy' from layer name (processes Layer Set contents)
// Input: Layer object, Layer Object index
// Return: none
///////////////////////////////////////////////////////////////////////////////
function processLayer(objectRef, i) {
if (objectRef.typename == 'LayerSet') {
var layerCount = processObjects(objectRef.layers, processLayer);
}
renameLayer(objectRef)
return 0
}

///////////////////////////////////////////////////////////////////////////////
// Function: renameLayer
// Usage: Removes 'copy' from layer name (processes Layer Set contents)
// Input: Layer object, Layer Object index
// Return: none
///////////////////////////////////////////////////////////////////////////////
function renameLayer(objectRef) {
var theRegEx = new RegExp(/^(.*)(copy(\s(\d)+)*)+$/)
if (theRegEx.test(objectRef.name)) {
var indexNumber = 0

indexnumber = objectRef.name.indexOf(" copy")
objectRef.name = objectRef.name.substr(0,indexnumber)
}
return 0
}