rename layer comps

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

deluxe

rename layer comps

Post by deluxe »

Hi,
Could anyone please write a script that will rename layer comps?
Search for a value and replace with another value, also would be handy if you could add a prefix or suffix to existing comps.

Thanks.

D

(CS6)
txuku

rename layer comps

Post by txuku »

Bonjour deluxe

Some variations on the name of the layer comps :

Code: Select all//Nombre de compositions de calque
alert( activeDocument.layerComps.length );


//changer tous les noms des compositions et les numeroter
for (i = 0; i < activeDocument.layerComps.length; i++ )
{
  //alert( activeDocument.layerComps.name );
  // activeDocument.layerComps.name = activeDocument.layerComps.name + "_" + i;
  activeDocument.layerComps.name = "Compo_" + i;
 // alert( activeDocument.layerComps.name );
}


//changer le nom de compo_3
for (i = 0; i < activeDocument.layerComps.length; i++ )
{
  if( activeDocument.layerComps.name=="Compo_3" ) { activeDocument.layerComps.name = "COMPO_3" ; alert( i ); }


//changer le nom de Compo_2 en Change_2 puis le rendre actif
var layerCompRef = activeDocument.layerComps.getByName( "Compo_2" );
layerCompRef.name = "Change_2";
layerCompRef.apply();

What is your exact purpose ?
Daniel_Wall

rename layer comps

Post by Daniel_Wall »

I have a similar need. I'm trying to find a batch renaming tool/script that will rename selected layer comps.
Something similar to this...
http://blog.kam88.com/en/group-layer-re ... cript.html ... cript.html
But for Layer Comps.

I've been trying to reuse code from a layer renaming script but I am quickly realizing that I am in way over my head.

Has anyone built this already???
txuku

rename layer comps

Post by txuku »

Bonjour

I m sorry:
I don t know how to select several LayerComps with script.
Daniel_Wall

rename layer comps

Post by Daniel_Wall »

I think the idea would be that you would need to select the layer comps you want to rename. That way you don't need to do a select all layer comps function.
something like...
app.activeDocument.layerComps.selected

I really am not to sure.
Limey

rename layer comps

Post by Limey »

It's too bad this thread died out without a solution. I am in the market myself for a good Layer Comp renaming script. Even if its just something that would add a suffix to all selected Layer comps.

I have been using the Layer Names Editor by pattesdours. This works really well. It would be awesome if there was something similar for Layer Comps.

Code: Select all//  ==================================================
//  Layer Names Editor
//  by pattesdours
//  ==================================================

#target photoshop
app.bringToFront();

// ensure that there is at least one document open
function docCheck() {
    if (!documents.length) {
        alert('There are no documents open.');
        return; // quit
    } layerNamesEditor();
}

docCheck();

// main script function
function layerNamesEditor()
{
   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];
         if(!win.setsOnly.value){
            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);         
          if(!win.layersOnly.value){
            obj.layerSets[l].name=lname;   
         }//layers Only
            findReplaceLayerName(obj.layerSets[l],Find,Replace);
         }
      }
   }
}

      // build dialog box
      var win = new Window("dialog{text:'Layer Names Editor',bounds:[100,100,510,420], \n" +
      "panel0:Panel{bounds:[210,10,400,120] , text:'Find and Replace' ,properties:{borderStyle:'etched',su1PanelCoordinates:true}, \n" +
         "button0:Button{bounds:[110,70,180,90] , text:'Replace' }, \n" +
         "statictext1:StaticText{bounds:[10,21,80,38] , text:'Find' ,properties:{scrolling:undefined,multiline:false}}, \n" +
         "statictext2:StaticText{bounds:[10,42,80,59] , text:'Replace' ,properties:{scrolling:undefined,multiline:false}}, \n" +
         "edittext0:EditText{bounds:[80,19,170,39] , text:'' ,properties:{multiline:false,noecho:false,readonly:false}}, \n" +
         "edittext1:EditText{bounds:[80,41,170,61] , text:'' ,properties:{multiline:false,noecho:false,readonly:false}}}, \n" +
      "panel1:Panel{bounds:[210,130,400,240] , text:'Remove' ,properties:{borderStyle:'etched',su1PanelCoordinates:true}, \n" +
         "button1:Button{bounds:[110,70,180,90] , text:'Remove' }, \n" +
         "statictext7:StaticText{bounds:[10,20,180,70] , text:'This command removes instances of < copy n>.' ,properties:{scrolling:undefined,multiline:true}}}, \n" +
      "button2:Button{bounds:[220,260,390,300] , text:'OK' }, \n" +
      "panel2:Panel{bounds:[10,200,200,310] , text:'Add Prefix / Suffix' ,properties:{borderStyle:'etched',su1PanelCoordinates:true}, \n" +
         "button3:Button{bounds:[110,70,180,90] , text:'Add' }, \n" +
         "statictext4:StaticText{bounds:[10,21,80,38] , text:'Prefix' ,properties:{scrolling:undefined,multiline:false}}, \n" +
         "statictext5:StaticText{bounds:[10,44,80,61] , text:'Suffix' ,properties:{scrolling:undefined,multiline:false}}, \n" +
         "edittext2:EditText{bounds:[80,19,170,39] , text:'' ,properties:{multiline:false,noecho:false,readonly:false}}, \n" +
         "edittext3:EditText{bounds:[80,41,170,61] , text:'' ,properties:{multiline:false,noecho:false,readonly:false}}}, \n" +
      "layersOnly:RadioButton{bounds:[20,43,170,64] , text:'Layers only' }, \n" +
      "setsOnly:RadioButton{bounds:[20,66,170,87] , text:'Layersets only' }, \n" +
      "both:RadioButton{bounds:[20,20,170,41] , text:'Layers and layersets', properties:{selected:true}}, \n" +
      "panel3:Panel{bounds:[10,100,200,190] , text:'Select Element by Name' ,properties:{borderStyle:'etched',su1PanelCoordinates:true}, \n" +
         "edittext0:EditText{bounds:[10,20,171,40] , text:'' ,properties:{multiline:false,noecho:false,readonly:false}}, \n" +
         "button0:Button{bounds:[110,50,181,71] , text:'Find' }}};");


      win.both.value = true;

      // Find and replace command
      win.panel0.button0.onClick = function() {
         findReplaceLayerName(activeDocument,win.panel0.edittext0.text,win.panel0.edittext1.text);
         alert("Find & Replace operation complete!");
      }

       // Find and replace command (remove ' copy n' text)
      win.panel1.button1.onClick = function() {
      findReplaceLayerName(activeDocument, " copy( \\d+)?$", '')
      alert("Remove operation complete!");
      }
   
       // Add prefix/suffix command
       win.panel2.button3.onClick = function() {
      addPrefixSuffix(activeDocument, win.panel2.edittext2.text, win.panel2.edittext3.text);
      alert("Add Prefix/Suffix operation complete!");
      }

       // Select element by name command
       win.panel3.button0.onClick = function() {
     selectLayerByName(activeDocument, win.panel3.edittext0.text);
      alert("Select Element by Name operation complete!");
      }   

      // show 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);
   }
