5 Replies Latest reply on Apr 2, 2017 11:02 PM by eboda_snaf

    JS to select objects at a predefined boundaries?

    eboda_snaf Level 1

      Given geometricBounds, is there a JS to select page items resting in that bound?

       

      Thanks much..

        • 1. Re: JS to select objects at a predefined boundaries?
          Ronald63 Level 4

          Hi,

           

          Try this snippet ...

           

          var doc = app.documents[0];
          var myPageItems = doc.allPageItems;
          var myBounds = [0,0,80,80]
          
          selectByBounds (myPageItems,myBounds)
          
          function selectByBounds (o /*items  array*/, b /*bounds  array*/){
              var len = myPageItems.length;
              var a = new Array();
              for (var j=0; j<len; j++){
                  // get all object inside:
                  if (o[j].geometricBounds[0] >= b[0] &&
                  o[j].geometricBounds[1] >= b[1] &&
                  o[j].geometricBounds[2] <= b[2] &&
                  o[j].geometricBounds[3] <= b[3])
                  {
                  a.push(o[j]);
                  }
                  app.select(a);
               }
          }
          
          • 2. Re: JS to select objects at a predefined boundaries?
            Laubender Adobe Community Professional & MVP

            Hi Ronald,

             

            app.select(a) will not work for pageItems on different spreads.
            And it will also not work as expected with nested objects.
            ( You are using the allPageItems array that is including nested objects. )

             

            Hi boda_snaf,

            do you want to simulate the behavior of a selection marquee?
            That everything the marquess is touching will be selected?

             

            Or is "selecting" for you something like "addressing" ?

             

            Regards,
            Uwe

            • 3. Re: JS to select objects at a predefined boundaries?
              eboda_snaf Level 1

              hi Ronald,

              i'd like to select items that are touching or within an object.

              Thanks much..

               

              i've faced a problem here, although the rotated rectangle did not touch the image physically.

              The result shows otherwise if comparing and checking the geometricbounds of the two pageitems.

              • 4. Re: JS to select objects at a predefined boundaries?
                Laubender Adobe Community Professional & MVP

                Hi eboda_snaf,

                 

                I would create a rectangle with the geometricBounds of your "selection area".
                And then do test with method intersectPath() on duplicates of all page items on the spread.
                It will return an error, if the objects used do not intersect.

                 

                The core of a test with intersectPath() will be something like this:

                 

                // Example
                // Two objects on the page selected:
                
                var objectA = app.selection[0];
                var objectB = app.selection[1];
                
                var dupA = objectA.duplicate();
                var dupB = objectB.duplicate();
                
                try
                {
                    var result = dupA.intersectPath(dupB);
                }
                catch(e)
                {
                   
                    dupA.remove();
                    dupB.remove();
                   
                    alert(e.message);
                };
                
                if(result !== undefined)
                {
                    result.remove();
                    app.select(objectB);
                };
                

                 

                Special cases you have to deal with separately:

                1. Groups and perhaps group like objects like MSOs and buttons.

                2. Graphic lines.

                 

                And this method will not consider stroke weights.

                 

                Regards,
                Uwe