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

How to correct (round) the size value?

  • March 13, 2017
  • 1 reply
  • 2736 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 14, 2017

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