Skip to content

Instantly share code, notes, and snippets.

@shivendra14
Created September 17, 2017 14:19
Show Gist options
  • Save shivendra14/f135d6279a20053a62a798be5da5e174 to your computer and use it in GitHub Desktop.
Save shivendra14/f135d6279a20053a62a798be5da5e174 to your computer and use it in GitHub Desktop.
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;
}
@24tracks
Copy link

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

fire script my friend. cheers.

@zlovatt
Copy link

zlovatt commented Apr 19, 2023

👋 For others finding this, I've posted up a modified version of it here.

This modified version will:

  • Prompt you to enter a new artboard dimension (in width & height pixel values)
  • Resize the current open AI file's artboard and all art to that size
  • Will handle hidden & locked layers

It doesn't batch-process an entire folder or let you pick files – it solely looks at the doc you're working in.

A bit more user-friendly imo, but it heavily relies on the work @shivendra14 put together here.

@LeyendaV
Copy link

LeyendaV commented May 8, 2023

👋 For others finding this, I've posted up a modified version of it here.

This modified version will:

  • Prompt you to enter a new artboard dimension (in width & height pixel values)
  • Resize the current open AI file's artboard and all art to that size
  • Will handle hidden & locked layers

It doesn't batch-process an entire folder or let you pick files – it solely looks at the doc you're working in.

A bit more user-friendly imo, but it heavily relies on the work @shivendra14 put together here.

Is it possible to make a version of this script for Photoshop?

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