How to write scripts for Adobe Illustrator
Learn why a designer needs scripts in Adobe applications and where to start learning them
What are scripts?
In the context of Adobe software, a script is a program, often small in size, that performs a specific task, usually to automate repetitive operations. Scripts may work without a user interface, carrying out tasks in the background.
For a graphic designer, scripts can make life easier by automating routine operations, generating objects, changing colors, exporting data, processing files, and much more. All these tasks can be done manually, but scripts save time.
Scripts, Extensions, and Plugins
Scripts and extensions are similar. They perform actions in a document using the ExtendScript language. However, extensions have their user interface built with HTML + CSS + JS. Extensions can interact with a web server and leverage advanced JS capabilities.
ExtendScript is an ancient relic of Adobe, a language based on Javascript ECMAScript 3 from 1999
Plugins are complete modules in C++ installed in Adobe software. They add new tools, filters, and effects, similar to plugins from Astute Graphics.
Next, I will talk about learning ExtendScript in the context of Adobe Illustrator, but the essence is applicable to other Adobe programs.
Pros of Scripts
Automation. Scripts eliminate the need to spend time on routine tasks. Although in simple cases, you can expedite work manually with recorded actions without programming.
Speed of Writing. A dozen lines of code, and a straightforward operation is automated. For example, replacing spaces in artboard names. Conveniently, scripts can be executed from anywhere without installing them in the Illustrator scripts folder. Extensions and plugins are much more complicated in this regard.
Versatility. Scripts work on both Windows and macOS. They usually do not depend on the Illustrator version, although occasionally Adobe engineers may disrupt the functionality of scripts with a new Illustrator release. On the other hand, plugins need to be written separately for each system and Illustrator version.
Functionality. Scripts are tailored for specific tasks, saving time. They are flexible and capable of tackling complex challenges.
Cons of Scripts
Performance. Scripts operate slower than plugins on large data. Keep in mind that the computer configuration also influences the speed of scripts.
Capabilities. Due to Adobe’s limited investment in the development of the ExtendScript language, the capabilities of scripts are more modest compared to plugins. For instance, scripts cannot extract information about the colors of pixels in raster images and cannot modify certain gradient properties.
How to Start Writing Scripts
Learn the syntax and principles of JavaScript, upon which ExtendScript is built. This is already half the journey. Knowledge of best practices and typical patterns, which can be gleaned from articles about JS, will be useful for scripting. This will help you write structured code from the beginning.
In terms of basic JS knowledge — mathematical operators, data types, loops — you can refer to resources like Javascript Info, Learn JavaScript.
Foundation:
- Variable declaration using
var
; - Conditional statements
if
,else
,switch
; - Loops
for…
,while…
,for…in
; - Function and object definition
{}
; - Exception handling
try…catch
.
However, you’ll need to sift through information about JS for web browsers and functions from standards newer than ES3. For example, ES5 array methods like forEach
, map
, filter
, string trim
, JSON handling won’t work, but you can write equivalent functions (polyfills) for the ES3 standard.
For simple scripts, it’s not necessary to be a seasoned, bearded programmer. Basics will be sufficient
With basic knowledge of JavaScript and ExtendScript, you’ll be able to read others’ scripts and even make your own modifications. If you want to automate various tasks in Illustrator and tackle complex processes, learning and gaining experience may take a while, but you’ll achieve results.
Immersing into ExtendScript
Primarily, in scripts, you will be manipulating document objects from this tree (DOM — document object model).
Documentation
In Adobe’s developer console, you can access the PDF documentation “Illustrator JS Reference”, “Illustrator Scripting Guide“ in English, where you can find information about Illustrator objects and their properties with some code examples. Kazuhiro Furuhata’s website has a reference for the object model and automation examples — the site is translated from Japanese using Google Translate. On my website (RU), all links are compiled in the “Resources” section. I recommend reading the manuals and simultaneously analyzing scripts from other authors. The list of websites with scripts from some authors is also available on my website.
Tutorials
On YouTube, you can find basic English tutorials on scripting, such as those by Nate Lovell, William Campbell, or articles with simple examples. It’s suitable for getting acquainted, but you won’t find a lot of information there. I haven’t come across comprehensive courses on Illustrator scripts.
Adobe Community
On the Adobe forums, users often ask questions like, “Help me write a script”, where you can study others’ solutions, and sometimes multiple authors offer their script variations. It’s akin to design, where you study other designers’ work to understand why a project was done in a certain way.
Editors (IDE)
Adobe has its development environment — ExtendScript Toolkit, the latest update of which was in 2011. ESTK includes a built-in guide, auto-completion of entered code, and step-by-step debugging. The archive of ESTK versions is on GitHub. Although I prefer writing scripts in VSCode with configured plugins, including ExtendScript, ExtendScript Debugger, and a set of my own snippets — blocks of frequently used code in scripts. You can write code in Sublime, Notepad++, or even a text editor — wherever you feel comfortable.
Regular Expressions
Regular expressions from JavaScript, as an additional foundation, come in handy for working with text, object names, and data type validation. Read about regular expressions on Prooglib, RegexLearn, Cheatography.
// Change the opacity of text beginning with a specific character
var doc = app.activeDocument;
for (var i = 0; i < doc.textFrames.length; i++) {
var text = doc.textFrames[i].contents;
if (/^(А|Р|У)/.test(text)) {
doc.textFrames[i].opacity = 20;
}
}
ChatGPT
Since 2022, developers have increasingly started utilizing neural networks for code generation. For instance, ChatGPT can write scripts for Illustrator, even in the free 3.5 model. If registration for ChatGPT is not available in your region, you can use the Google Bard, the HuggingChat, the Openchat chatbots, although it generates scripts slightly less effectively, or install plugins like Bito or Rubberduck in VSCode.
Pros of AI
Quick Start. ChatGPT can rapidly generate tips and code examples with explanations, accelerating problem-solving and aiding in learning.
Sequence. Neural networks can be used to extend, modify, or fix existing code.
Cons of AI
Limited Knowledge. The neural network knows only what it has been trained on (ChatGPT 3.5 with data up to January 2022). It lacks knowledge of all the intricacies of scripting and errors in various versions of Illustrator.
Code Errors. AI sometimes produces incorrect code, requiring additional user verification and corrections. Therefore, gaining personal knowledge and experience in scripting is advisable. In the example with randomly colored text characters, I had to refine the ChatGPT prompt several times and point out syntax errors to the neural network.
Lack of Context Understanding. Results depend on the phrasing of queries, sometimes requiring numerous iterations and clarifications, consuming time.
Testing. ChatGPT does not test the code for you. Users must independently test the generated script in Illustrator. If errors occur and you lack personal knowledge, identifying whether the problem lies in the neural network’s code or Illustrator can be challenging. In the case of the “Recolor with closest Pantone match” topic, a user attempted to generate a script with ChatGPT, converting a regular color to the closest Pantone. The neural network generated seemingly logical code but with non-existent methods in the ExtendScript language, something the designer couldn’t be aware of, as they are not proficient in scripting.
Conclusions
Writing scripts for Illustrator doesn’t necessarily mean becoming a programmer. Simply, step by step, learn the essential parts of JavaScript and ExtendScript, focusing on solving tasks relevant to you.
Even a basic understanding of if
statements, for…
loops, regular expressions, and the PathItem object model will enable you to work with vector paths in the document.
// Change the width of the paths to at least 50px
var paths = app.selection;
var length = paths.length;
var newWidth = 50;
for (var i = 0; i < length; i++) {
if (paths[i].width < 50) {
paths[i].width = 50;
}
}
And if you’re really interested, don’t give up on learning. Be patient, and gradually you will accumulate scripting skills.
But you didn’t ask what I like most about Illustrator, so I’ll tell you: Javascript. And I’m not a programmer. Nor even a good Javascripter.
Illustrator Interview: James Talmage, 2006