Using scripts to automate Illustrator functions

How to set and forget (part of) your icon design

4 min readJun 29, 2017

--

I’ve recently rediscovered Adobe Creative Cloud (and by ‘rediscovered’, I mean that I’ve got a licence through my work that brings me no end of joy), so I’ve been re-learning some of the Creative Suite programs. The last time I was intimately familiar with Illustrator was in CS…3 maybe? I assumed that Adobe had since made CSI-like effects possible with the tap of a single button, so I was eager to take this baby from zero to a whole bunch of icons in 60 seconds.

Part of my job involves making icons, and there’s never just one colour of an icon — there are resting states, hover states, active states, open states, closed states….so I always need to make the same icon in several different colours.

I created one Illustrator file and put each colour on its own layer because I figured there was a function somewhere that would save each layer in a separate file. After opening every menu and doing some googling I discovered that this feature doesn’t exist. But! I also discovered that JavaScript plays really well with Illustrator — who would have thought?! By writing (or borrowing, let’s be real) some not-too-complicated scripts, you can have your artwork practically saving itself!

There are some scripts that come built into your Illustrator, but the real secret to owning this script thing is an extension that Adobe calls ESTK — ExtendScript Toolkit — that allows you to write your own scripts. I understand that it used to be bundled with AI but starting with CC you need to download it separately. You can find it in the Extensions section of your Creative Cloud dashboard.

Once you’ve got ESTK installed, open it up. Choose Adobe Illustrator and your version from the dropdown in the upper left corner. And here’s where the fun starts!

You can write your own scripts using the available documentation (always read the docs!), or you can thank the stars for the amazing open-source tech community and find yourself a well-written script that already does the thing you have spent all day trying to do. I found a few different options online, and this is the one I used:

#target Illustrator

// script.name = exportLayersAsCSS_PNGs.jsx;
// script.description = mimics the Save for Web, export images as CSS Layers (images only);
// script.requirements = an open document; tested with CS5 on Windows.
// script.parent = carlos canto // 05/24/13; All rights reseved
// script.elegant = false;


/**
* export layers as PNG
* @author Niels Bosma
*/
// Adapted to export images as CSS Layers by CarlosCanto


if (app.documents.length>0) {
main();
}
else alert('Cancelled by user');


function main() {
var document = app.activeDocument;
var afile = document.fullName;
var filename = afile.name.split('.')[0];


var folder = afile.parent.selectDlg("Export as CSS Layers (images only)...");


if(folder != null)
{
var activeABidx = document.artboards.getActiveArtboardIndex();
var activeAB = document.artboards[activeABidx]; // get active AB
var abBounds = activeAB.artboardRect;// left, top, right, bottom


showAllLayers();
var docBounds = document.visibleBounds;
activeAB.artboardRect = docBounds;


var options = new ExportOptionsPNG24();
options.antiAliasing = true;
options.transparency = true;
options.artBoardClipping = true;

var n = document.layers.length;
hideAllLayers ();
for(var i=n-1, k=0; i>=0; i--, k++)
{
//hideAllLayers();
var layer = document.layers[i];
layer.visible = true;


var file = new File(folder.fsName + '/' +filename+ '-' + k +".png");

document.exportFile(file,ExportType.PNG24,options);
layer.visible = false;
}

showAllLayers();
activeAB.artboardRect = abBounds;
}


function hideAllLayers()
{
forEach(document.layers, function(layer) {
layer.visible = false;
});
}


function showAllLayers()
{
forEach(document.layers, function(layer) {
layer.visible = true;
});
}


function forEach(collection, fn)
{
var n = collection.length;
for(var i=0; i<n; ++i)
{
fn(collection[i]);
}
}
}

I’d love to take credit for this genius script, but there are some wizards out there who did the heavy lifting long before I even knew that this kind of power existed. This particular work of art was scripted by CarlosCanto and posted to Adobe Forums.

In the spirit of never just copying-and-pasting things that we don’t understand: the script basically runs forEach loops through your file. Every time the script hits a layer a new file is created. Each new file is given the name of the original file, a number that corresponds with the layer number, and saved as a .png.

Here’s how to watch this magic in action:

  1. Copy the script and paste it into a new file in ESTK. Save the script with a .jsx extension.
  2. Make sure you’ve got Illustrator open with the file that you want to save. (If Illustrator isn’t open, the script will automatically cancel.)
  3. In ESTK, make sure you’ve got your version of Illustrator selected from the top left-side drop-down.
  4. Press the ‘run’ button in ESTK — it’s in the menu across the top and is a green arrow that looks like a ‘play’ button.
  5. You’ll see Illustrator pop to the forefront of your screen, and you’ll be asked where you’d like to save your files.
  6. There will be a kind-of ripple effect as the script goes through the layers of your file.
  7. And then you’re done! Open up the folder that you saved all the files to, and you’ll see that you’ve got one new .png file for each layer!

If you’re super scripty, you could read through the docs and tweak the script to name the files with the layer names, or you could change the order that the layers are saved, or probably lots of other things. I’m really happy with this (relatively) basic script for now — it saves me all kinds of time. Hopefully you’ll get as much use and joy out of it as I do!

Sign up to discover human stories that deepen your understanding of the world.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Jessica Del Grande
Jessica Del Grande

Written by Jessica Del Grande

Web Developer, @junocollege alum (cohort 13).

Responses (2)

Write a response