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

How to correct (round) the size value?

  • March 13, 2017
  • 1 reply
  • 2749 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 16, 2017

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


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