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

Recheck pathItems.length property in for loop

Explorer ,
Jan 08, 2017 Jan 08, 2017

Copy link to clipboard

Copied

hello everyone,

i need your help , i am grouping pathitems and text frames in a layer , while looping through pathitems and text frames using for loop  , if required pathitem and text frames is found it will group move the group to beginning , but here is the problem so pathitem position is changed so i could miss many pathitems so how to resolve this problem.

for eg

here is my code

function read_create()

{

  

  for(layer_iteration=0;layer_iteration<layer_file.length;layer_iteration++)

    {

if (layer_file[layer_iteration].locked){

       layer_file[layer_iteration].locked=false;

       }

    if(layer_file[layer_iteration].name=="NUMBER")

    {

        var theGrp =layer_file[layer_iteration].pathItems;

      

          for(var i=0,len1=theGrp.length; i<len1; i++) {

             

      

if(theGrp.pathPoints.length == 8)

              if(theGrp.stroked == true && theGrp.filled == true) {

           var strkColor = new CMYKColor();

        strkColor.cyan = Math.round(theGrp.strokeColor.cyan);

        strkColor.magenta = Math.round(theGrp.strokeColor.magenta);

        strkColor.yellow = Math.round(theGrp.strokeColor.yellow);

        strkColor.black = Math.round(theGrp.strokeColor.black);

     

        if(Math.round(theGrp.strokeWidth*25.4/72*10)/10 == 0.3 && strkColor.cyan == magentaColor.cyan && strkColor.magenta == magentaColor.magenta && strkColor.yellow == magentaColor.yellow && strkColor.black == magentaColor.black) { 

            

            

                 x=Math.round(theGrp.pathPoints[1].anchor[0]);

                 y=Math.round(theGrp.pathPoints[3].anchor[1]);

                 xx=Math.round(theGrp.pathPoints[5].anchor[0]);

                 xy=Math.round(theGrp.pathPoints[7].anchor[1]);

                 count=0;

                  for(text_iteration=0,len=layer_file[layer_iteration].textFrames.length;text_iteration<len;text_iteration++)

                 {

                     if(text_frame[text_iteration].contents.match(/[A-Z]{1}[0-9]{1}$/) != null||text_frame[text_iteration].contents.match(/[A-Z]{1}[0-9]{2}$/) != null||text_frame[text_iteration].contents.match(/[A-Z]{1}[0-9]{3}$/) != null){

                        

                   t=Math.round(text_frame[text_iteration].anchor[1]);

                

                   t1=(Math.round(text_frame[text_iteration].anchor[0]))

                   alert(text_frame[text_iteration].contents);

                 

                 if(t1>xx&&t1<x){

                     if(t>y&&t<xy) {

                       

                    

                    

                  {

                    

                      var newGroup = layer_file[layer_iteration].groupItems.add();

                      newGroup.name = text_frame[text_iteration].contents+":";

                      theGrp.moveToBeginning(newGroup);

                      text_frame[text_iteration].moveToBeginning(newGroup);

                      len=layer_file[layer_iteration].textFrames.length;

                 

                      

                      }

                 

               }}}

   }}}}}}}}}

TOPICS
Scripting

Views

646

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

Engaged , Jan 08, 2017 Jan 08, 2017

maybe replace this line:

theGrp.moveToBeginning(newGroup);

to that:

theGrp.move(newGroup, ElementPlacement.PLACEATEND);

Votes

Translate

Translate
Adobe
Engaged ,
Jan 08, 2017 Jan 08, 2017

Copy link to clipboard

Copied

Try to use reverse loop sorting of items, like this:

for (var i = collection.length - 1; i >= 0; i--) {

  var obj = collection;

  // some actions

}

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
Engaged ,
Jan 08, 2017 Jan 08, 2017

Copy link to clipboard

Copied

I'm few simplified the code:

  • without changing the logic I removed unnecessary levels of nesting
  • use the reverse iterators (for objects that moved)
  • simplify the regexp
  • declared variables and simplify some variable names

Now it is better to read. Should work. Run it in ESTK and catch the errors:

//@target illustrator

read_create(activeDocument.layers);

function read_create(lays) {

  var lay_i, txt_i, x, y, xx, xy, count, txtFramesLen, t, t1;

  for (lay_i = 0; lay_i < lays.length; lay_i++) {

    if (lays[lay_i].locked) lays[lay_i].locked = false;

    if (lays[lay_i].name != "NUMBER") continue;

    var theGrp = lays[lay_i].pathItems;

    for (var i = theGrp.length - 1; i >= 0 ; i--) {

      if (theGrp.pathPoints.length != 8)  continue;

      if (!theGrp.stroked || !theGrp.filled)  continue;

      var strkColor     = new CMYKColor();

      strkColor.cyan    = Math.round(theGrp.strokeColor.cyan);

      strkColor.magenta = Math.round(theGrp.strokeColor.magenta);

      strkColor.yellow  = Math.round(theGrp.strokeColor.yellow);

      strkColor.black   = Math.round(theGrp.strokeColor.black);     

      if (!(Math.round(theGrp.strokeWidth * 25.4 / 72 * 10) / 10 == 0.3 &&

        strkColor.cyan == magentaColor.cyan &&

        strkColor.magenta == magentaColor.magenta &&

        strkColor.yellow == magentaColor.yellow &&

        strkColor.black == magentaColor.black)) {

        continue;

      }

      x            = Math.round(theGrp.pathPoints[1].anchor[0]);

      y            = Math.round(theGrp.pathPoints[3].anchor[1]);

      xx           = Math.round(theGrp.pathPoints[5].anchor[0]);

      xy           = Math.round(theGrp.pathPoints[7].anchor[1]);

      count        = 0;

      txtFramesLen = lays[lay_i].textFrames.length;

      for (txt_i = txtFramesLen - 1; txt_i >= 0; txt_i--) {

        var text_frame = lays[lay_i].textFrames;

        if (!text_frame[txt_i].contents.match(/[A-Z][0-9]{1,3}$/)) continue;

        t  = Math.round(text_frame[txt_i].anchor[1]);

        t1 = (Math.round(text_frame[txt_i].anchor[0]));

        alert(text_frame[txt_i].contents);

        if (!(t1 > xx && t1 < x)) continue;

        if (!(t > y && t < xy)) continue;

        var newGroup  = lays[lay_i].groupItems.add();

        newGroup.name = text_frame[txt_i].contents + ":";

        text_frame[txt_i].moveToBeginning(newGroup);

      }     

      theGrp.moveToBeginning(newGroup);

    }

  }

}

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
Explorer ,
Jan 08, 2017 Jan 08, 2017

Copy link to clipboard

Copied

Thank you sir,  but pathitems is at beginning , i want text frame should be placed before the pathItem inside the group so textframe will be visible.

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
Engaged ,
Jan 08, 2017 Jan 08, 2017

Copy link to clipboard

Copied

maybe replace this line:

theGrp.moveToBeginning(newGroup);

to that:

theGrp.move(newGroup, ElementPlacement.PLACEATEND);

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
Explorer ,
Jan 08, 2017 Jan 08, 2017

Copy link to clipboard

Copied

LATEST

thank you so much!!!

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