• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

Create a rectangle around object?

Community Beginner ,
Oct 03, 2012 Oct 03, 2012

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

5.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 04, 2012 Oct 04, 2012

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 = Se

...

Votes

Translate

Translate
Adobe
Community Expert ,
Oct 03, 2012 Oct 03, 2012

Copy link to clipboard

Copied

yes, it can be done

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 04, 2012 Oct 04, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 04, 2012 Oct 04, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 05, 2012 Oct 05, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 05, 2012 Oct 05, 2012

Copy link to clipboard

Copied

Add this at as the very first line:

makeRect() ;

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 05, 2012 Oct 05, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 05, 2012 Oct 05, 2012

Copy link to clipboard

Copied

Ive fixed it now!

Thanks everyone for the help great forum!

thanks pixxxel schubser great job!!

Martin

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 05, 2012 Oct 05, 2012

Copy link to clipboard

Copied

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

thanks

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 17, 2019 Jul 17, 2019

Copy link to clipboard

Copied

Hey Martin, I had the same requirement as you.

can you post that finalised scrip here for us then?

Thank you, in advance,

Fred

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 18, 2019 Jul 18, 2019

Copy link to clipboard

Copied

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);

        }

    }

}

makeRect();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 18, 2019 Jul 18, 2019

Copy link to clipboard

Copied

Thank you for that almost instant reply!

Best,

Fred

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 10, 2022 Jul 10, 2022

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jul 10, 2022 Jul 10, 2022

Copy link to clipboard

Copied


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;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 11, 2022 Jul 11, 2022

Copy link to clipboard

Copied

KyuLegend_0-1657523150802.png

it got me closer, but for some reason it's still off... it's adding 0.7917 instead of 0.75.

I looked up the unit conversion and the 0.375*72 part should be correct... but I don't know where the extra area is coming from... My attached photo is a 1 inch x 1 inch square.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 11, 2022 Jul 11, 2022

Copy link to clipboard

Copied

nevermind... I figured it out... the initially selected object path can't have a stroke. Thank You!

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jul 12, 2022 Jul 12, 2022

Copy link to clipboard

Copied

Avec un contour, utilisez

var SelVB = Sel[0].geometricBounds;
var dMarg = 0.375 * 72;

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 02, 2022 Aug 02, 2022

Copy link to clipboard

Copied

Hi, this is a very useful script! Could I ask if it's possible to make the stroke colour a named spot colour instead?

 

This is what I have -

 

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 = 5.5; 
        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 = 0; 
        newCMYK.magenta = 0; 
        newCMYK.yellow = 0; 
        newCMYK.black = 69; 
        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 = 0.25; 
        MargBox.strokeColor = newCMYK; << ADD A SPOT COLOUR INSTEAD?
        MargBox.filled = false; 
        // MargBox.fillColor = newCMYK; 
        MargBox.move(myGroup, ElementPlacement.PLACEATEND); 
        } 
    } 
}
makeRect();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Aug 02, 2022 Aug 02, 2022

Copy link to clipboard

Copied

Hi!

MargBox.strokeColor = activeDocument.spots["toto"].color;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 02, 2022 Aug 02, 2022

Copy link to clipboard

Copied

Excellent thanks for your help!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 03, 2022 Aug 03, 2022

Copy link to clipboard

Copied

Sorry I thought it had worked. It seems to be just adding the colour as a normal colour and not a spot. Any ideas?

 

This is the code -

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 = 5.5; 
        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 = 0; 
        newCMYK.magenta = 0; 
        newCMYK.yellow = 0; 
        newCMYK.black = 69; 
        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 = 0.25; 
	MargBox.strokeColor = activeDocument.spots["PerfCutContour"].color; 
        MargBox.filled = false; 
        // MargBox.fillColor = newCMYK; 
        MargBox.move(myGroup, ElementPlacement.PLACEATEND); 
        } 
    } 
}
makeRect();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Aug 03, 2022 Aug 03, 2022

Copy link to clipboard

Copied

renl80416020_0-1659523962122.png

Pour moi j'ai compris celà ?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 03, 2022 Aug 03, 2022

Copy link to clipboard

Copied

Unfortunetely I'm not getting that. It's applying the correct spot colour but it's not a spot colour with a name. Without the name my plotter wont read the strokes as cut lines. 

Thanks for helping. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Aug 04, 2022 Aug 04, 2022

Copy link to clipboard

Copied

Hi robbo78!

Pouvez vous en dire plus sur le fonctionnement de votre traceur?

Si le tracé doit porter ce nom, alors ajoutez,
MargBox.name = "PerfCutContour";

elleere

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 05, 2022 Aug 05, 2022

Copy link to clipboard

Copied

Hi, could I ask what you mean by "tracker"? Sorry I'm a bit of a newbie to scripting.

 

I will try adding that and see how it goes 🙂

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines