Skip to main content
Participating Frequently
October 3, 2012
Answered

Create a rectangle around object?

  • October 3, 2012
  • 1 reply
  • 6311 views

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

This topic has been closed for replies.
Correct answer pixxxelschubser

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.

1 reply

CarlosCanto
Adobe Expert
October 3, 2012

yes, it can be done

Participating Frequently
October 4, 2012

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

femkeblanco
Brainiac
July 10, 2022

Sorry to repost, but I wanted to make this with no fill and margin of 0.375 inches all around (so +0.75 inches top/bottom and +0.75 inches left/right). I tried to change the dMarg variable but I don't quite get the ratio. New to scripting. Any help will be appreciated.



I wanted to make this with no fill


Change these two lines

//MargBox.filled = false;
MargBox.fillColor = newCMYK;

to

MargBox.filled = false;
// MargBox.fillColor = newCMYK;

 


... and margin of 0.375 inches all around ...


Units are in points. So change dMarg to

var dMarg = 0.375 * 72;