5 Replies Latest reply on Apr 8, 2013 10:14 AM by pixxxel schubser

    How to make a rectangle around activeDocument.selection?

    zjbook

      Hi,everyone!

      Recently I met a awful problem. I want to make a rectangle around current selection, but Not success.

      Could you help me or give some clues? Thanks!

        • 1. Re: How to make a rectangle around activeDocument.selection?
          Damienned

          Try the following code.

           

          Cheers Daniel

           

           

          boundingbox();

           

          function boundingbox()

          {

              // zet nulpunt linksonder

              app.activeDocument.pageOrigin = Array(0,0);

              app.activeDocument.rulerOrigin = Array(0,0);

           

              // set variable

              var breedte = app.activeDocument.width;

              var hoogte = app.activeDocument.height;

              var doc = app.activeDocument;

           

              // omrekenfactor

              var omf = 2.834645;

           

              // maak lijnkleur aan

              var newRGBColor = new RGBColor();

              newRGBColor.red = 0;

              newRGBColor.green = 255;

              newRGBColor.blue = 0;

              var black = new GrayColor();

              black.gray = 100;

              var layercheck = 0;

              var targetDocument = app.activeDocument;

              var layerCount = targetDocument.layers.length;

              var a=1;

              for (var i=1 ; i < 2; i++) {

                  if (a==1){var txt = ''; var layname = 'BB';};

                  var layercheck = 0;

                  for (u = layerCount -1; u >= 0; u--){

                      targetLayer = targetDocument.layers[u];

                      var layerName = new String (targetLayer.name);

                          if (layerName.indexOf(layname) == 0){

                          targetDocument.layers[u].visible= true;

                          targetDocument.layers[u].locked= false;

                          var layercheck = 1;

                      }

                  }

                  if (layercheck == 0){

                      newlayer = documents[0].layers.add();

                      newlayer.name = (layname);

                      newlayer.color = newRGBColor;   

                  }

              }

              alignType = 'top';

              mySelection = activeDocument.selection;

                  if (mySelection.length > 0) {

                      anchorBounds = getPosition(mySelection[0]);

                      var artLayer = doc.layers['BB'].groupItems.add();

                      targetDocument.layers['BB'].preview = false;

                      var breedte = bnds[2] - bnds[0];

                      var hoogte = bnds[1] - bnds[3];

                      var rect = artLayer.pathItems.rectangle ((bnds[1] + (20*omf)),(bnds[0] - (20*omf)), breedte + (40*omf), hoogte + (40*omf));

                      rect.filled = false;

                      rect.stroked = false;

                  } else {

                      anchorBounds = getDocPosition();

                      var doc = app.activeDocument;

                      var omtrekartwork = app.activeDocument.geometricBounds;

                      var leftartwork = omtrekartwork[0];

                      var topartwork = omtrekartwork[1];

                      var rightartwork = omtrekartwork[2];

                      var bottomartwork = omtrekartwork[3];

                      var origin = doc.rulerOrigin;

                      var leftpage    = -origin[0];

                      var toppage    = -origin[1] + doc.height;

                      var rightpage    = -origin[0] + doc.width;

                      var bottompage    = -origin[1];

                      var centerxpage    = -origin[0] + doc.width/2;

                      var centerypage    = -origin[1] + doc.height/2;

                      var bleedleft = leftpage - leftartwork;

                      var bleedright = rightartwork - rightpage;

                      var bleedtop = topartwork - toppage;

                      var bleedbottom = bottompage - bottomartwork;

                      var breedteartwork = rightpage - leftpage;

                      var hoogteartwork = toppage - bottompage;

                      if (bleedleft <= 0 && bleedright <= 0 && bleedtop <= 0 && bleedbottom <=0){

                              var artLayer = doc.layers['BB'].groupItems.add();

                              targetDocument.layers['BB'].preview = false;

                              var rect = artLayer.pathItems.rectangle ((toppage + (20*omf)),(leftpage - (20*omf)), (breedteartwork + (40*omf)), (hoogteartwork + (40*omf)));

                              rect.filled = false;

                              rect.stroked = false;

                      } else {

                              if    (bleedleft >= bleedright){

                                  var bleedx = bleedleft + (20*omf);

                              } else {

                                  var bleedx = bleedright + (20*omf);

                              };

                              if    (bleedbottom >= bleedtop){

                                  var bleedy = bleedbottom + (20*omf);

                              } else {

                                  var bleedy = bleedtop + (20*omf);

                              };

                              var artLayer = doc.layers['BB'].groupItems.add();

                              targetDocument.layers['BB'].preview = false;

                              var rect = artLayer.pathItems.rectangle ((toppage + bleedy),(leftpage - bleedx), (breedteartwork + (2*bleedx)), (hoogteartwork + (2*bleedy)));

                              rect.filled = false;

                              rect.stroked = false;

                      };

                  };

          }

           

          function getBounds(obj) {

              var vbounds = new Array();

              if( obj.typename == 'TextFrame' ) {

                  objcopy=obj.duplicate().createOutline();

                  vbounds = objcopy.visibleBounds;

                  objcopy.remove();

              }

              else if( (obj.typename == 'GroupItem') && !obj.clipped ) {

                  vbounds = getBounds(obj.pageItems[0]);

                  for( n=1; n<obj.pageItems.length; n++  ) {

                      vgb = getBounds(obj.pageItems[n]);

                      if( vbounds[0] > vgb[0] ) vbounds[0] = vgb[0];

                      if( vbounds[1] < vgb[1] ) vbounds[1] = vgb[1];

                      if( vbounds[2] < vgb[2] ) vbounds[2] = vgb[2];

                      if( vbounds[3] > vgb[3] ) vbounds[3] = vgb[3];

                  }

              }

              else if( (obj.typename == 'GroupItem') && obj.clipped && (obj.pageItems.length > 1) ) {

                  vbounds = getBounds(obj.pageItems[0]);

                  cvgb = getBounds(obj.pageItems[1]);

                  objleft = cvgb[0];

                  objtop = cvgb[1];

                  objright = cvgb[2];

                  objbottom = cvgb[3];

                  obj.pageItems[0].stroked ? so = 0 : so = 1;

                  for( n=so; n<obj.pageItems.length; n++ ) {

                      cvgb = getBounds(obj.pageItems[n]);

                      if( objleft > cvgb[0] ) objleft = cvgb[0];

                      if( objtop < cvgb[1] ) objtop = cvgb[1];

                      if( objright < (cvgb[2]) ) objright = cvgb[2];

                      if( objbottom > (cvgb[3]) ) objbottom = cvgb[3];

                  }

                  if( vbounds[0] < objleft ) vbounds[0] = objleft;

                  if( vbounds[1] > objtop ) vbounds[1] = objtop;

                  if( vbounds[2] > objright ) vbounds[2] = objright;

                  if( vbounds[3] < objbottom ) vbounds[3] = objbottom;

              }

              else {

                  vbounds = obj.visibleBounds;

              }

              return vbounds;

          }

           

          function getPosition(obj) {

              var b = new Array();

              bnds = getBounds(obj);

              b['left']    = bnds[0];

              b['top']    = bnds[1];

              b['right']    = bnds[2];

              b['bottom']    = bnds[3];

              b['centerX']    = bnds[0] + (bnds[2]-bnds[0])/2;

              b['centerY']    = bnds[1] - (bnds[1]-bnds[3])/2;

              return b;

          }

                 

          function getDocPosition() {

              var b = new Array();

              doc = activeDocument;

              var origin = doc.rulerOrigin;

              b['left']    = -origin[0];

              b['top']    = -origin[1] + doc.height;

              b['right']    = -origin[0] + doc.width;

              b['bottom']    = -origin[1];

              b['centerX']    = -origin[0] + doc.width/2;

              b['centerY']    = -origin[1] + doc.height/2;

              return b;

          }

          • 2. Re: How to make a rectangle around activeDocument.selection?
            zjbook Level 1

            Thanks,Damienned.

            But I want this script to get the green rectangle while selecting three rectangle in black stroke

            Clip_8.png

            • 3. Re: How to make a rectangle around activeDocument.selection?
              pixxxel schubser Level 5

              Which AI-version?

               

              In CS3 this works for me:

               

              // Box4selectedPath.jsx
              // for selected pathItems   // CS3
              // creates a green rectangle around all the selected paths (visibleBounds)
              // regards pixxxelschubser
              
              // http://forums.adobe.com/thread/1183584
              
              var StrtPOrigin = app.activeDocument.pageOrigin;
              var StrtROrigin = app.activeDocument.rulerOrigin;
              app.activeDocument.pageOrigin = Array(0,0);
              app.activeDocument.rulerOrigin = Array(0,0);
              
              var aDoc = app.activeDocument;
              var Sel = aDoc.selection;
              
              var newRGBColor = new RGBColor();
              newRGBColor.red = 0;
              newRGBColor.green = 255;
              newRGBColor.blue = 0;
              
              if (Sel.length >0 ) {
                  var vBounds = Sel[0].visibleBounds;
                  vBounds_Li = vBounds[0];
                  vBounds_Ob = vBounds[1];
                  vBounds_Re = vBounds[2];
                  vBounds_Un = vBounds[3];
              
                  if (Sel.length >1 ) {
              
                  for (i=1; i<Sel.length ; i++) {
                      vBdsI = Sel[i].visibleBounds;
                      if( vBounds_Li > vBdsI[0] ) {vBounds_Li = vBdsI[0]};
                      if( vBounds_Ob < vBdsI[1] ) {vBounds_Ob = vBdsI[1]};
                      if( vBounds_Re < vBdsI[2] ) {vBounds_Re = vBdsI[2]};
                      if( vBounds_Un > vBdsI[3] ) {vBounds_Un = vBdsI[3]};
                      }
                  }
              
              
              var aRectangle = aDoc.pathItems.rectangle(vBounds_Ob, vBounds_Li, vBounds_Re - vBounds_Li, vBounds_Ob - vBounds_Un);
              aRectangle.strokeColor = newRGBColor;
              aRectangle.strokeWidth= 2;
              aRectangle.filled = false;
              aRectangle.stroked = true;
              aDoc.selection = null;
              
              } else {
                  alert ("no selection")
                  }
              
              app.activeDocument.pageOrigin = StrtPOrigin;
              app.activeDocument.rulerOrigin = StrtROrigin;
              

               

               

              Remember: only pathItems

              • 4. Re: How to make a rectangle around activeDocument.selection?
                zjbook Level 1

                Thanks,Pixxxel.

                It works for me, very nice, Thank you again!