Script to find/replace text in layer names?
Script to find/replace text in layer names?
I'm looking for a script to perform a standard text "find and replace" of group names and layer names within photoshop... a way of error-checking documents that have many hundreds of layers that will need to conform to a particular naming convention.
Script to find/replace text in layer names?
Here's a start, might be a bit slow though..
Code: Select allfunction findReplaceLayerName(obj,Find,Replace){
var regFind = new RegExp(Find,"gi");
if(obj.artLayers.length>0){
for(var z = 0;z<obj.artLayers.length;z++){
var layer = obj.artLayers[z];
layer.name = layer.name.replace(regFind,Replace);
}
if(obj.layerSets.length > 0){
for(var l=0;l<obj.layerSets.length;l++){
findReplaceLayerName(obj.layerSets[l],Find,Replace);
}
}
}
}
findReplaceLayerName(activeDocument,"Text","NewText");
Code: Select allfunction findReplaceLayerName(obj,Find,Replace){
var regFind = new RegExp(Find,"gi");
if(obj.artLayers.length>0){
for(var z = 0;z<obj.artLayers.length;z++){
var layer = obj.artLayers[z];
layer.name = layer.name.replace(regFind,Replace);
}
if(obj.layerSets.length > 0){
for(var l=0;l<obj.layerSets.length;l++){
findReplaceLayerName(obj.layerSets[l],Find,Replace);
}
}
}
}
findReplaceLayerName(activeDocument,"Text","NewText");
Script to find/replace text in layer names?
"Here's a start, might be a bit slow though.. "
Wow, thanks- that's a great start!
Wow, thanks- that's a great start!
Script to find/replace text in layer names?
If I have a list of separate characters or strings of text that I want to replace with a single character or string, I'm wondering how best to implement that?
For instance, if I want to replace all uses of #, @, &, !, etc... with an underscore or other such character.
I see duplicating the command line
"findReplaceLayerName(activeDocument,"Text","NewText"); "
doesn't work... still very new to this stuff!
For instance, if I want to replace all uses of #, @, &, !, etc... with an underscore or other such character.
I see duplicating the command line
"findReplaceLayerName(activeDocument,"Text","NewText"); "
doesn't work... still very new to this stuff!
Script to find/replace text in layer names?
Unless I'm mistaken, the function uses regular expressions. Does anyone have photoshop javascript experience in the matter? Can we use reg-exp inside the parameter parenthesis instead of using quotes? Also, would it be a lot of work to have a dialogue window pop up so we could enter the two separate strings for 'text' and 'new text' with default values to return the original name ($1 + "suffix") or something?
(picked-up on my enthousiasm yet? I've been wanting to do something like this for a while )
(picked-up on my enthousiasm yet? I've been wanting to do something like this for a while )
Script to find/replace text in layer names?
You would have to do this in two passes. One to do the simple text replacement, and a second to remove special characters.
Code: Select allfindReplaceLayerName(activeDocument, "Text", "NewText");
findReplaceLayerName(activeDocument, /\#|@|&|!/g, '_');
-X
Code: Select allfindReplaceLayerName(activeDocument, "Text", "NewText");
findReplaceLayerName(activeDocument, /\#|@|&|!/g, '_');
-X
Script to find/replace text in layer names?
Nice... and what would the syntax be if you were to do this ?
'entire original layername'
(becomes)
'prefix' + 'entire original layername' + 'suffix'
I'm thinking $1 $2 and so on would return words if the layer name contains spaces. How would I get the whole thing without using layer.name?
'entire original layername'
(becomes)
'prefix' + 'entire original layername' + 'suffix'
I'm thinking $1 $2 and so on would return words if the layer name contains spaces. How would I get the whole thing without using layer.name?
Script to find/replace text in layer names?
pattesdours wrote:Can we use reg-exp inside the parameter parenthesis instead of using quotes?
That function Paul posted is just using a regex string, so you can just put in a regex that meets all your needs into that function. For example you should be able to just use "[#@&!]" without quotes as your find string to replace all of those in the same pass.
I took what Paul posted and added a dialog for it.
Code: Select allfunction main()
{
try
{
// Paul's function to loop through document
function findReplaceLayerName(obj,Find,Replace){
var regFind = new RegExp(Find,"gi");
if(obj.artLayers.length>0){
for(var z = 0;z<obj.artLayers.length;z++){
var layer = obj.artLayers[z];
layer.name = layer.name.replace(regFind,Replace);
}
if(obj.layerSets.length > 0){
for(var l=0;l<obj.layerSets.length;l++){
findReplaceLayerName(obj.layerSets[l],Find,Replace);
}
}
}
}
// build dialog box
var win = new Window("dialog{text:':)',bounds:[100,100,350,240],\
panel0:Panel{bounds:[10,10,240,130] , text:'Find & Replace' ,properties:{borderStyle:'etched',su1PanelCoordinates:true},\
statictext0:StaticText{bounds:[10,20,90,37] , text:'Find:' ,properties:{scrolling:undefined,multiline:undefined}},\
statictext1:StaticText{bounds:[10,40,90,57] , text:'Replacement:' ,properties:{scrolling:undefined,multiline:undefined}},\
edittext0:EditText{bounds:[100,16,220,36] , text:'' ,properties:{multiline:false,noecho:false,readonly:false}},\
edittext1:EditText{bounds:[100,40,220,60] , text:'' ,properties:{multiline:false,noecho:false,readonly:false}},\
button0:Button{bounds:[10,70,220,100] , text:'Go!' }\
}\
};");
// set dialog button to execute our function
win.panel0.button0.onClick = function() {
findReplaceLayerName(activeDocument,win.panel0.edittext0.text,win.panel0.edittext1.text);
alert("DONE!");
}
// show user the dialog
win.center();
win.show();
} catch(e) {
// if the script fails, give the user a reason
alert("Script failed with the following error: \n\n"+ e);
}
}
// run script
main();
Patrick
That function Paul posted is just using a regex string, so you can just put in a regex that meets all your needs into that function. For example you should be able to just use "[#@&!]" without quotes as your find string to replace all of those in the same pass.
I took what Paul posted and added a dialog for it.
Code: Select allfunction main()
{
try
{
// Paul's function to loop through document
function findReplaceLayerName(obj,Find,Replace){
var regFind = new RegExp(Find,"gi");
if(obj.artLayers.length>0){
for(var z = 0;z<obj.artLayers.length;z++){
var layer = obj.artLayers[z];
layer.name = layer.name.replace(regFind,Replace);
}
if(obj.layerSets.length > 0){
for(var l=0;l<obj.layerSets.length;l++){
findReplaceLayerName(obj.layerSets[l],Find,Replace);
}
}
}
}
// build dialog box
var win = new Window("dialog{text:':)',bounds:[100,100,350,240],\
panel0:Panel{bounds:[10,10,240,130] , text:'Find & Replace' ,properties:{borderStyle:'etched',su1PanelCoordinates:true},\
statictext0:StaticText{bounds:[10,20,90,37] , text:'Find:' ,properties:{scrolling:undefined,multiline:undefined}},\
statictext1:StaticText{bounds:[10,40,90,57] , text:'Replacement:' ,properties:{scrolling:undefined,multiline:undefined}},\
edittext0:EditText{bounds:[100,16,220,36] , text:'' ,properties:{multiline:false,noecho:false,readonly:false}},\
edittext1:EditText{bounds:[100,40,220,60] , text:'' ,properties:{multiline:false,noecho:false,readonly:false}},\
button0:Button{bounds:[10,70,220,100] , text:'Go!' }\
}\
};");
// set dialog button to execute our function
win.panel0.button0.onClick = function() {
findReplaceLayerName(activeDocument,win.panel0.edittext0.text,win.panel0.edittext1.text);
alert("DONE!");
}
// show user the dialog
win.center();
win.show();
} catch(e) {
// if the script fails, give the user a reason
alert("Script failed with the following error: \n\n"+ e);
}
}
// run script
main();
Patrick
Script to find/replace text in layer names?
Thanks Patrick and Xbytor, Just modified the script slightly Patrick it should now rename layerset names as well as layer names.
Code: Select allfunction main()
{
try
{
function findReplaceLayerName(obj,Find,Replace){
var regFind = new RegExp(Find,"gi");
if(obj.artLayers.length>0){
for(var z = 0;z<obj.artLayers.length;z++){
var layer = obj.artLayers[z];
layer.name = layer.name.replace(regFind,Replace);
}
if(obj.layerSets.length > 0){
for(var l=0;l<obj.layerSets.length;l++){
var lname = obj.layerSets[l].name.replace(regFind,Replace);
obj.layerSets[l].name=lname;
findReplaceLayerName(obj.layerSets[l],Find,Replace);
}
}
}
}
// build dialog box
var win = new Window("dialog{text:':)))',bounds:[100,100,350,240],"+
"panel0:Panel{bounds:[10,10,240,130] , text:'Find & Replace' ,properties:{borderStyle:'etched',su1PanelCoordinates:true},"+
"statictext0:StaticText{bounds:[10,20,90,37] , text:'Find:' ,properties:{scrolling:undefined,multiline:undefined}},"+
"statictext1:StaticText{bounds:[10,40,90,57] , text:'Replacement:' ,properties:{scrolling:undefined,multiline:undefined}},"+
"edittext0:EditText{bounds:[100,16,220,36] , text:'' ,properties:{multiline:false,noecho:false,readonly:false}},"+
"edittext1:EditText{bounds:[100,40,220,60] , text:'' ,properties:{multiline:false,noecho:false,readonly:false}},"+
"button0:Button{bounds:[10,70,120,100] , text:'Go!' },"+
"button1:Button{bounds:[122,70,220,100] , text:'Cancel' }} };");
// set dialog button to execute our function
win.panel0.button0.onClick = function() {
findReplaceLayerName(activeDocument,win.panel0.edittext0.text,win.panel0.edittext1.text);
alert("DONE!");
}
// show user the dialog
win.center();
win.show();
} catch(e) {
// if the script fails, give the user a reason
alert("Script failed with the following error: \n\n"+ e);
}
}
// run script
main();
Code: Select allfunction main()
{
try
{
function findReplaceLayerName(obj,Find,Replace){
var regFind = new RegExp(Find,"gi");
if(obj.artLayers.length>0){
for(var z = 0;z<obj.artLayers.length;z++){
var layer = obj.artLayers[z];
layer.name = layer.name.replace(regFind,Replace);
}
if(obj.layerSets.length > 0){
for(var l=0;l<obj.layerSets.length;l++){
var lname = obj.layerSets[l].name.replace(regFind,Replace);
obj.layerSets[l].name=lname;
findReplaceLayerName(obj.layerSets[l],Find,Replace);
}
}
}
}
// build dialog box
var win = new Window("dialog{text:':)))',bounds:[100,100,350,240],"+
"panel0:Panel{bounds:[10,10,240,130] , text:'Find & Replace' ,properties:{borderStyle:'etched',su1PanelCoordinates:true},"+
"statictext0:StaticText{bounds:[10,20,90,37] , text:'Find:' ,properties:{scrolling:undefined,multiline:undefined}},"+
"statictext1:StaticText{bounds:[10,40,90,57] , text:'Replacement:' ,properties:{scrolling:undefined,multiline:undefined}},"+
"edittext0:EditText{bounds:[100,16,220,36] , text:'' ,properties:{multiline:false,noecho:false,readonly:false}},"+
"edittext1:EditText{bounds:[100,40,220,60] , text:'' ,properties:{multiline:false,noecho:false,readonly:false}},"+
"button0:Button{bounds:[10,70,120,100] , text:'Go!' },"+
"button1:Button{bounds:[122,70,220,100] , text:'Cancel' }} };");
// set dialog button to execute our function
win.panel0.button0.onClick = function() {
findReplaceLayerName(activeDocument,win.panel0.edittext0.text,win.panel0.edittext1.text);
alert("DONE!");
}
// show user the dialog
win.center();
win.show();
} catch(e) {
// if the script fails, give the user a reason
alert("Script failed with the following error: \n\n"+ e);
}
}
// run script
main();
Script to find/replace text in layer names?
FWIW, this should should remove " copy" layer name extensions as well as any associated sequence numbers.
Code: Select allfindReplaceLayerName(activeDocument, " copy( \\d+)?$", '')
-X
Code: Select allfindReplaceLayerName(activeDocument, " copy( \\d+)?$", '')
-X