Tutorial: Scripting for Illustrator CS6

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

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.

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).

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!

You can download my script here: https://github.com/michaelchaize/appliness/blob/master/Illustrator-create-artboards/CreateArtboardsLayers.jsx  You can see it in action in the video that introduces the ExtendScript IDE.

Feel free to improve and use it. Special thanks to David Deraedt for inspiring me on this one. I’ll talk more about David’s work later.

 

 

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

13 Comments

  1. Excellent! And just in time. I have been waiting for a super-easy tutorial to get started in scripting for Ai.

    Cheers

  2. Sorry, I forgot the second part of my comment…

    Do you know of any good resource with common keywords, properties, objects and whatnot (e.g. words like “app” or “currentLayer”) Illustrator supports?

    Kind of like a glosary like jQuery has at http://api.jquery.com/

    Thanks for the great tut!

  3. Many thanks for this script, it saved me a lot of time! It would be really awesome if it copied the layers names to the created artboards.

  4. Thank you so much for this!
    I didn’t even know this was possible.

    One query I have about the automatic sizing of the artboards is, will the artboard detect an effect on a layer, such as a drop shadow?
    Will the artboard fit that into it also?

    Cheers

    • Thanks Niven. Yes, if you drop a shadow, Illustrator will include it and fit the art-board perfectly.

  5. 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?

    • Maybe it’s because one layer is locked, or unnamed… I don’t test all the cases in my script. Try first with a simple file, and see if the script works.

  6. Super useful and less labourous than having to do it manually, will be using this script forever! Thanks!!

  7. Hi ,

    I want to export drop shadow of any art item. do u have any idea how to export this in adobe illustrator javascript api?

    Regards
    Ashish.

  8. Is it just me, or does that GitHub link go to an empty script?

  9. hello Michael, Is there a sample available to get symbol layers. doc.layers returns the layers in document; i have symbols, placed on artbards and i need to get all “symbol layer names”

    Also, when a AI file is exported as SVG, can we maintain the layers order in SVG. In the SVGs i generated, the layer order is not the same as in AI

Trackbacks/Pingbacks

  1. Export SVG for the web with Illustrator CC | Creative droplets - [...] (one per symbol) in a single operation. If you are not familiar with artboards in Illustrator, check this article …

Submit a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">