Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to scale up Illustrator Assets
#target Illustrator
/*
Revision-1
Author: Shivendra Agarwal
Year: 2017
Title: Script to scale-up artwork and artboard above 15 Mpixel
*/
if ( app.documents.length > 0 )
alert("ERROR: \n Close all documents before running this script." );
requiredABarea = prompt( 'Enter a minimum desired artboard area in pixels.', '15000000', 'Select artboard area');
dir = Folder.selectDialog("Select root folder containing Illustrator assets.");
// If dir variable return null, user most likely canceled the dialog or
// the input folder and it subfolders don't contain any .ai files.
if ( dir != null )
{
// returns an array of file paths in the selected folder.
files = GetFiles( dir );
alert ('Total ' + files.length + ' files (AI/EPS) will be processed.', 'Alert');
for (var f = 0; f< files.length; f++)
{
var doc = app.open(files[f]);
resizeArtboardAndArwork();
doc.close(SaveOptions.SAVECHANGES);
}
}
function resizeArtboardAndArwork()
{
activeDoc = app.activeDocument;
if (activeDoc.artboards.length > 1)
alert ('Script is not designed for multiple artboard. \nWorking on active artboard.', 'Alert');
abActive = activeDoc.artboards[ activeDoc.artboards.getActiveArtboardIndex() ];
//activeDoc.fitArtboardToSelectedArt(activeDoc.artboards.getActiveArtboardIndex());
var abProps = getArtboardBounds(abActive);
scale = findRequiredScale(abProps);
abXoffset = -1* (abProps.left + abProps.width/2);
abYoffset = -1* (abProps.top - abProps.height/2);
if (scale > 1)
{
// select all items
var items = activeDoc.pageItems;
for(var i = 0;i < items.length;i++)
{
items[i].selected = true;
}
var selection = activeDoc.selection;
// Translate artwork to bring artboard-center at document center, and then apply scale.
if (selection.length > 0)
{
for (i = 0; i < selection.length; i++)
{
selection[i].translate (abXoffset, abYoffset, true, true, true, true);
selection[i].resize (scale*100, scale*100, true, true, true, true, scale*100, Transformation.DOCUMENTORIGIN);
}
}
var scaledArtboardRect = newRect(-abProps.width/2 * scale, -abProps.height/2 * scale, abProps.width * scale,abProps.height * scale);
var newAB = activeDoc.artboards.add(scaledArtboardRect);
abActive.remove();
//activeDoc.fitArtboardToSelectedArt(activeDoc.artboards.getActiveArtboardIndex());
app.executeMenuCommand("fitall");
}
}
function findRequiredScale(props)
{
requiredABarea = 15000000; //px
currentABarea = props.width * props.height;
scale = (Math.sqrt(requiredABarea/ currentABarea));
if (scale >1)
return scale;
else
return 1;
}
// Artboard bounds helper (used above):
function getArtboardBounds(artboard) {
var bounds = artboard.artboardRect,
left = bounds[0],
top = bounds[1],
right = bounds[2],
bottom = bounds[3],
width = right - left,
height = top - bottom,
props = {
left : left,
top : top,
width : width,
height : height
};
return props;
}
function newRect(x, y, width, height)
{
var l = 0;
var t = 1;
var r = 2;
var b = 3;
var rect = [];
rect[l] = x;
rect[t] = -y;
rect[r] = width + x;
rect[b] = -(height - rect[t]);
return rect;
};
function GetFiles( folder )
{
var i, item,
// Array to store the files in...
files = [],
// Get files...
items = folder.getFiles();
// Loop through all files in the given folder
for ( i = 0; i < items.length; i++ )
{
item = items[i];
// Find .ai files
var aifileformat = item.name.match(/\.ai$/i);
var epsfileformat = item.name.match(/\.eps$/i);
// If item is a folder, check the folder for files.
if ( item instanceof Folder )
{
// Combine existing array with files found in the folder
files = files.concat( GetFiles( item ) );
}
// If the item is a file, push it to the array.
else if ( item instanceof File && (epsfileformat || aifileformat) )
{
// Push files to the array
files.push( item );
}
}
return files;
}
@mmnowak86
Copy link

mmnowak86 commented Jan 16, 2018

Great script! Thanks a lot :)

@chiaroscurosity
Copy link

chiaroscurosity commented Jun 11, 2018

Not sure how to use this script exactly. By default, it has 15000000 as a pixel size. When I put in 56 it outputs 3872.98 px. This is not what I want. I want 56 px x 56 px. What am I not understanding? Thanks.

@24tracks
Copy link

24tracks commented Oct 23, 2019

Not sure how to use this script exactly. By default, it has 15000000 as a pixel size. When I put in 56 it outputs 3872.98 px. This is not what I want. I want 56 px x 56 px. What am I not understanding? Thanks.

Me too!

@pnjsl
Copy link

pnjsl commented Mar 16, 2020

Really good script. Thanks !

@emmamy
Copy link

emmamy commented May 14, 2021

This script looks great, but I'm also unclear how to get the correct dimensions; I'm getting seemingly random results too. I'm trying to resize my artboards to 1920 pixels wide, but they end up 5163.9778 pixels wide after running this script.

@Tom003
Copy link

Tom003 commented Oct 22, 2021

Not sure how to use this script exactly. By default, it has 15000000 as a pixel size. When I put in 56 it outputs 3872.98 px. This is not what I want. I want 56 px x 56 px. What am I not understanding? Thanks.

Me too!

Looks like you have to edit the script.

findRequiredScale(props)

needs another argument, eg userInput to get the user-defined value from the prompt. Currently this function is just defining the required artboard area to be a default value. The user input value never gets captured.

@justcodebruh
Copy link

justcodebruh commented May 3, 2022

fire script my friend. cheers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment