Getting started with scripting in adobe illustrator using 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.
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 |
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:
/Illustrator CC/Presets
folder in your Illustrator CC installation directory.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.
helloWorld.jsx
.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.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!"
text_edit.jsx
.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;
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;
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