25 Replies Latest reply on May 25, 2018 6:27 AM by KatrodiyaDhaval

    Count text letters & set font size

    KatrodiyaDhaval Level 1

      hi, i am using photoshop variables to import csv file into photoshop.

       

      the csv file contains three type of text lines.
      1. 50 letter text line
      2. 75 letter text line
      3. 100 letter text line

       

      i dont no anything about scripting. so i need help
      i want a script that do following things.

       

      for example, if text line contains "50 letters" set font size to "140 px".
      if text line contains "75 letters" set font size to "110 px"
      if text line contains "100 letters" set font size to "80 px"

        • 1. Re: Count text letters & set font size
          JJMack Most Valuable Participant

          You give no information about the Photoshop document. Canvas size pixel wide pixel high print resolution, Text also has a font  all are not the same size  characters vary in size in some and are fixed size in other.  Text is very complex.  Fully expand some actions that add text and look at the expended text steps. 

          • 2. Re: Count text letters & set font size
            Chuck Uebele Adobe Community Professional & MVP

            You need to import your cvs file into a string variable and break it into lines using the split command:

             

            var doc = activeDocument;
            var cvsLines = myCVSstring.split('\n');
            for(var i=0;i<cvsLines.length;i++){
                 //put code here to make a text layer
                 var txtLayer = doc.activeLayer;//need code to create this text layer.
                 if(cvsLines[i].length<60){txtLayer.size = 140}
                 else if(cvsLines[i].length<85){txtLayer.size = 110}
                 else{txtLayer.size = 80}
                 }
            
            • 3. Re: Count text letters & set font size
              KatrodiyaDhaval Level 1

              i have excel file

              & i  am using photoshop variables feature to import this data.

               

              I am using variables visibility feature to switch between this three layer

               

              I am exporting this files via "Data sets as files" feature, which creates 200+ psd files.

               

              i want a script that do following things.

               

              for example, if  contains "25 to 50 letters" set font size to "140 px".
              if  contains "51 to 75 letters" set font size to "110 px"
              if contains "76 to 100 letters" set font size to "80 px"

               

              then i will create an action like this one.

              Untitled.png

              & apply them to all 200+ psd files via batch function.

              • 4. Re: Count text letters & set font size
                KatrodiyaDhaval Level 1

                thank you for a script sir,

                i have save your code as jsx file.

                but when i load your script, it gives me following error

                 

                Untitled.png

                 

                one again, thanks for your help

                • 5. Re: Count text letters & set font size
                  Chuck Uebele Adobe Community Professional & MVP

                  My code is incomplete. It just shows how to split the text in a CVS string into lines and get their length, and then apply a size to an existing text layer. You still need to write the code to open the cvs file, and put its contents into a string. You also need to write the code to create the text layers, if they don't already exist.

                  Will have to look at your files later, when I'm at my computer.

                  • 6. Re: Count text letters & set font size
                    Chuck Uebele Adobe Community Professional & MVP

                    Try this after you import your data sets.

                     

                    #target photoshop
                    var doc = activeDocument;
                    var txtLayer1 = doc.layers.getByName('Text1');
                    var txtLayer2 = doc.layers.getByName('Text2');
                    var txtLayer3 = doc.layers.getByName('Text3');
                    
                    setSize (txtLayer1)
                    setSize (txtLayer2)
                    setSize (txtLayer2)
                    
                    function setSize(layer){
                        doc.activeLayer = layer;
                        try{
                            if(layer.textItem.contents.length<51){layer.textItem.size = 140}
                            else if(layer.textItem.contents.length<76){layer.textItem.size = 110}
                            else{layer.textItem.size = 80}
                            }
                        catch(e){}
                        }
                    
                    • 7. Re: Count text letters & set font size
                      KatrodiyaDhaval Level 1

                      Thank you so much, your code work perfectly.

                      However their is one problem

                       

                      When i export files via "Data sets as files", it creates 200+ files

                      Each file will have 1 active text layer & 2 hidden text layers.

                       

                      For example, the 3rd dataset file have

                      Text1 & Text2 layer are hidden &

                      Text3 layer is visible

                       

                      This script working fine but it is making all three layers visible

                      • 8. Re: Count text letters & set font size
                        Chuck Uebele Adobe Community Professional & MVP

                        This should keep the visibility the same:

                         

                        #target photoshop
                        var doc = activeDocument;
                        var txtLayer1 = doc.layers.getByName('Text1');
                        var txtLayer2 = doc.layers.getByName('Text2');
                        var txtLayer3 = doc.layers.getByName('Text3');
                        
                        setSize (txtLayer1)
                        setSize (txtLayer2)
                        setSize (txtLayer2)
                        
                        function setSize(layer){
                            var vis = layer.visible
                            doc.activeLayer = layer;
                            try{
                                if(layer.textItem.contents.length<51){layer.textItem.size = 140}
                                else if(layer.textItem.contents.length<76){layer.textItem.size = 110}
                                else{layer.textItem.size = 80}
                                }
                            catch(e){}
                            layer.visible = vis
                            }
                        
                        1 person found this helpful
                        • 9. Re: Count text letters & set font size
                          KatrodiyaDhaval Level 1

                          All problems are solved, thnx you one again.

                          You made my day, god bless you

                          • 10. Re: Count text letters & set font size
                            dominique filiol Level 1

                            Bonjour

                             

                            Why the same problem on two forums ?

                             

                             

                            Here

                            • 11. Re: Count text letters & set font size
                              KatrodiyaDhaval Level 1

                              hi, i got one more problem after using your script.

                              The problem is with "Line Spacing"

                              Untitled.png

                              if line contains only few words, above problem occurs.

                              so if u can  change your script to do following things, it will very helpfull

                               

                              for example, if Line contains "25 to 50 letters" set font size to "140 px".& Set Line Spacing to "250"

                              if Line contains "51 to 75 letters" set font size to "110 px" & Set Line Spacing to "170"

                              if Line contains "76 to 100 letters" set font size to "80 px" & Set Line Spacing to "130"

                              • 12. Re: Count text letters & set font size
                                KatrodiyaDhaval Level 1

                                & if can you do following thing with your script

                                 

                                align Text layer center to the canvas

                                • 13. Re: Count text letters & set font size
                                  Chuck Uebele Adobe Community Professional & MVP

                                  You can try this:

                                   

                                  #target photoshop
                                  var doc = activeDocument;
                                  var txtLayer1 = doc.layers.getByName('Text1');
                                  var txtLayer2 = doc.layers.getByName('Text2');
                                  var txtLayer3 = doc.layers.getByName('Text3');
                                  
                                  setSize (txtLayer1)
                                  setSize (txtLayer2)
                                  setSize (txtLayer2)
                                  
                                  function setSize(layer){
                                      var vis = layer.visible
                                      doc.activeLayer = layer;
                                      try{
                                          if(layer.textItem.contents.length<51){
                                              layer.textItem.size = 140
                                              layer.textItem.leading = 140    
                                              alignLayer ();
                                              }
                                          else if(layer.textItem.contents.length<76){
                                              layer.textItem.size = 110
                                              layer.textItem.leading = 110
                                              alignLayer ();
                                              }
                                          else{
                                              layer.textItem.size = 80
                                              layer.textItem.leading = 80
                                              alignLayer ();
                                              }
                                          }
                                      catch(e){}
                                      layer.visible = vis
                                      }
                                  
                                  function alignLayer(){
                                      var idslct = charIDToTypeID( "slct" );
                                          var desc2 = new ActionDescriptor();
                                          var idnull = charIDToTypeID( "null" );
                                              var ref1 = new ActionReference();
                                              var idLyr = charIDToTypeID( "Lyr " );
                                              ref1.putName( idLyr, "Background" );
                                          desc2.putReference( idnull, ref1 );
                                          var idselectionModifier = stringIDToTypeID( "selectionModifier" );
                                          var idselectionModifierType = stringIDToTypeID( "selectionModifierType" );
                                          var idaddToSelection = stringIDToTypeID( "addToSelection" );
                                          desc2.putEnumerated( idselectionModifier, idselectionModifierType, idaddToSelection );
                                          var idMkVs = charIDToTypeID( "MkVs" );
                                          desc2.putBoolean( idMkVs, false );
                                          var idLyrI = charIDToTypeID( "LyrI" );
                                              var list1 = new ActionList();
                                              list1.putInteger( 1 );
                                              list1.putInteger( 2 );
                                          desc2.putList( idLyrI, list1 );
                                      executeAction( idslct, desc2, DialogModes.NO );
                                  
                                      var idAlgn = charIDToTypeID( "Algn" );
                                          var desc4 = new ActionDescriptor();
                                          var idnull = charIDToTypeID( "null" );
                                              var ref2 = new ActionReference();
                                              var idLyr = charIDToTypeID( "Lyr " );
                                              var idOrdn = charIDToTypeID( "Ordn" );
                                              var idTrgt = charIDToTypeID( "Trgt" );
                                              ref2.putEnumerated( idLyr, idOrdn, idTrgt );
                                          desc4.putReference( idnull, ref2 );
                                          var idUsng = charIDToTypeID( "Usng" );
                                          var idADSt = charIDToTypeID( "ADSt" );
                                          var idAdCH = charIDToTypeID( "AdCH" );
                                          desc4.putEnumerated( idUsng, idADSt, idAdCH );
                                      executeAction( idAlgn, desc4, DialogModes.NO );
                                      }
                                  
                                  1 person found this helpful
                                  • 14. Re: Count text letters & set font size
                                    KatrodiyaDhaval Level 1

                                    hi, thank you for your response.

                                    your script works very well.

                                    however their is one problem with align.

                                     

                                    the script is aligning, Text layer center to the canvas horizontally not vertically.

                                     

                                    i mean script is centering textlayer horizontally, but it is not centering textlayer vertically.

                                     

                                    thank you ones again for your help

                                    • 15. Re: Count text letters & set font size
                                      Chuck Uebele Adobe Community Professional & MVP

                                      Do you want it to center both horizontally and vertically? Or just vertically?

                                      • 16. Re: Count text letters & set font size
                                        Chuck Uebele Adobe Community Professional & MVP

                                        This script will align in both directions.

                                         

                                        #target photoshop
                                        var doc = activeDocument;
                                        var txtLayer1 = doc.layers.getByName('Text1');
                                        var txtLayer2 = doc.layers.getByName('Text2');
                                        var txtLayer3 = doc.layers.getByName('Text3');
                                        
                                        setSize (txtLayer1)
                                        setSize (txtLayer2)
                                        setSize (txtLayer2)
                                        
                                        function setSize(layer){
                                            var vis = layer.visible
                                            doc.activeLayer = layer;
                                            try{
                                                if(layer.textItem.contents.length<51){
                                                    layer.textItem.size = 140
                                                    layer.textItem.leading = 140    
                                                    alignLayer ();
                                                    }
                                                else if(layer.textItem.contents.length<76){
                                                    layer.textItem.size = 110
                                                    layer.textItem.leading = 110
                                                    alignLayer ();
                                                    }
                                                else{
                                                    layer.textItem.size = 80
                                                    layer.textItem.leading = 80
                                                    alignLayer ();
                                                    }
                                                }
                                            catch(e){}
                                            layer.visible = vis
                                            }
                                        
                                        function alignLayer(){
                                            var idslct = charIDToTypeID( "slct" );
                                                var desc2 = new ActionDescriptor();
                                                var idnull = charIDToTypeID( "null" );
                                                    var ref1 = new ActionReference();
                                                    var idLyr = charIDToTypeID( "Lyr " );
                                                    ref1.putName( idLyr, "Background" );
                                                desc2.putReference( idnull, ref1 );
                                                var idselectionModifier = stringIDToTypeID( "selectionModifier" );
                                                var idselectionModifierType = stringIDToTypeID( "selectionModifierType" );
                                                var idaddToSelection = stringIDToTypeID( "addToSelection" );
                                                desc2.putEnumerated( idselectionModifier, idselectionModifierType, idaddToSelection );
                                                var idMkVs = charIDToTypeID( "MkVs" );
                                                desc2.putBoolean( idMkVs, false );
                                                var idLyrI = charIDToTypeID( "LyrI" );
                                                    var list1 = new ActionList();
                                                    list1.putInteger( 1 );
                                                    list1.putInteger( 2 );
                                                desc2.putList( idLyrI, list1 );
                                            executeAction( idslct, desc2, DialogModes.NO );
                                        
                                            var idAlgn = charIDToTypeID( "Algn" );  //Align Horizontally
                                                var desc4 = new ActionDescriptor();
                                                var idnull = charIDToTypeID( "null" );
                                                    var ref2 = new ActionReference();
                                                    var idLyr = charIDToTypeID( "Lyr " );
                                                    var idOrdn = charIDToTypeID( "Ordn" );
                                                    var idTrgt = charIDToTypeID( "Trgt" );
                                                    ref2.putEnumerated( idLyr, idOrdn, idTrgt );
                                                desc4.putReference( idnull, ref2 );
                                                var idUsng = charIDToTypeID( "Usng" );
                                                var idADSt = charIDToTypeID( "ADSt" );
                                                var idAdCH = charIDToTypeID( "AdCH" );
                                                desc4.putEnumerated( idUsng, idADSt, idAdCH );
                                            executeAction( idAlgn, desc4, DialogModes.NO );
                                            
                                            var idAlgn = charIDToTypeID( "Algn" );  //Align Vertically
                                                var desc3 = new ActionDescriptor();
                                                var idnull = charIDToTypeID( "null" );
                                                    var ref1 = new ActionReference();
                                                    var idLyr = charIDToTypeID( "Lyr " );
                                                    var idOrdn = charIDToTypeID( "Ordn" );
                                                    var idTrgt = charIDToTypeID( "Trgt" );
                                                    ref1.putEnumerated( idLyr, idOrdn, idTrgt );
                                                desc3.putReference( idnull, ref1 );
                                                var idUsng = charIDToTypeID( "Usng" );
                                                var idADSt = charIDToTypeID( "ADSt" );
                                                var idAdCV = charIDToTypeID( "AdCV" );
                                                desc3.putEnumerated( idUsng, idADSt, idAdCV );
                                            executeAction( idAlgn, desc3, DialogModes.NO );    
                                            }
                                        
                                        1 person found this helpful
                                        • 17. Re: Count text letters & set font size
                                          KatrodiyaDhaval Level 1

                                          thank you so so much.

                                          • 18. Re: Count text letters & set font size
                                            KatrodiyaDhaval Level 1

                                            hello Chuck Uebele,

                                            i got one problem,

                                             

                                            Untitled.png

                                            as you can see in above picture, some paragraph fit perfectly in textbox

                                             

                                            2Untitled.png

                                            & some does't,

                                            i want the above image should look like this.

                                            5Untitled.png

                                             

                                            i want a script that can do following things :

                                            if paragraph have 5 lines (like 1st image) then its ok ,do nothing , but

                                             

                                            if paragraph have 4 lines (like 2nd image) then change layer's vertical location (Y: position)

                                            ex. if layer's position are like this =

                                            x: 900px

                                            y: 500px

                                            change its y: position to 520px.

                                            so, 4 line paragraph look like 3rd image.

                                             

                                            note: i have tried to center text layer both horizontally & vertically. it is not working. i mean i am not able to get result like 3rd image.

                                             

                                            hope! you can help me.

                                            • 19. Re: Count text letters & set font size
                                              Chuck Uebele Adobe Community Professional & MVP

                                              That's a hard one, as if you use a text box, I don't know of anyway to center just the text within the box. I've always used hard returns for new lines, when I need to center text like that.

                                              • 20. Re: Count text letters & set font size
                                                r-bin Level 4

                                                https://forums.adobe.com/people/Chuck+Uebele

                                                 

                                                That's a hard one, as if you use a text box, I don't know of anyway to center just the text within the box. I've always used hard returns for new lines, when I need to center text like that.

                                                To fully align the text in textbox, you can try this script

                                                 

                                                // Horizontal alignment
                                                activeDocument.activeLayer.textItem.justification = Justification.CENTER;
                                                
                                                // Vertical alignment
                                                var r = new ActionReference();  
                                                r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("textKey"));      
                                                r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));  
                                                
                                                var yy = 1;  
                                                try { yy = executeActionGet(r).getObjectValue(stringIDToTypeID("textKey")).getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("yy")); } catch(e) {}  
                                                var box_height = executeActionGet(r).getObjectValue(stringIDToTypeID("textKey")).getList(stringIDToTypeID("textShape")).getObjectValue(0).getObjectValue(stringIDToTypeID("bounds")).getDouble(stringIDToTypeID("bottom"));  
                                                
                                                box_height = box_height*yy;  
                                                
                                                app.preferences.rulerUnits = Units.POINTS;
                                                
                                                var shift = (box_height - (activeDocument.activeLayer.bounds[3].value - activeDocument.activeLayer.bounds[1].value))/2;
                                                
                                                activeDocument.activeLayer.textItem.baselineShift = -shift;
                                                
                                                1 person found this helpful
                                                • 21. Re: Count text letters & set font size
                                                  KatrodiyaDhaval Level 1

                                                  thanx for your reply,

                                                   

                                                  5Untitled.png

                                                  your script works. for 4line paragraph

                                                  but it didn't work for 5line paragraph

                                                  6Untitled.png

                                                   

                                                  any idea ! what to do?

                                                  • 22. Re: Count text letters & set font size
                                                    r-bin Level 4

                                                    Give please a simple file with an example of text that is not aligning

                                                    • 24. Re: Count text letters & set font size
                                                      r-bin Level 4

                                                      OK. Your file is not compatible with CS6, but nevertheless here's a new script that works in CC2018.

                                                       

                                                      // Horizontal alignment  
                                                      activeDocument.activeLayer.textItem.justification = Justification.CENTER;  
                                                        
                                                      // Vertical alignment  
                                                      var r = new ActionReference();    
                                                      r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("textKey"));        
                                                      r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));    
                                                        
                                                      var yy = 1;    
                                                      try { yy = executeActionGet(r).getObjectValue(stringIDToTypeID("textKey")).getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("yy")); } catch(e) {}    
                                                      
                                                      var d = executeActionGet(r).getObjectValue(stringIDToTypeID("textKey")).getList(stringIDToTypeID("textShape")).getObjectValue(0).getObjectValue(stringIDToTypeID("bounds"));    
                                                      
                                                      var box_height = d.getDouble(stringIDToTypeID("bottom")) - d.getDouble(stringIDToTypeID("top"));    
                                                        
                                                      box_height = box_height*yy;    
                                                        
                                                      app.preferences.rulerUnits = Units.POINTS;  
                                                        
                                                      var shift = (box_height - (activeDocument.activeLayer.bounds[3].value - activeDocument.activeLayer.bounds[1].value))/2;  
                                                        
                                                      activeDocument.activeLayer.textItem.baselineShift = -shift;  
                                                      
                                                      2 people found this helpful
                                                      • 25. Re: Count text letters & set font size
                                                        KatrodiyaDhaval Level 1

                                                        Wow! your script, work perfecty.

                                                        thank you so much.