7 Replies Latest reply on Jan 9, 2013 2:36 PM by pixxxel schubser

    Basic question, duplicating a circle with javascript

    maguskrool

      Hello. I'm trying to build something like a bullseye using javascript. I'm new to scripting for Illustrator but I've used javascript before so I thought it would be simple, but maybe I'm missing some basic notions here.

       

      I'm using Illustrator CS6. I created a blank document and drew a circle with a 1pt black stroke and no fill. Selecting that circle, I my script to copy and paste it in front and increase its radius by one centimeter. I attempted a simple duplication first:

       

      if ( app.documents.length > 0 ) {

          var numberOfItems = 3;

          var selectedItem = app.activeDocument.selection;

          for (i=0; i < numberOfItems; i++) {

               newItem = selectedItem.duplicate();

          }

      }

      With my circle selected I get an error message saying "Error 24: selectedItem.duplicate() is not a function". What am I doing wrong?

      Thank you in advance.

        • 1. Re: Basic question, duplicating a circle with javascript
          maguskrool Level 1

          Ok, solved this one when I realized that app.activeDocument.selection returned an array of objects. So now I've got:

           

          if ( app.documents.length > 0 ) {

              var newItem;

              var numberOfItems = 3;

              var radiusIncrease = 20;

              var selectedItem = app.activeDocument.selection[0];

              for (i=0; i < numberOfItems; i++) {

                   newItem = selectedItem.duplicate();

                   newItem.height += radiusIncrease;

                   newItem.width += radiusIncrease;

              }

          }

          All I need is to convert the pixels to centimeters. Does anyone know the best way to achieve this? Thanks.

          1 person found this helpful
          • 2. Re: Basic question, duplicating a circle with javascript
            CarlosCanto Adobe Community Professional & MVP

            selection returns an array, so either do either

             

            var selectedItem = app.activeDocument.selection[0];

             

            or

             

            newItem = selectedItem[0].duplicate();

            • 3. Re: Basic question, duplicating a circle with javascript
              pixxxel schubser Level 5

              the other way (your script):

               

              if ( app.documents.length > 0 ) {

                  var numberOfItems = 3;

                  var selectedItem = app.activeDocument.selection;

                  for (i=0; i < numberOfItems; i++) {

                      newItem = selectedItem[i].duplicate();

                      }

                  }

               

               

              • 4. Re: Basic question, duplicating a circle with javascript
                maguskrool Level 1

                Thanks. I got an error using this option, though, not sure why but it says the last item (in this case, selectedItem[i] when i = 2) is undefined.

                • 5. Re: Basic question, duplicating a circle with javascript
                  maguskrool Level 1

                  Thanks for all the help, my code for now is:

                   

                  if ( app.documents.length > 0 ) {

                      var newItem;

                      var numberOfItems = 3;

                      var radiusIncrease = new UnitValue("1 cm").as("px");

                      var selectedItem = app.activeDocument.selection;

                      for (i=0; i < numberOfItems; i++) {

                          newItem = selectedItem[0].duplicate();

                          newItem.height += radiusIncrease;

                          newItem.width += radiusIncrease;

                          selectedItem[0] = newItem;

                      }

                  }

                  All I need now is to center all the circles, but I haven't really delved into that yet, and if I do run into difficulties I'll start a new thread

                  Again, thank you.

                  • 6. Re: Basic question, duplicating a circle with javascript
                    CarlosCanto Adobe Community Professional & MVP

                    one way to get cm is to use the unitValue object, see a sample usage here

                     

                    http://forums.adobe.com/message/4875863#4875863

                    1 person found this helpful
                    • 7. Re: Basic question, duplicating a circle with javascript
                      pixxxel schubser Level 5

                      if ( app.documents.length > 0 ) {

                          var numberOfItems = 3;

                          var selectedItem = app.activeDocument.selection;

                          for (i=0; i < numberOfItems; i++) {

                              newItem = selectedItem[i].duplicate();

                              }

                          }

                      maguskrool wrote:

                       

                      Thanks. I got an error using this option, though, not sure why but it says the last item (in this case, selectedItem[i] when i = 2) is undefined.

                      Your script says:

                      When (min) one document is open --> say numberOfItems = 3        //the counter is 3 --> min three or more items should be selected by yourself

                      selectedItem --> is the array of all selected items in document

                       

                      then go for counter = 0 to counter < numberOfItems (means 3-1) -->

                      counter 0 = loop 1 --> duplicate this (item 1 of 3 selected) item

                      counter 1 = loop 2 --> duplicate this (item 2 of 3 selected) item   //when only 1 item is selected by yourself --> result is undefined

                      counter 2 = loop 3 --> duplicate this (item 3 of 3 selected) item   //when only 2 items are selected by yourself --> result is undefined

                       

                       

                      It's equal, how many items are selected by user - but min you have defined here --> var numberOfItems = 3; 

                      You need in this example a selection of three items (better: three selected items)