Script to Resize Artboard Dimensions if They're Below Specified Width or Height

Explorer ,
Sep 17, 2019

Copy link to clipboard

Copied

Hello world! I've built a script for Illustrator 2019 that checks if any artboards are under a certain minimum size specified as var width & var height and resizes any that are under those widths or heights to another specified height/width. It's commemted out so much that even a complete noob like me would understand every little thing it does.

 

It's based on this script here: https://github.com/iconifyit/ai-scripts/blob/master/Resize%20All%20Artboards.jsx

which i think is based on Carlos Cantos' script from these forums.

 

Here's a script for if you only need the width checked:

 

#target Illustrator  

    var idoc  = app.activeDocument;

    var width = (7*72) //# of inches multiplied by ppi which is 72
    //var height = (7*72) //uncomment this if you want to set height as well

    for (i=0; i<idoc.artboards.length; i++) {

            var abBounds = idoc.artboards[i].artboardRect; // the contents of this list are 0=left, 1=top, 2=right, 3=bottom


            var ableft = abBounds[0]; //these 4 vars just set the bounds of the artboard to readable names
            var abtop = abBounds[1];
            var abright = abBounds[2];
            var abbottom = abBounds[3];

            var abwidth = abBounds[2] - ableft; // these 2 vars calculate the width & height based on the bounds. right - left = width
            var abheight = abtop- abBounds[3]; // top - bottom = height

            var abctrx = abwidth/2+ableft; //these 2 vars help center the artboard
            //var abctry = abtop-abheight/2; //uncomment this if you want to set height as well

            var ableft = abctrx-width/2; //these 4 vars calculate the new bounds of the artboard
            //var abtop = abctry+height/2;  //uncomment this if you want to set height as well
            var abright = abctrx+width/2;
            //var abbottom = abctry-height/2;  //uncomment this if you want to set height as well


           /** if (abwidth < (6*72) && abheight < (6*72)) { //this if statement detects if your width & height is under your set # of inches multiplied by ppi which is 72 (since illustrator measures in pixels). if it's lower, it resizes it to var width & var height shown above.

                idoc.artboards[i].artboardRect = [ableft, abtop, abright, abbottom]; } else { //this resizes the width & height, artboard centered
            */      
                    
                    //uncomment above for fixing height as well
                if (abwidth < (6*72)) { 

                    idoc.artboards[i].artboardRect = [ableft, abBounds[1], abright, abBounds[3]]; } //this resizes the width only if it's less than specified above, artboard centered
                /** else { if (abheight < (6*72)) {
                        idoc.artboards[i].artboardRect = [abBounds[0], abtop, abBounds[2], abbottom]; //this resizes the height only if it's less than specified above, artboard centered
                    }}} */
                    //uncomment above for fixing height as well
                }

 

 

& Here's a version if you want it to check both width & height and fix only the dimension(s) that are smaller than the set thresholds.

 

#target Illustrator  

    var idoc  = app.activeDocument;

    var width = (7*72) //# of inches multiplied by ppi which is 72
    var height = (7*72) //uncomment this if you want to set height as well

    for (i=0; i<idoc.artboards.length; i++) {

            var abBounds = idoc.artboards[i].artboardRect; // the contents of this list are 0=left, 1=top, 2=right, 3=bottom


            var ableft = abBounds[0]; //these 4 vars just set the bounds of the artboard to readable names
            var abtop = abBounds[1];
            var abright = abBounds[2];
            var abbottom = abBounds[3];

            var abwidth = abBounds[2] - ableft; // these 2 vars calculate the width & height based on the bounds. right - left = width
            var abheight = abtop- abBounds[3]; // top - bottom = height

            var abctrx = abwidth/2+ableft; //these 2 vars help center the artboard
            var abctry = abtop-abheight/2; //uncomment this if you want to set height as well

            var ableft = abctrx-width/2; //these 4 vars calculate the new bounds of the artboard
            var abtop = abctry+height/2;  //uncomment this if you want to set height as well
            var abright = abctrx+width/2;
            var abbottom = abctry-height/2;  //uncomment this if you want to set height as well


           if (abwidth < (6*72) && abheight < (6*72)) { //this if statement detects if your width & height is under your set # of inches multiplied by ppi which is 72 (since illustrator measures in pixels). if it's lower, it resizes it to var width & var height shown above.

                idoc.artboards[i].artboardRect = [ableft, abtop, abright, abbottom]; } else { //this resizes the width & height, artboard centered
                 
                    
                    //uncomment above for fixing height as well
                if (abwidth < (6*72)) { 

                    idoc.artboards[i].artboardRect = [ableft, abBounds[1], abright, abBounds[3]]; } //this resizes the width only if it's less than specified above, artboard centered
                else { if (abheight < (6*72)) {
                        idoc.artboards[i].artboardRect = [abBounds[0], abtop, abBounds[2], abbottom]; //this resizes the height only if it's less than specified above, artboard centered
                    }}}
                    //uncomment above for fixing height as well
                }

 

 

Thanks for checking this out!

TOPICS
How to, Scripting, SDK, Third party plugins, Tools

Views

447

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Sep 17, 2019

Copy link to clipboard

Copied

good job!! 

 

thanks for sharing!

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
Sep 18, 2019

Copy link to clipboard

Copied

Thanks Carlos! Great to get a reply from the scripting legend I keep seeing everywhere haha!

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Sep 18, 2019

Copy link to clipboard

Copied

I'm Legend, so is Will Smith 🙂

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more