Skip to main content
skincareBMD
Known Participant
July 3, 2019
Answered

Re-size Artboards on multiple files (quickly)

  • July 3, 2019
  • 2 replies
  • 3661 views

Community,

I have thousands of PDF files (Illustrator editable) that need to have the first Artboard resized. Completing this manually would consume too many man hours and is unfeasible.

The Actions panel cannot re-size Artboards so I cannot use that method.

I am aware of the scripting potential that is available with Adobe Illustrator. Does anyone have a script that can possibly assist me with my dilemma?

Any sort of assistance would be greatly appreciated.

Thanks,

This topic has been closed for replies.
Correct answer CarlosCanto

I put together a script to resize the 1st artboard of pdf and ai files in a supplied folder.

 

**WARNING**

the script will overwrite your files, please make back ups if your files are valuable...you've been warned.

 

// script.name = batchResizeArtboards.jsx;

// script.description = opens all files in selected folder and resizes first artboard

// script.requirements = open illustrator before running the script

// script.parent = CarlosCanto // 07/04/2019;

// script.support = canto29@gmail.com;

 

 

 

 

function main () {

    // DEBUG ONLY, press shift to set donate flag to false and force bringing up the donate dialog, otherwise, read the preference file to get the flag from it

    var forceShowDialog = false;

    if (ScriptUI.environment.keyboardState.shiftKey) {

        forceShowDialog = true;

    }

 

 

    var sourceFolder = getPreselectFolder ('resizeArtboards', 'sourceFolder');

  

    if (sourceFolder) {

    }

    else {return};

 

 

 

 

    var files = sourceFolder.getFiles (/\.(pdf|ai)$/i); // get files

      

    var filecount = files.length;

  

    if (filecount == 0) {

        alert('no files found in selected folder');

        return;

    }

 

 

    // skip open options window, open file with defaults

    app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

  

    var inches = 72;

 

 

    var title = "Resize First Artboard";

  

    var width = Number(Window.prompt ("Enter New Artboard Width in inches", 5.625, title))*inches;

    var height = Number(Window.prompt ("Enter New Artboard Height in inches", 4.87, title))*inches;

 

 

    var idoc;

      

    for (var k=0; k<files.length; k++) {

        idoc = app.open(files[k]);

      

        artboardProperties (idoc.artboards[0], width, height, true, false);

                      

        app.activeDocument.close (SaveOptions.SAVECHANGES);

    }

  

    donate (File($.fileName).displayName, forceShowDialog);

  

    app.userInteractionLevel = UserInteractionLevel.DISPLAYALERTS;

}

 

 

main ();

 

 

 

 

// resizes the active artboard, returns the new document origin

function artboardProperties (ab, width, height, resize, fromTop) {

 

 

    var abBounds = ab.artboardRect;// left, top, right, bottom

 

 

    var ableft = abBounds[0];

    var abtop = abBounds[1];

    var abright = abBounds[2];

    var abbottom = abBounds[3];

  

    var abwidth = abBounds[2] - ableft;

    var abheight = abtop- abBounds[3];

 

 

    var abctrx = abwidth/2+ableft;

    var abctry = abtop-abheight/2;

 

 

    if (width && height) {

        if (fromTop) {

            var ableft = abctrx-width/2;

            var abtop = abtop;

            var abright = abctrx+width/2;

            var abbottom = abtop-height;

          

            var abctry = abtop-(abtop- abbottom)/2;

        }

        else {

            var ableft = abctrx-width/2;

            var abtop = abctry+height/2;

            var abright = abctrx+width/2;

            var abbottom = abctry-height/2;

        }

  

        var abwidth = abright - ableft;

        var abheight = abtop- abbottom;

      

        if (resize)

            ab.artboardRect = [ableft, abtop, abright, abbottom];

    }

    return {left:ableft, top:abtop, right:abright, bottom:abbottom, centerx:abctrx, centery:abctry, width:abwidth, height:abheight}

}

 

 

 

 

function getPreselectFolder(scriptname, strfolder, extension) {

    var folderStrPref = 'aiscripts.com/'+scriptname+'/'+strfolder;

    var preselectFolder = Folder(app.preferences.getStringPreference (folderStrPref));

    if (!preselectFolder.exists) {

        var preselectFolder = new Folder('~/Desktop');

    }

 

 

    if (extension) {

        var sourceFile = preselectFolder.openDlg('Select Source file...', '*.'+extension, false); // select source file

        if (sourceFile==null) {  // exit if press cancel in either dialog

            alert('Cancelled by user');

            return;

        }

        var folder = sourceFile.parent;

        app.preferences.setStringPreference (folderStrPref, folder.fsName);

        return sourceFile;

    }

    else {

        var folder = preselectFolder.selectDlg('Select '+strfolder+'...'); // select source folder

    }

 

 

    if (folder==null) {  // exit if press cancel in either dialog

        alert('Cancelled by user');

        return;

    }

    app.preferences.setStringPreference (folderStrPref, folder.fsName);

    //preselectFolder = Folder(app.preferences.getStringPreference (folderStrPref));

  

    //return preselectFolder;

    return folder;

}

 

 

 

 