// Add Prefix/Suffix
function addPrefixSuffix(obj, layerNamePrefix, layerNameSuffix)
{
   
   if(obj.artLayers.length>0)
   {
      for(var z = 0;z<obj.artLayers.length;z++)
      {
         var layer = obj.artLayers[z];
       if(!win.setsOnly.value){
         layer.name = layerNamePrefix + layer.name + layerNameSuffix;
       }
      }
   }
   if(obj.layerSets.length > 0)
   {
      for(var l=0;l<obj.layerSets.length;l++)
      {
         var layer = obj.layerSets[l];
       if(!win.layersOnly.value){
         layer.name = layerNamePrefix + layer.name + layerNameSuffix;
       }
         addPrefixSuffix(obj.layerSets[l], layerNamePrefix, layerNameSuffix);
      }
   }
}

// find layer/layerset and select it
function selectLayerByName (sl_doc, tname)
{
   var dlayers = sl_doc.layers;
   var flAr = [];
   for (var ahk = 0; ahk < dlayers.length; ahk++)
   {
      flAr.push(dlayers[ahk]);
   }
   for (var ahi = 0;ahi < flAr.length;ahi++)
   {
      var tLayer = flAr[ahi];
      if (flAr[ahi].name == tname)
      {
         sl_doc.activeLayer = flAr[ahi];
         return true;
      }
   if (flAr[ahi].typename == 'LayerSet')
   {
      for (var ahj = 0; ahj < flAr[ahi].layers.length; ahj++)
      {
         flAr.push(flAr[ahi].layers[ahj]);
      }
   }
}

return false;
}
}

// run script

All the best,
Limey
pfaffenbichler

rename layer comps

Post by pfaffenbichler »

I am in the market myself for a good Layer Comp renaming script.
So go ahead and give making one a try!

But as txuku hinted at I don’t think one can select several or determine more than one selected* LayerComps with a Script.

*: A work-around for this (deleting, determining which ones are absent, undoing) seems feasible, though if they do not all have different names it might have to be more convoluted.
pfaffenbichler

rename layer comps

Post by pfaffenbichler »

Limey, it seems that, apart from determining the selected LayerComps, such a Script could work fine with DOM code when one applies the name changes to the Layer Comps only after OK-ing the dialog.
Daniel_Wall

rename layer comps

Post by Daniel_Wall »

What about something that would step through all of the layer comps one by one and rename them based on find replace/add/remove/prefix/suffix...?

I know it's possible to export just the selected layer comps, why wouldn't it be possible to rename just the selected layer comps?

I don't know about other people but I seem to have the situation where I'm re-purposing Photoshop documents with a lot of layer comps in them and need to rename all of the layer comps by hand every time. I do this minimum 10 times per week. Usually it's just a suffix character change but it's still a pain.

Has anyone figured this out yet?
pfaffenbichler

rename layer comps

Post by pfaffenbichler »

I think a possible approach to determining the selected Layer Comps has been mentioned previously (deleting the selected Layer Comps with AM code, then determining which ones are missing and afterwards going back in the History) – have you tried pursuing that?