Tutorial: Scripting for Illustrator CS6

Scripting for Creative Suite products can be a time saver. I will explain in my next post how to share Illustrator files in the Creative Cloud. I’ve figured out that Illustrator creates one page per artboard. If you need to share a page with seven logos for instance, then it’s better to have one artboard per logo. This is not automatic, and you can lose 10 minutes just creating artboards. I took this challenge as an opportunity to write my first script for Illustrator.

What is scripting?

You can write a sequence of commands to control Illustrator. Scripting is also available for other CS6 products, including Photoshop, After Effects, and InDesign. To write a script, you can use your preferred programming language, choosing among AppleScript, JavaScript, or VBScript. I’m on a Mac, so I cannot use VBScript. AppleScript is not my cup of tea, so let’s start with JavaScript. Apparently, I can also code in ActionScript and develop an extension, using Flex to develop a UI for my script. Because I’ll just be writing Illustrator-specific commands, I’ll just code a .jsx file (JavaScript Extension).

Where is the IDE?

To develop an ExtendScript, you can use the ExtendScript Toolkit (ESTK). I was happily surprised to see that it’s installed by default with the CS tools. So let’s launch the ExtendScript Toolkit:

The ExtendScript Toolkit – nice and efficient IDE

Honestly, I wasn’t expecting any IDE. You have Code completion, step-by-step debugging, breakpoints, variable introspection, a documented API, and more. So cool. There are also a lot of samples in the Illustrator folder: Illustrator CS6 > Scripting > Sample Scripts > JavaScript

There is just one thing that really annoyed me with this IDE: the default typeface It’s not a font designed for developers. I could hardly differentiate an ‘i’ from an ‘l’. But you can set the font you like in the Preferences menu. I always use the free and open source “Source Code Pro” font.

Switching from the default font to Source Code Pro. Notice the difference with the “i” and the “l” characters.

Here is a short presentation of the IDE and how you can test your code with Illustrator CS6:

My script

The goal of my script is to generate one art board per layer that will automatically fit the dimensions of the objects contained in each layer. The script starts with a simple test, to check if there is a document opened in Illustrator:

Then I can start my custom function called: createArtboardPerLayer(). It takes the active document as a parameter and starts playing with the layers. To get the total number of layers, here is the line of JavaScript you have to enter. It clearly shows how readable the code is (and the API).

JavaScript

1

2

//get the total number of layers in the active document

vartotalLayers=doc.layers.length;

Then I can loop on the layers and do simple operations:

Check if the layer is visible or not. If the layer is hidden, I skip it.

Check if the layer is locked. If so, I unlock it.

Select everything in the layer.

Create a new art board (Artboard object).

Magically fit the dimensions of the Artboard to the objects in the layer. To do so, there is a fitArtboardtoSelectedArt function. Magic

Then I unselect everything and I work on the next layer.

You don’t need to understand all the functions and objects used in this script, but I want you to see how readable and easy to understand this code is!

Thanks! thought this is just what I was looking for but when I go to run it, as shown, I get the message: Error 60: Not well-formed (invalid token). Line 516
I’m not that savvy with scripts but I would love this to work for me, or something with this function.
Could errors creep in from the download process?