10 Replies Latest reply on Sep 19, 2017 5:03 AM by Vinothmannu

    Increment text value in illustrator

    Vinothmannu Level 1

      Hi All,

       

      i have found the below script for text increment in illustrator this is working fine, but i need the increment value to be variable

       

      If the starting number is 7 and the increment value is 7 and number of steps is 5 then our requirement would be 7,14, 21,28,35.

       

      #target illustrator

       

       

      var selectedItems;

       

      // if a document is open

      if(documents.length >0)

      {

          var idoc = app.activeDocument;  

       

         // if textframes exist in the document

         if(activeDocument.textFrames.length > 0)

         {

            // check to make sure something is selected.

            selectedItems = selection

            if (selectedItems.length > 0)

            {

                  var itemFound = false;

                  for(var i=0; i<selectedItems.length; i++)

                  {

                     if(selectedItems[i].typename == "TextFrame")

                     {

                        itemFound = true;

                        alert ("The following text item is selected: >" + selectedItems[i].contents + "<")

       

                        var startNum = prompt ("Enter a starting number");

                        var incY = prompt ("Enter a Y offset in points (72 points = 1 inch)", "72", "Y Offset");

                        var steps = prompt ("How many steps?");

                        var increment = prompt ("count?"); //Added new

                     

       

                        selectedItems[i].contents = startNum;

                        var myGroup = app.activeDocument.groupItems.add();

                        selectedItems[i].move( myGroup , ElementPlacement.PLACEATEND);

                        selectedItems[i].position = [0,0];

       

                        for(var a=0; a<steps; a++)

                        {               

                            selectedItems[i].duplicate ();

                            selectedItems[i].move( myGroup , ElementPlacement.PLACEATEND);

                            var newNumber = parseFloat(startNum) + (a+1);

                            selectedItems[i].contents = newNumber;

                            var newY = incY / 72;

                            selectedItems[i].position = [0, -((a+1)*incY)];

                         }

       

                     }

                  }

       

                  if(itemFound == false) alert("No text items are selected.");

       

            }

            else

            {

               alert("Nothing is selected, select a text item(s) or a text range.");

            }

       

         }

         else

         {

            alert("Open a document and select 1 or more text items or a text range.");

         }

      }

      else

      {

         alert("Open a document and select 1 or more text items or a text range.");

      }

       

      Regards,

      Vinoth

        • 1. Re: Increment text value in illustrator
          Ten A Adobe Community Professional

          Try to change this line,

          var newNumber = parseFloat(startNum) + (a+1) ;

          to

          var newNumber = parseFloat(startNum) + (a+1) * increment;

          • 2. Re: Increment text value in illustrator
            Vinothmannu Level 1

            Thanks for the update, its working.

             

            selectedItems[i].position = [0,0];

            Instead of [0,0]; position the text has to be retained in the same position where the text is selected, it that is possible.

            Regards,

            Vinoth

            • 3. Re: Increment text value in illustrator
              Vinothmannu Level 1

              Any further updates for the above.

              • 4. Re: Increment text value in illustrator
                Ten A Adobe Community Professional

                You can reference selected objects position, duplicate it and move it. reference below

                 

                var pos = selectedItems.position;

                for(.....){

                   SelectedItems.position = [pos[0], pos[1]-((a+1)*incY)]

                }

                • 5. Re: Increment text value in illustrator
                  Vinothmannu Level 1

                  Thanks for update Ten,

                   

                  I am not so good in scripting, could you please update this for me.

                   

                  Regards,

                  Vinoth

                  • 6. Re: Increment text value in illustrator
                    Ten A Adobe Community Professional

                    Try it

                    #target illustrator
                    
                    
                    var selectedItems, pos;
                    
                    // if a document is open
                    if(documents.length >0)
                    {
                        var idoc = app.activeDocument;  
                    
                       // if textframes exist in the document
                       if(activeDocument.textFrames.length > 0)
                       {
                          // check to make sure something is selected.
                          selectedItems = selection
                          if (selectedItems.length > 0)
                          {
                                var itemFound = false;
                                for(var i=0; i<selectedItems.length; i++)
                                {
                                   if(selectedItems[i].typename == "TextFrame")
                                   {
                                      itemFound = true;
                                      alert ("The following text item is selected: >" + selectedItems[i].contents + "<")
                    
                                      var startNum = prompt ("Enter a starting number");
                                      var incY = prompt ("Enter a Y offset in points (72 points = 1 inch)", "72", "Y Offset");
                                      var steps = prompt ("How many steps?");
                                      var increment = prompt ("count?"); //Added new
                                   
                    
                                      selectedItems[i].contents = startNum;
                                      var myGroup = app.activeDocument.groupItems.add();
                                      selectedItems[i].move( myGroup , ElementPlacement.PLACEATEND);
                                      //selectedItems[i].position = [0,0]; //ignore this line.
                                      pos = selectedItems[i].position; //get original objects position(x,y cordinates) in Array.
                    
                                      for(var a=0; a<steps; a++)
                                      { 
                                          selectedItems[i].duplicate ();
                                          selectedItems[i].move( myGroup , ElementPlacement.PLACEATEND);
                                          var newNumber = parseFloat(startNum) + (a+1);
                                          selectedItems[i].contents = newNumber;
                                          var newY = incY / 72;
                                          selectedItems[i].position =  [pos[0], pos[1]-((a+1)*incY)]; //
                                       }
                    
                                   }
                                }
                    
                                if(itemFound == false) alert("No text items are selected.");
                    
                          }
                          else
                          {
                             alert("Nothing is selected, select a text item(s) or a text range.");
                          }
                    
                       }
                       else
                       {
                          alert("Open a document and select 1 or more text items or a text range.");
                       }
                    }
                    else
                    {
                       alert("Open a document and select 1 or more text items or a text range.");
                    }
                    

                     

                     

                    However, I recommend you to learn around Scripting. Actually, We don't have enough time to write for someone and you don't always get it.

                    • 7. Re: Increment text value in illustrator
                      Vinothmannu Level 1

                      Thanks a lot. surely i am in the learning process.

                      • 8. Re: Increment text value in illustrator
                        Vinothmannu Level 1

                        In the decimal value for example 0.5 , 1.0 , 1.5 , 2.0. etc the value are found as 0.5 , 1 , 1.5 , 2. the value of 0 is missing after round value.

                        • 9. Re: Increment text value in illustrator
                          Ten A Adobe Community Professional

                          You can use toFix method like below.

                           

                          var pai = 3.1415926535897932;

                          var fixedValue = pai.toFix(1);

                          1 person found this helpful
                          • 10. Re: Increment text value in illustrator
                            Vinothmannu Level 1

                            Great i solved this issue, Thanks