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

Creating Multiple Objects using a for loop

New Here ,
Feb 28, 2011 Feb 28, 2011

Copy link to clipboard

Copied

PLEASE HELP

I'm newer to AI Scripting.

I need to create 30 arrow like objects (d0,d1,...,d29) with various lengths.  I can store the lengths in an array ('mylength[]') - but do i need to copy the same code over and over 30 times?  Can I create an object where the name of the object is a member of another array?  If i could I could run a for loop to create all 30 arrows.

This is the code i have -

var d0 = peoplelayer.pathItems.add(); d0.closed = true;

d0.setEntirePath([[0,0], [-16, 12], [-16, 8], [-mylength[0], 8],[-mylength[0],-8],[-16,-8],[-16,-12]]);

This code has the idea but it doesnt work -

var arrownames = ["d0","d1","d2",...,"d29"]

var arrownames[0] = peoplelayer.pathItems.add(); arrownames[0].closed = true;

arrownames[0].setEntirePath([[0,0], [-16, 12], [-16, 8], [-mylength[0], 8],[-mylength[0],-8],[-16,-8],[-16,-12]]);

Have something that might?

TOPICS
Scripting

Views

628

Translate

Translate

Report

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

Community Expert , Mar 01, 2011 Mar 01, 2011

Hi theBGF.

var mylength = [5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100,105,110,115,120,125,130,135,140,145,150,155];
var d = new Array();

for (i=0;i<30;i++){
    d = app.activeDocument.layers[0].pathItems.add();
    d.setEntirePath([[0,0], [-16, 12], [-16, 8], [-mylength, 8],[-mylength,-8],[-16,-8],[-16,-12]]);
    d.closed = true;
    }

Try this script.
You can access arrow object (pathItems) are d[1] to d[30].

Votes

Translate

Translate
Adobe
Community Expert ,
Mar 01, 2011 Mar 01, 2011

Copy link to clipboard

Copied

Hi theBGF.

var mylength = [5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100,105,110,115,120,125,130,135,140,145,150,155];
var d = new Array();

for (i=0;i<30;i++){
    d = app.activeDocument.layers[0].pathItems.add();
    d.setEntirePath([[0,0], [-16, 12], [-16, 8], [-mylength, 8],[-mylength,-8],[-16,-8],[-16,-12]]);
    d.closed = true;
    }

Try this script.
You can access arrow object (pathItems) are d[1] to d[30].

Votes

Translate

Translate

Report

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
Guide ,
Mar 01, 2011 Mar 01, 2011

Copy link to clipboard

Copied

Just an alternative… If I understood…

for (var i = 0, l = 5; i < 30; i++, l += 5)      {      //$.writeln(i+' '+l);      var a = app.activeDocument.layers[0].pathItems.add();      a.name = 'Arrow '+i;      a.setEntirePath([[0,0],[-16, 12],[-16, 8],[-l, 8],[-l,-8],[-16,-8],[-16,-12]]);      a.closed = true;      }      app.activeDocument.layers[0].pathItems.getByName('Arrow 10');

Votes

Translate

Translate

Report

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 ,
Mar 01, 2011 Mar 01, 2011

Copy link to clipboard

Copied

I think some situation, name property is very useful.
Thanks Mark!  (^_^;

Votes

Translate

Translate

Report

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
New Here ,
Mar 01, 2011 Mar 01, 2011

Copy link to clipboard

Copied

Awesome - this worked perfectly.  Thank you.

Votes

Translate

Translate

Report

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
Guide ,
Mar 02, 2011 Mar 02, 2011

Copy link to clipboard

Copied

LATEST

No problem… I saw no point in storing an Array for later access… You already have one the relative 'path items collection'… Naming items is the easiest method you can also use notes or better still tags (a single item can have numerous tags) very handy. No need to have a list of incrementing values when you can make them as you go… someObject.getByName(someString); will return you the 'first' object that matches your query… so make each unique…

Votes

Translate

Translate

Report

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