Skip to main content
Disposition_Dev
Legend
August 12, 2015
Question

Help with cleaning up array of pageItems, please.

  • August 12, 2015
  • 3 replies
  • 1017 views

Hey folks.

I have a question that only loosely pertains to illustrator, as I think I'm just missing something really obvious in basic JS logic..

I've written a script to create artboards for me that fit tight to the outside of many pageItems. In the first iteration, I wrote the script to look only for groupItems because I was assuming my production artists were doing things properly. Turns out, many of them 'walk to the beat of a different drummer', if you will. So I've decided to give up on changing their habits and take this as an exercise in self improvement with regards to scripting unpredictability.

Now I'm rewriting the script to work with any pageItem. The problem I'm running into is the exclusion of a pageItem that is either- A: Completely contained within another object, or B: Shares left && || top && || right && || bottom coordinates (eg. object a has bounds 5, 5, 10, -10 and object b has bounds 5, 4, 9, -9).

So far I have a function written to sort through the pageItems and sort them into rows (within a certain tolerance). After i determine all pageItems that are in a particular row, I want to loop that row to check for any items that are completely contained within another object, or share some bounds as the example above. The problem I'm running into is how to properly and efficiently do this check.. I've done this successfully when I'm only looking for something completely contained within, like this:

var array = [(all pageItems of given row are pushed here)]

for(a=array.length-1;a>-1;a--){

     var curObj = array;

     var L = curObj.left;

     var T = curObj.top;

     var R = L + curObj.width;

     var B = T - curObj.height;

     for(b=array.length-1;b>-1;b--){

          var compareObj = array;

          var CL = compareObj.left;

          var CT = compareObj.top;

          var CR = CL + compareObj.width;

          var CB = CT - compareObj.height;

          if(CL > L && CT < T && CR < R && CB > B){

               array.splice(b,1);

          }

     }

}

This part works fine, but only because it returns false when a=b (since when a=b it's comparing the same object, so if I include >= or <=, it splices every object from the array and nothing happens).

So, my question is, how do I set up that second loop to ignore curObj? My first inclination is to simply splice the curObj before I start the second loop and set the compareObj variable. But there's a good chance that object needs to be compared later.

Sorry if that's confusing.. i've been pulling my hair out trying to wrap my head around this (probably really simple) issue.

**EDIT**

I made a quick visualization for what i'm looking to do. In the attached screenshot I want to loop through this row of objects and splice the black boxes out of the array while leaving the gray boxes.

This topic has been closed for replies.

3 replies

Disposition_Dev
Legend
August 12, 2015

why why why why why why why why why on earth is artwork that protrudes outside of the bounds of a clipping mask included in the "visibleBounds" of said clipping mask.

the very nature of the clipping mask is that it makes INVISIBLE anything that's outside of that clipping mask.

Silly-V
Legend
August 12, 2015

I think your issue is that you had some crazy crackpot helping you out with this in the first place.

However, I was able to do something like

  1.           if(CL == L && CT == T && CR == R && CB == B){ 
  2.                continue
  3.           }

Which ignores the object when it's the same object- is that what you need?

*Of course be ware of objects having identical coords, one on top of the other precisely- that'll fail.

Disposition_Dev
Legend
August 12, 2015

haha. he was no crackpot i assure you.

I think that might work.. However, i don't always want to ignore something just because it has the same bounds.. I only want to ignore the curObj from loop A.

The reason for this is, sometimes I'll have a clipping mask that is the exact same size as my "container object" (ie. the shirt body. See screenshot. the clipping mask on the left would fit directly over the shirt on  the right, and the clip mask is made with the body of the shirt, so the bounds are identical.)

I still want to splice that clipping mask out of the array, otherwise i get 2 artboards in the same spot.

Silly-V
Legend
August 12, 2015

Hey, if the problem is artboards in the same spot, can you just make sure to not put any artboards into the same spot where a previous one already exists? This would negate the splicing issue.

CarlosCanto
Community Expert
Community Expert
August 12, 2015

The issue may be in Splicing, why don't you push objects into a different array?

Disposition_Dev
Legend
August 12, 2015

Because after I've ascertained the "container objects" as i've come to call them (the gray boxes in the example) i want to remove everything that falls inside of these. At the end of the script, I use this array to create the artboards via docRef.fitArtboardToSelectedArt();

Once i've determined that an object is inside of another object, I want the script to forget about it all together.

CarlosCanto
Community Expert
Community Expert
August 12, 2015