Getting started with scripting in adobe illustrator using JavaScript.

Author: Praweg Koirala

🗓️ June 27, 2024

⌛️ 5 mins

🪴 Growing

🏷️    javascript

Adobe Illustrator is a fantastic tool for designing logos, icons, illustrations, and any type of graphic for prints, digital media, mobile and web applications, or any other design work you can think of. As a designer, you should focus as much as possible on creative tasks, not mundane, repetitive ones such as creating files, resizing, dealing with different formats, etc. This is where automation comes in handy. For those comfortable working with code, scripting in Adobe Illustrator can help increase productivity and efficiency. This tutorial covers the automation process using JavaScript.

Scripting Languages & Support in Adobe Illustrator

While this article focuses on scripting using JavaScript, Adobe Illustrator also supports other languages. Here are the details:

Supported languages and extensions (source: Official Documentation Adobe Illustrator CC):

| Script type | File types(extensions) | Platforms | | ----------------------------- | ------------------------------------------------ | --------------- | | Apple script | compiled script (.scpt) OSAS file (no extension) | Mac OS | | JavaScript or extended script | text (.js or .jsx) | Windows, Mac OS | | VB script | text (.vbs) | Windows |

Prerequisites
Adobe Illustrator Software
IDE/TextEditor (VS Code used in this tutorial; any allowing JavaScript writing can be used)
Familiarity with JavaScript (.js/.jsx file extensions)
Running Scripts within Adobe Illustrator

Scripts can be accessed from the script menu in the toolbar or from an external source. For experimental use, accessing from an external source is more preferable. However, if you plan on using scripts regularly, it's easier to have them available in the toolbar.

There are two ways to access the script:

Script Menu
    To include a script in the Scripts menu (File > Scripts), save the script in the Scripts folder, located in the /Illustrator CC/Presets folder in your Illustrator CC installation directory.
    The newly added script should appear in the file>script menu (Adobe Illustrator needs to be restarted if the script was created and saved while running the application).
External Source (Other Scripts Menu)
    Create your script file and save it in any destination.
    To load the script, go to the Script Menu(File > Scripts) and select other scripts. This will bring up a dialog box that allows you to select your script file from any destination.
Writing your first script in Illustrator

In our example, we use VS Code to write JavaScript code. We store our script file in a directory called myIllustrator_scripts in the system's root directory. You can store your script in any folder you like.

Open Visual Studio Code and the project folder. Inside the project folder, create a new file called helloWorld.jsx.
We are going to create a script that will automatically open a new illustrator document with a text that says “Hello World”..
In our code, first we need to create a new illustrator document . Then, create a text frame inside the new document and store it in another variable. Define the position for this text and its content as “Hello World” and save the file.
Open Adobe Illustrator, go to file > scripts > other scripts and navigate to the destination folder to select the helloWorld.jsx script. Illustrator will run this script, and as a result, we will get a new document with a “Hello World” text positioned as we defined in our script.
Congratulations! You've successfully created and run your first script.

Here's the code snippet for the helloWorld.jsx script:

// Hello World! var myDocument = app.documents.add(); // Create a new text frame and assign it to the variable "myTextFrame" var myTextFrame = myDocument.textFrames.add(); // Set the contents and position of the text frame myTextFrame.position = [200, 200]; myTextFrame.contents = "Hello World!"
Now, let's do some more scripting to edit the content that we just created. Lets resize the text to half the width of our document and a quarter of its height. We will create a separate script for this.
Create a new file and call it text_edit.jsx.
In this file, access our document as the active document and define the width and height as our desired width and height.
Create a reference to our text and store it in a variable.
Set the width and height of our text reference to the height and width we defined earlier.
If we run this script while our previous hello world document is still open, it should resize the text to the height and width we defined in our script.

Here's the code snippet for the text_edit.jsx script:

// Accessing active Illustrator document var docRef = app.activeDocument; // Setting desired height and width var docWidth = docRef.width/2; var docHeight = docRef.height / 4; // Accessing our "Hello World" text var frameRef = docRef.textFrames[0]; // Setting the text size frameRef.width = docWidth; frameRef.height = docHeight;
Now, modify the script so that the text will be centered in the document's center. To do this, we will calculate our document center and the dimensions of our text. This way, we can instruct our script to center the text based on its dimensions.

Here's the snippet to center our text:

// Accessing the active Illustrator document var docRef = app.activeDocument; // Calculating the center of the document var centerX = docRef.width / 2; var centerY = docRef.height / 2; // Accessing the "Hello World" text frame var frameRef = docRef.textFrames[0]; // Calculating the size of the text frame var textWidth = frameRef.width; var textHeight = frameRef.height; // Calculating new position to center the text frame var newPositionX = centerX - (textWidth / 2); var newPositionY = centerY - (textHeight / 2); // Setting the new position to center the text frame frameRef.left = newPositionX; frameRef.top = newPositionY;
Next, we will edit the text by changing its font properties: color and font family.
To set the text color to red and change the font style to bold, modify the characterAttributes of the textRange object within the textFrame. Specifically, set the fillColor property for the color and ensure the font you choose supports a bold style or select a bold variant of the font if available.

Below are the code snippets for the edits.

var desiredFont = "Impact"; // Check if the desired font exists in Illustrator's font list var fontExists = app.textFonts.getByName(desiredFont); // If the font exists, apply it to the text frame if (fontExists) { frameRef.textRange.characterAttributes.textFont = app.textFonts.getByName(desiredFont); } else { alert("Font not found."); } var newColor = new RGBColor(); newColor.red = 255; newColor.green = 0; newColor.blue = 0; frameRef.textRange.characterAttributes.fillColor = newColor;

Here is the result of our simple scripting:

This is Just the start. If you are interested in more complex stuff there are bunch of sample scripts that comes with the install and are available under presets>sample scripts , in the directory where the application is installed. Also Adobe’ official Documentation has all the needed resources to play around more with scripting.

Checkout Official Documentation for more: https://ai-scripting.docsforadobe.dev/index.html

Here are few Github Repo’s I have found with some nice scripts :

This post was last updated on: June 27, 2024