function donate(thisScript, forceShowDialog) {

    var donateStrPref = 'aiscripts.com/donate/'+thisScript;

  

    // press shift to set donate flag to false and force brign up the donate dialog, otherwise, read the preference file to get the flag

    var donated;

    if (forceShowDialog) {

        donated = false;

        app.preferences.setStringPreference (donateStrPref, 'false');

    }

    else {

        donated = app.preferences.getStringPreference (donateStrPref);

    }

  

    if (donated != 'true') {

        var showAgain = showDonateWindow ();

        if (showAgain) {

            //alert('checked');

            app.preferences.setStringPreference (donateStrPref, 'true');

        }

    }

}

 

 

function showDonateWindow() {

    var win = new Window('dialog', 'https://www.paypal.me/aiscripts');

    var lblDonate = win.add('staticText', undefined, 'Do you find the script valuable?\nDo you use the script commercially?\n\nPlease consider donating.', {multiline:true});

    var grp = win.add('group');

    var btnCancel = grp.add('button', undefined, 'Cancel');

    var btnDonate = grp.add('button', undefined, 'Donate');

    var chk = win.add('checkbox', undefined, "Do not show me this message again");

 

 

    btnDonate.onClick = function(){

        openURL( 'www.paypal.me/aiscripts' );

        win.close();

    }

 

 

    win.center();

  

    if(win.show() == 2){

        //alert("pressed Cancel");

        return chk.value;

    } else {

        //alert('pressed donate');

        return chk.value;

    }

    

    function openURL( address ) {

        var f = File( Folder.desktop + '/aiOpenURL.url' );

        f.open( 'w' );

        f.write( '[InternetShortcut]' + '\r' + 'URL=http://' + address + '\r' );

        f.close();

        f.execute();

        f.remove();

    };

}

2 replies

CarlosCanto
Community Expert
CarlosCantoCommunity ExpertCorrect answer
Community Expert
July 5, 2019

I put together a script to resize the 1st artboard of pdf and ai files in a supplied folder.

 

**WARNING**

the script will overwrite your files, please make back ups if your files are valuable...you've been warned.

 

// script.name = batchResizeArtboards.jsx;

// script.description = opens all files in selected folder and resizes first artboard

// script.requirements = open illustrator before running the script

// script.parent = CarlosCanto // 07/04/2019;

// script.support = canto29@gmail.com;

 

 

 

 

function main () {

    // DEBUG ONLY, press shift to set donate flag to false and force bringing up the donate dialog, otherwise, read the preference file to get the flag from it

    var forceShowDialog = false;

    if (ScriptUI.environment.keyboardState.shiftKey) {

        forceShowDialog = true;

    }

 

 

    var sourceFolder = getPreselectFolder ('resizeArtboards', 'sourceFolder');

  

    if (sourceFolder) {

    }

    else {return};

 

 

 

 

    var files = sourceFolder.getFiles (/\.(pdf|ai)$/i); // get files

      

    var filecount = files.length;

  

    if (filecount == 0) {

        alert('no files found in selected folder');

        return;

    }

 

 

    // skip open options window, open file with defaults

    app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

  

    var inches = 72;

 

 

    var title = "Resize First Artboard";

  

    var width = Number(Window.prompt ("Enter New Artboard Width in inches", 5.625, title))*inches;

    var height = Number(Window.prompt ("Enter New Artboard Height in inches", 4.87, title))*inches;

 

 

    var idoc;

      

    for (var k=0; k<files.length; k++) {

        idoc = app.open(files[k]);

      

        artboardProperties (idoc.artboards[0], width, height, true, false);

                      

        app.activeDocument.close (SaveOptions.SAVECHANGES);

    }

  

    donate (File($.fileName).displayName, forceShowDialog);

  

    app.userInteractionLevel = UserInteractionLevel.DISPLAYALERTS;

}

 

 

main ();

 

 

 

 

// resizes the active artboard, returns the new document origin

