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

How to correct (round) the size value?

Explorer ,
Mar 13, 2017 Mar 13, 2017

Copy link to clipboard

Copied

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!

TOPICS
Scripting

Views

1.7K

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
Adobe
Community Expert ,
Mar 13, 2017 Mar 13, 2017

Copy link to clipboard

Copied

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

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
Explorer ,
Mar 14, 2017 Mar 14, 2017

Copy link to clipboard

Copied

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.

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 ,
Mar 14, 2017 Mar 14, 2017

Copy link to clipboard

Copied

So, that makes things more complicated. Because resizing only works on individual objects, which means if you have lots of groups and paths and textframes that make up the artwork you want to resize, each one would be resized individually. Fortunately it sounds like you're using CS6+, so we have access to the "app.executeMenuCommand("group")" method.

Here's the basics. Careful though, this doesn't have any error checking or anything, and I identified a bug regarding live text.. Illustrator is wonky about resizing live text for some reason. You can give it an explicit value, and the correct value will be returned, but then the text will still not be exactly the right size.. For this particular snippet, you either have to kill live text before running the script, or make sure no live text touches the bounding box when you select all the art (in other words, it should be completely contained within the bounds of the other art.)

function resizeArtwork()

{

    var docRef = app.activeDocument;

    var sel = docRef.selection;

    app.executeMenuCommand("group");

    var artGroup = docRef.groupItems[0];

    artGroup.width = Math.round(artGroup.width);

    artGroup.height = Math.round(artGroup.height);

    app.executeMenuCommand("ungroup");

    docRef.selection = null;

}

resizeArtwork();

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
Explorer ,
Mar 15, 2017 Mar 15, 2017

Copy link to clipboard

Copied

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?

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 ,
Mar 15, 2017 Mar 15, 2017

Copy link to clipboard

Copied

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. =(

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
Explorer ,
Mar 16, 2017 Mar 16, 2017

Copy link to clipboard

Copied

Woohoo! The reason is real value of 1 point to 1 mm. True value was 0.35277777777777800000 not 0.353

So, now it work's right, take a look please and correct syntax if it needs.

function resizeArtwork()

{

    var docRef = app.activeDocument;

    var sel = docRef.selection;

    app.executeMenuCommand("group");

    var artGroup = docRef.groupItems[0];

    artGroup.width = Math.round(artGroup.width*0.35277777777777800000)/0.35277777777777800000;

    artGroup.height = Math.round(artGroup.height*0.35277777777777800000)/0.35277777777777800000;

    app.executeMenuCommand("ungroup");

    docRef.selection = null;

}

resizeArtwork();

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 ,
Mar 16, 2017 Mar 16, 2017

Copy link to clipboard

Copied

hmm. perhaps you used more exact decimal points than I did. I used 9 and still got incorrect results.

Glad you got it working though. 😃

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
Explorer ,
Apr 06, 2022 Apr 06, 2022

Copy link to clipboard

Copied

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.

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 ,
Apr 06, 2022 Apr 06, 2022

Copy link to clipboard

Copied

Try chaning this line

var thisAb = aB;

to this

var thisAb = aB[a];

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
Explorer ,
Apr 07, 2022 Apr 07, 2022

Copy link to clipboard

Copied

Awesome, thanks, now the script works. However, the result is a little different than I'd expected. I think Illustrator uses pixels/points as the units when running this script. I have various files with artboard sizes in mm that are XXXX.25mm (w), XXXX.25 (h), I was hoping this script would round them to just XXXXmm (w), XXXXmm (h), but it has rounded to XXXX.328mm (w), XXXX.336mm (h).

Are you able to help (further) so that I can acheive the result I am after?

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 ,
Apr 07, 2022 Apr 07, 2022

Copy link to clipboard

Copied

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. 

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
Explorer ,
Apr 08, 2022 Apr 08, 2022

Copy link to clipboard

Copied

LATEST

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

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