Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Basic question, duplicating a circle with javascript

Participant ,
Jan 09, 2013 Jan 09, 2013

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.

TOPICS
Scripting
1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Participant , Jan 09, 2013 Jan 09, 2013

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 d

...
Translate
Adobe
Participant ,
Jan 09, 2013 Jan 09, 2013

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 09, 2013 Jan 09, 2013

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

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 09, 2013 Jan 09, 2013

selection returns an array, so either do either

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

or

newItem = selectedItem[0].duplicate();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 09, 2013 Jan 09, 2013

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.duplicate();

        }

    }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 09, 2013 Jan 09, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 09, 2013 Jan 09, 2013
LATEST

if ( app.documents.length > 0 ) {

    var numberOfItems = 3;

    var selectedItem = app.activeDocument.selection;

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

        newItem = selectedItem.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 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)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 09, 2013 Jan 09, 2013

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines