8 Replies Latest reply on Dec 6, 2017 12:08 PM by ti46978409

    change path/vector lineweights using a loop

    ti46978409

      Hi,

       

      I am trying to change the lineweight using loops so that vectors in each layer is subsequently thinner by a certain amount. Here is a sample of the code that I am working with. Here, I am prompting the user to input a starting lineweight, and then using that for the first layer, and the subsequent layers are multiplied by a factor of 0.125.

       

      So far, it only iterates it twice (there are more than two layers), but all the lineweights are all same value.

      ///////////////////

      var vectors = doc.pathItems;

                          while (isNaN(stroke) == true)   {

                              var stroke = Number(prompt(doc.layers[i].name, "Enter lineweight: "));

                          }

                          for (var j = 0; j < vectors.length; j++)   {

                          vectors[j].selected = false;

                          vectors[j].fillColor = replaceColor;

                          vectors[j].strokeWidth = stroke * 0.125;

                          }

        • 1. Re: change path/vector lineweights using a loop
          Ray Yorkshire Adobe Community Professional

          Maybe try asking in  the scripting forum

           

          illustrator-scripting

          • 2. Re: change path/vector lineweights using a loop
            CarlosCanto Adobe Community Professional & MVP

            you're assigning the same value to all pathItems,

                stroke * 0.125

             

            you have to decrease the value of stroke on each pass.

                stroke -= 0.125 (same as stroke = stroke - 0.125)

             

            var doc = app.activeDocument;
            var vectors = doc.pathItems;
            var stroke = undefined;
            
            
            while (isNaN(stroke) == true) {
                var stroke = Number(prompt('Prompt', "Enter lineweight: "));
            }
            
            
            vectors[0].strokeWidth = stroke; // starting line weight
            
            
            for (var j = 1; j < vectors.length; j++) {
                //vectors[j].selected = false;
                //vectors[j].fillColor = replaceColor;
                vectors[j].strokeWidth = stroke -= 0.125; // 2nd path to last, reduce weight by 0.125
            }
            
            1 person found this helpful
            • 3. Re: change path/vector lineweights using a loop
              ti46978409 Level 1

              Hi,

              I nested this under another loop (for layers), and was hoping one lineweight would apply to all the vectors (pathitems) in each of the layers. What I set up here only loops through few vectors in the first layer:

               

              #target illustrator

              function main()   {

                  if (app.documents.length > 0)   {

                  var doc = app.activeDocument; 

                  var vectors = doc.pathItems; 

                  var stroke = undefined; 

                  var totallayers = doc.layers.length;

                   

                  while (isNaN(stroke) == true) { 

                      var stroke = Number(prompt('Prompt', "Enter lineweight: ")); 

                  } 

               

               

                  for (var i = 0; i < totallayers; i++)   {

                      var currentLayer = doc.layers[i];

                      while (isNaN(stroke) == true)   {

                          stroke = Number(prompt('Prompt', "Enter lineweight: "));

                      }

                      // Sets initial layer's lineweight to the entered stroke

                      doc.layers[0].vectors.strokeWidth = stroke; // starting line weight

                      for (var j = 0; j < vectors.length; j++)   {

                      doc.layers[0].vectors[j].strokeWidth = stroke;

                      currentLayer.vectors[j].strokeWidth = stroke*=0.125;

                      }

                  }

                  }

              }

              • 4. Re: change path/vector lineweights using a loop
                Silly-V Adobe Community Professional
                What I set up here only loops through few vectors in the first layer:

                 

                        doc.layers[0].vectors[j].strokeWidth = stroke;

                        currentLayer.vectors[j].strokeWidth = stroke*=0.125;

                 

                You always are going to doc.layers[0].vectors[j], maybe this needs to be a removed line.

                • 5. Re: change path/vector lineweights using a loop
                  ti46978409 Level 1

                  I removed it but didn't change anything. Here is what I have now:

                  //////////////////////////////////////////////////////////////////////////////////////////

                  #target illustrator

                  function main()   {

                      if (app.documents.length > 0)   {

                      var doc = app.activeDocument; 

                      var vectors = doc.pathItems; 

                      var stroke = undefined; 

                      var totallayers = doc.layers.length;

                       

                      while (isNaN(stroke) == true) {

                          stroke = Number(prompt('Prompt', "Enter lineweight: ")); 

                      } 

                          vectors[0].strokeWidth = stroke;

                          for (var i = 0; i < totallayers; i++)   {

                              for (var j = 0; j < vectors.length; j++)   {

                              vectors[j].strokeWidth = stroke;

                              }

                              stroke -= 0.125;

                          }

                      }

                  }

                  main();

                  //////////////////////////////////////////////////////////////////////////////////////////

                  • 6. Re: change path/vector lineweights using a loop
                    Silly-V Adobe Community Professional

                    Well what is your vectors[j],  where does it come from, where should it come from, and where should that come from?

                    Right now your vectors is doc.Path items, so it's doing a loop for every layer on all the document's pathItems, and changes all of them to the new stroke variable.

                    To fix it, you'll have to change the vectors to something like totalLayers[i].pathItems.

                    1 person found this helpful
                    • 7. Re: change path/vector lineweights using a loop
                      ti46978409 Level 1

                      Thanks! It worked

                      • 8. Re: change path/vector lineweights using a loop
                        ti46978409 Level 1

                        Here is what I ended up doing:

                         

                        #target illustrator

                        function main()   {

                            if (app.documents.length > 0)   {

                            var doc = app.activeDocument; 

                            var stroke = undefined;

                            var vectors = undefined;

                            var totalLayers = doc.layers.length;

                         

                         

                            while (isNaN(stroke) == true) {

                                stroke = Number(prompt('Enter a numeric lineweight:', "1")); 

                            }

                             

                                for (var i = 0; i < totalLayers; i++)   {

                        vectors = doc.layers[i].pathItems;vectors = doc.layers[i].pathItems;

                        for (var j = 0; j < vectors.length; j++)   {

                        vectors[j].strokeWidth = stroke;

                        }

                                stroke *= 0.500;

                                }

                            }

                        }

                        main();