Skip to main content
Inspiring
September 18, 2019
Question

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

  • September 18, 2019
  • 2 replies
  • 3021 views

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!

This topic has been closed for replies.

2 replies

Participant
April 2, 2023

Thank you so much. That's so helpful. I do not know anything about Ai/jsx Scripts but still I am able to use this with the help of your comments in the code. Will you be able to help me for a specific request? I am just a designer and do not know much about these. I think it will be a simple one for you but not for me. I can pay you for that if that doesn't cost too much.

Participant
April 2, 2023

Believe me! I have figured out a solution for my requirement with script from three people like you. I combined all those and created a solution for mine. You guys are great helping people like us. The beauty here is I don't know scripting. Your code is so detailed that helped me. Thank you so much for spending time for us.

CarlosCanto
Community Expert
Community Expert
September 18, 2019

good job!! 

 

thanks for sharing!

Inspiring
September 18, 2019
Thanks Carlos! Great to get a reply from the scripting legend I keep seeing everywhere haha!
CarlosCanto
Community Expert
Community Expert
September 18, 2019
I'm Legend, so is Will Smith 🙂