Copy link to clipboard
Copied
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.
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
...Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
one way to get cm is to use the unitValue object, see a sample usage here
Copy link to clipboard
Copied
selection returns an array, so either do either
var selectedItem = app.activeDocument.selection[0];
or
newItem = selectedItem[0].duplicate();
Copy link to clipboard
Copied
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();
}
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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)
Copy link to clipboard
Copied
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.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now