16 Replies Latest reply on Jan 5, 2017 2:52 AM by toddr44415872

    Rename layer if it contains text

    toddr44415872

      Hope someone can help my with there two scripts I started:

      #target photoshop

      var Name=activeDocument.name

      if (Name.indexOf(".") != -1 ) Name=Name.substr(0,Name.lastIndexOf("."));

      activeDocument.activeLayer.name="115 x 115 "+ Name +"-115.jpg";

       

      #target photoshop

      var Name=activeDocument.name

      if (Name.indexOf(".") != -1 ) Name=Name.substr(0,Name.lastIndexOf("."));

      activeDocument.activeLayer.name="1366 x 1366 "+ Name +"-1366.jpg";

       

      How can I merge them together and use an if then statement so that IF any layer name contains 115 or 1366 THEN the layer will be renamed as shown in the above script?  I plan on attaching this script to the save command to get it to execute.

      Thank you!

        • 1. Re: Rename layer if it contains text
          Kukurykus Adobe Community Professional

          For the beginning:

           

          #target photoshop
          
          if (nme = (aDN = (aD = activeDocument).name).match(/.*115.*(?=\.)/)) {
            aD.activeLayer.name ="115 x 115 " + nme +"-115.jpg";
          }
          
          if (nme = aDN.match(/.*1366.*(?=\.)/)) {
            aD.activeLayer.name ="1366 x 1366 " + nme +"-1366.jpg";
          }
          
          • 2. Re: Rename layer if it contains text
            toddr44415872 Level 1

            Thank you!  I have tried this script and I am not getting anything to happen.  Sorry to bother but should there be a loop somewhere that checks all of the layer group names?  Thanks again.

            • 3. Re: Rename layer if it contains text
              pixxxel schubser MVP & Adobe Community Professional

              Please show us at first one sample file (or screenshot) with visible layers palette and some layer names. And one example with layer name before and one after running the script.

              • 4. Re: Rename layer if it contains text
                Kukurykus Adobe Community Professional

                If you just create a new document with some name like "115" then this script won't be working, but when you save that new document with name for ex, aaaaa115aaaaa.jpg and then run the script it'll change name of active Layer to "115 x 115 115-115.jpg".

                 

                It's not so difficult to write more advanced script to loop over all layers in a document. It has sense as far there is only one layer beetwen others with name cointaining 115 or 1366 number. So if you don't plan to have one document with at least 2 layers where name of one of them contain 115 and second 1366 I can write a script it's going to loop all layers searching for that one. The question is are you going to have there also groups, i mean layerSets in your document, or only layers. And if layerSets also will there searched for layer inside one of them, or there always layer with 115 or  1366 name having no parent (so no beeing in any group)?

                • 5. Re: Rename layer if it contains text
                  toddr44415872 Level 1

                  Thank you! Please see current workflow video attached.  Problem is that if filename changes, layer names won't change unless I remember to run the scripts.  Dropbox - 2017-01-04_13-38-24.mp4

                  • 6. Re: Rename layer if it contains text
                    pixxxel schubser MVP & Adobe Community Professional

                    Thx for the video. But I can't see enough details of the image name and the layers names

                    IdidntSee.png

                    • 7. Re: Rename layer if it contains text
                      Kukurykus Adobe Community Professional

                      It's still not enough clear to me, so let me ask another qestion.

                       

                       

                      Let's say you got some file contianing groups and layers. Only groups (not layers as I thought before, because video shows differently) are named 115 or 1366, right?

                       

                      The file with these groups and layers is named somehow, but meantime for some reason you change its name and you want that at same time or after running specific script only groups with similar name to document name were changed.

                       

                      For example if there are 115 group and 1366 group and you changed name of your file to name cointaining 115 you want all groups with name 115 get changed to appriopae name while those with 1366 left untouched, right?

                       

                      When I'm home today I download this video because I can't see it watching it live well, anyway the main qestion is what I described above, so please be more precise because in first post you didn't mention anything about looping and groups, and later you do, the best will be if you point evertying, step by step, thx)

                      • 9. Re: Rename layer if it contains text
                        Kukurykus Adobe Community Professional

                        It's not about video, I saw enough before with my inner focus I hope I don't have to send you to mind reader, I even don't know any who can so, so be back when you finish better description and I promise to write some script back

                        • 10. Re: Rename layer if it contains text
                          toddr44415872 Level 1

                          Sorry for the confusion!

                          Current workflow:

                          1. Click on layer group to be renamed

                          2. Go to File>Scripts and choose 115 Layer Rename.

                          3. This script we be executed:

                          #target photoshop

                          var Name=activeDocument.name

                          if (Name.indexOf(".") != -1 ) Name=Name.substr(0,Name.lastIndexOf("."));

                          activeDocument.activeLayer.name="115 x 115 "+ Name +"-115.jpg";

                           

                          Preferred workflow:

                          1. Go to File>Scripts and choose "Layer Rename"

                          2. Script will search thru all layer group names.

                          a. If it contains any text with 115 then this script is exucuted:

                          #target photoshop

                          var Name=activeDocument.name

                          if (Name.indexOf(".") != -1 ) Name=Name.substr(0,Name.lastIndexOf("."));

                          activeDocument.activeLayer.name="115 x 115 "+ Name +"-115.jpg";

                           

                          b. If it contains any text with 1366 then this script is exucuted:

                          #target photoshop

                          var Name=activeDocument.name

                          if (Name.indexOf(".") != -1 ) Name=Name.substr(0,Name.lastIndexOf("."));

                          activeDocument.activeLayer.name="1366 x 1366 "+ Name +"-1366.jpg";

                           

                          Hope this is clear!

                          • 11. Re: Rename layer if it contains text
                            Kukurykus Adobe Community Professional

                            Now I won't write it - I'm close to leave work, but in 1,5 hour why not - it's enough clear now.

                            • 12. Re: Rename layer if it contains text
                              JavierAroche Level 3

                              Hey toddr44415872, try this:

                               

                              var doc = app.activeDocument;
                              var Name = doc.name;
                              
                              if( Name.indexOf('.') != -1 ) {
                                   Name = Name.substr( 0, Name.lastIndexOf('.') );
                              }
                              
                              var docLayerSets = getLayerSets( doc.layerSets, [] );
                              
                              for( var ii = 0; ii < docLayerSets.length; ii++ ) {
                                   var layerName = docLayerSets[ii].name;
                              
                                   if( layerName.indexOf('115') != -1 ) {
                                        docLayerSets[ii].name = '115 x 115 ' + Name + '-115.jpg';
                                   } else if ( layerName.indexOf('1366') != -1 ) {
                                        docLayerSets[ii].name = '1366 x 1366 ' + Name + '-1366.jpg';
                                   }
                              }
                              
                              // getLayerSets - recursive function to get all layer sets in current document
                              function getLayerSets( docLayers, sets ) {
                                   for( var i = 0; i < docLayers.length; i++ ) {
                                        if( docLayers[i].typename == 'LayerSet' ) {
                                             sets.push( docLayers[i] );
                                             sets = getLayerSets( docLayers[i].layers, sets );
                                        }
                                   }
                              
                                   return sets;
                              }
                              

                               

                              If you have a lot of layers in your documents, you could improve performance by using ActionManager code. If you only have a few, this should be fine.

                               

                              Hope it helps!

                              2 people found this helpful
                              • 13. Re: Rename layer if it contains text
                                pixxxel schubser MVP & Adobe Community Professional

                                Hi JavierAroche,

                                nice try.

                                 

                                 

                                Do you really need the two if clauses?

                                How about to replace line 13 -17 in your code with

                                var checkpart; if ((checkpart = layerName.match(/115|1366/)) != null)
                                {docLayerSets[ii].name = checkpart[0] + ' x ' + checkpart[0] + ' ' + Name + '-' + checkpart[0] + '.jpg';}
                                
                                • 14. Re: Rename layer if it contains text
                                  JavierAroche Level 3

                                  Hey pixxxel schubser, nope you don't necessarily need both if clauses. Luckily, there's a million ways to script this. I wrote this using Todd's logic, so he can follow along and see where I'm using his code. I also did it so he could understand how to add another variable, if he ever needs to. I know that helped me when I started learning JavaScript

                                   

                                  Also, he asked about how to set an if statement, so I thought it'd be useful to show how to keep stacking them in pretty basic JavaScript. If Todd knows more than basic scripting, then he'll be able to improve the code without a problem.

                                   

                                  Btw, I just tested both options (double if / regex match) and they both take around 12.2 seconds on a document with 70 groups. My code just looks a little longer because I like to have separate lines of code for variables, if's, functions, etc... but you could just have all ifs in 1 line

                                   

                                  Like I said, if you have more complicated documents with a lot of groups, you might want to try doing it with ActionManager code.

                                  • 15. Re: Rename layer if it contains text
                                    Kukurykus Adobe Community Professional

                                    Just shorter version without hard-level recursion, but simple loop on Action Manager code:

                                    function sze() {
                                      if ((dln = (aL = (aD = activeDocument).activeLayer).name).match(/115/)) {
                                      aL.name ="115 x 115 " + aD.name.match(/.*(?=\.)/ )+"-115.jpg";
                                      }
                                      if (nme = dln.match(/1366/)) {
                                      aL.name ="1366 x 1366 " + aD.name.match(/.*(?=\.)/ )+"-1366.jpg";
                                      }
                                    }
                                    
                                    function cTT(v) {return charIDToTypeID(v)}; function sTT(v) {return stringIDToTypeID(v)}
                                    
                                    for (i = 1;; i++) {
                                      (ref1 = new ActionReference()).putIndex(sTT('layer'), i),
                                      (dsc1 = new ActionDescriptor()).putReference(sTT('null'), ref1)
                                      dsc1.putBoolean(sTT('makeVisible'), false)
                                      try{executeAction(sTT('select'), dsc1, DialogModes.NO)} catch (err) {break}
                                      if (String(activeDocument.activeLayer).slice(1, 9) == 'LayerSet') sze()
                                    }
                                    
                                    1 person found this helpful
                                    • 16. Re: Rename layer if it contains text
                                      toddr44415872 Level 1

                                      Thank you everyone!!