8 Replies Latest reply on Oct 5, 2012 7:40 AM by CarlosCanto

    Create a rectangle around object?

    martin_Cahill

      Can this be done in illustrator scripting??

       

      http://coreldraw.com/forums/t/35280.aspx

       

      creating a rectangle around selected object or objects and set margins...

       

       

       

      Regards

       

      Martin

        • 1. Re: Create a rectangle around object?
          CarlosCanto Adobe Community Professional & MVP

          yes, it can be done

          • 2. Re: Create a rectangle around object?
            martin_Cahill Level 1

            Great news.

             

            I have this script that does what I want, it draws the rectangle around the obect with a distance margin of 0.05cm then groups the objects togeather.

             

            can you help me to convert this script to work in illustrator?

             

            this works in coral draw its VBA

             

            Sub makeRect()

             

                Dim s As Shape, sRect As Shape

                Dim x As Double, y#, h#, w#

                Dim dMarg#

                Dim sr As New ShapeRange

               

                dMarg = 0.05

                ActiveDocument.Unit = cdrCentimeter

               

               

               

                Set s = ActiveShape

                If s Is Nothing Then Exit Sub

               

                s.GetBoundingBox x, y, w, h

               

                Set sRect = ActiveLayer.CreateRectangle2(x - dMarg, y - dMarg, w + (dMarg * 2), h + (dMarg * 2))

                sRect.Outline.Width = 0.001

               

               

               

                sRect.CreateSelection

             

                sr.Add sRect

                sr.Add s

                sr.Group

               

            End Sub

            • 3. Re: Create a rectangle around object?
              pixxxel schubser Level 5

              Hallo martin_Cahill,

              My English is not the best, because of that in German.

               

              Grundsätzlich sind sich VBA und Javascript sehr ähnlich. Versuche die folgende Funktion mit den VBA-Äquivalenten zu vergleichen. Learning by doing

              function makeRect() {
              if (selection.length == 0) {
                  alert("Kein Objekt …");
                  return;
              }else{
                  if (selection.length > 1) {
                      alert("Zu viele Objekte …");
                      return;
                  }else{
                      var myDoc = app.activeDocument;
                      var Sel = myDoc.selection
                      var SelVB = Sel[0].visibleBounds;
                      var dMarg = 28.3464567;
                      var Links = SelVB[0];
                      var Oben = SelVB[1];
                      var SelBreite = (SelVB[2] - SelVB[0]);
                      var SelHoehe = (SelVB[1] - SelVB[3]);
                      var newCMYK = new CMYKColor();
                      newCMYK.cyan = 100;
                      newCMYK.magenta = 0;
                      newCMYK.yellow = 0;
                      newCMYK.black = 0;
                      var myGroup =myDoc.groupItems.add();
                      Sel[0].move(myGroup, ElementPlacement.PLACEATBEGINNING);
                      var MargBox = myDoc.pathItems.rectangle(Oben+dMarg, Links-dMarg, SelBreite+2*dMarg, SelHoehe+2*dMarg);
                      MargBox.stroked = true;
                      MargBox.strokeWidth = 10;
                      //MargBox.filled = false;
                      MargBox.fillColor = newCMYK;
                      MargBox.move(myGroup, ElementPlacement.PLACEATEND);
                      }
                  }
              }
              

               

              Hilft dir das weiter?

               

              Viel Spass noch.

               

              • 4. Re: Create a rectangle around object?
                martin_Cahill Level 1

                Hallo Vielen Dank für die Hilfe

                 

                 

                I have copied the code into a text file and saved it as .jsx in the preset folder of illustrator

                 

                I run the script no errors but nothing happens??

                 

                Martin

                • 5. Re: Create a rectangle around object?
                  [Jongware] Most Valuable Participant

                  Add this at as the very first line:

                   

                  makeRect() ;

                   

                  -- schubser defined a new function but forgot to call it

                  • 6. Re: Create a rectangle around object?
                    martin_Cahill Level 1

                    yes it works!

                     

                    Only thing last thin I would  like no fill colour atall?

                     

                    I have comented MargBox.fillColor = newCMYK; but didnt work

                     

                     

                    Regards

                     

                    Martin

                    • 7. Re: Create a rectangle around object?
                      martin_Cahill Level 1

                      Ive fixed it now!

                       

                      Thanks everyone for the help great forum!

                       

                      thanks pixxxel schubser great job!!

                       

                       

                      Martin

                      • 8. Re: Create a rectangle around object?
                        CarlosCanto Adobe Community Professional & MVP

                        please get into the habit of marking your posts as "Answered" if you get satisfactory answers.

                         

                        thanks