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

D4v3
Inspiring
April 6, 2022

Hi, I am really late to the party here, but I stumbled accross your snippet to round artboard sizes to the nearest integer. As it happens, this is exactly what I want to do. However, when I run the script I get this error:

 

Error 2: artboard is undefined.

line: 25

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

 

I am using Illustrator 26. Also, I know very little about Javascript. If you (or anyone else) are able to help me to resolve this, it would be greatly appreciated.

D4v3
Inspiring
April 8, 2022

I don't think what you want is possible.  As you say, Illustrator's inner units is points.  Conversion between mm and points will always end up with a decimal. 


Ok, no problem. Thanls very much for your help and taking the time to reply.