function artboardProperties (ab, width, height, resize, fromTop) {

 

 

    var abBounds = ab.artboardRect;// left, top, right, bottom

 

 

    var ableft = abBounds[0];

    var abtop = abBounds[1];

    var abright = abBounds[2];

    var abbottom = abBounds[3];

  

    var abwidth = abBounds[2] - ableft;

    var abheight = abtop- abBounds[3];

 

 

    var abctrx = abwidth/2+ableft;

    var abctry = abtop-abheight/2;

 

 

    if (width && height) {

        if (fromTop) {

            var ableft = abctrx-width/2;

            var abtop = abtop;

            var abright = abctrx+width/2;

            var abbottom = abtop-height;

          

            var abctry = abtop-(abtop- abbottom)/2;

        }

        else {

            var ableft = abctrx-width/2;

            var abtop = abctry+height/2;

            var abright = abctrx+width/2;

            var abbottom = abctry-height/2;

        }

  

        var abwidth = abright - ableft;

        var abheight = abtop- abbottom;

      

        if (resize)

            ab.artboardRect = [ableft, abtop, abright, abbottom];

    }

    return {left:ableft, top:abtop, right:abright, bottom:abbottom, centerx:abctrx, centery:abctry, width:abwidth, height:abheight}

}

 

 

 

 

function getPreselectFolder(scriptname, strfolder, extension) {

    var folderStrPref = 'aiscripts.com/'+scriptname+'/'+strfolder;

    var preselectFolder = Folder(app.preferences.getStringPreference (folderStrPref));

    if (!preselectFolder.exists) {

        var preselectFolder = new Folder('~/Desktop');

    }

 

 

    if (extension) {

        var sourceFile = preselectFolder.openDlg('Select Source file...', '*.'+extension, false); // select source file

        if (sourceFile==null) {  // exit if press cancel in either dialog

            alert('Cancelled by user');

            return;

        }

        var folder = sourceFile.parent;

        app.preferences.setStringPreference (folderStrPref, folder.fsName);

        return sourceFile;

    }

    else {

        var folder = preselectFolder.selectDlg('Select '+strfolder+'...'); // select source folder

    }

 

 

    if (folder==null) {  // exit if press cancel in either dialog

        alert('Cancelled by user');

        return;

    }

    app.preferences.setStringPreference (folderStrPref, folder.fsName);

    //preselectFolder = Folder(app.preferences.getStringPreference (folderStrPref));

  

    //return preselectFolder;

    return folder;

}

 

 

 

 

function donate(thisScript, forceShowDialog) {

    var donateStrPref = 'aiscripts.com/donate/'+thisScript;

  

    // press shift to set donate flag to false and force brign up the donate dialog, otherwise, read the preference file to get the flag

    var donated;

    if (forceShowDialog) {

        donated = false;

        app.preferences.setStringPreference (donateStrPref, 'false');

    }

    else {

        donated = app.preferences.getStringPreference (donateStrPref);

    }

  

    if (donated != 'true') {

        var showAgain = showDonateWindow ();

        if (showAgain) {

            //alert('checked');

            app.preferences.setStringPreference (donateStrPref, 'true');

        }

    }

}

 

 

function showDonateWindow() {

    var win = new Window('dialog', 'https://www.paypal.me/aiscripts');

    var lblDonate = win.add('staticText', undefined, 'Do you find the script valuable?\nDo you use the script commercially?\n\nPlease consider donating.', {multiline:true});

    var grp = win.add('group');

    var btnCancel = grp.add('button', undefined, 'Cancel');

    var btnDonate = grp.add('button', undefined, 'Donate');

    var chk = win.add('checkbox', undefined, "Do not show me this message again");

 

 

    btnDonate.onClick = function(){

        openURL( 'www.paypal.me/aiscripts' );

        win.close();

    }

 

 

    win.center();

  

    if(win.show() == 2){

        //alert("pressed Cancel");

        return chk.value;

    } else {

        //alert('pressed donate');

        return chk.value;

    }

    

    function openURL( address ) {

        var f = File( Folder.desktop + '/aiOpenURL.url' );

        f.open( 'w' );

        f.write( '[InternetShortcut]' + '\r' + 'URL=http://' + address + '\r' );

        f.close();

        f.execute();

        f.remove();

    };

}

skincareBMD
Known Participant
July 8, 2019

CarlosCanto,

I greatly appreciate you sharing this script with me. It worked exactly as intended which is a huge relief for me. The only issue I have now is; re-sizing everything based off a search as opposed to pointing the script to a specific folder.

Would this be possible to accomplish?

Any assistance would be greatly appreciated!

CarlosCanto
Community Expert
Community Expert
July 8, 2019

if you're on Windows there might be hope.

in the meantime is it feasible to move the searched results to a temp folder?

Kurt Gold
Community Expert
Community Expert
July 3, 2019

Can you share a sample file?

Which version of Illustrator are you using?

skincareBMD
Known Participant
July 3, 2019

I am trying to resize the first artboard which is an 8.5 x 11 to (5.625 x 4.87).

I am using the latest version.

https://brandmd.box.com/s/0yrd0era89tla6qq0p9q7o1u555wwmaj

Kurt Gold
Community Expert
Community Expert
July 4, 2019

It should be possible to modify the action that has been discussed in this thread:

Action to add specific dimension to left and right side of current Artboard?

Then you could execute it in batch mode.