Skip to main content
arop16461101
Known Participant
March 13, 2017
Question

How to correct (round) the size value?

  • March 13, 2017
  • 1 reply
  • 2739 views

All documents i have to work with, they artworks size is like "W:700,125 H:500,211" or "W:400,114 H:500,024".

I need them to "ideal" values, like "W:700 H:500" or "W:400 H:500".

Mannually is too long and booring.

I wonder if code ninjas can help me to make that function;

Thanks!

This topic has been closed for replies.

1 reply

Disposition_Dev
Legend
March 13, 2017

You want to resize the artboards? Or the actual artwork inside the document? For now I'll assume you want to adjust the artboard since resizing the artwork can be much trickier depending on it's composition. Here's a function that will resize all the artboards to the closest integer.

function resizeArtboards()

{

    if(app.documents.length > 0)

    {

        var docRef = app.activeDocument;

        var aB = docRef.artboards;

        //loop each artboard

        for(var a=0;a<aB.length;a++)

        {

            var thisAb = aB;

            //get the width and height of this artboard

            var rect = thisAb.artboardRect;

            var w = Math.round(rect[2] - rect[0]);

            var h = Math.round(rect[1] - rect[3]);

            //set the dimensions of this artboard to the rounded size

            var newDim = [rect[0],rect[1],rect[0] + w, rect[1] - h];

            thisAb.artboardRect = newDim;

        }

    }

    else

    {

        alert("You must have a document open.");

    }

}

resizeArtboards();

arop16461101
Known Participant
March 14, 2017

Thanks alot!

If we resize selected art? Then fit to artwork bounds in another tusk u written to. Take a look please. Resizing artbords is not that i want.

Disposition_Dev
Legend
March 15, 2017

Thanks! It works well, but i artGroup.width still by default in points, not in millimeters, so we round points, then illustrator show them to me in millimeters (like in general prefences), so in millimeters value come not true, 400,241 mm come to 400,05 but not to 400.

Can we transform points to millimeters in script?


Hmm. That seems to be trickier because some information is always lost in the conversion between points and mm... Perhaps someone else has an idea, but it's not working for me because even after you convert the points to mm and then round it, the values are no longer round whenever you set the width/height because you have to convert the rounded mm value back to points which results in some data loss and an imperfect mm value when it's all said and done. =(