Sub topic 13
Automating Flat Setup with a Script
Assume you can navigate Illustrator, create an artboard, and work with paths, fills, and strokes. You should also know the garment parts used in a flat, such as an outline, stitching, and annotations.
A technical flat usually begins with the same setup: an open Illustrator document, a named artboard, and layers for different types of drawing. Repeating this setup manually is slow and makes files inconsistent. Illustrator scripting lets you automate repeated actions with a small JavaScript file.
What an Illustrator script does
An Illustrator script is a text file containing commands that control Illustrator. ExtendScript is Adobe's JavaScript-based scripting language for applications such as Illustrator. Save the file with a .jsx extension, then run it from File > Scripts > Other Script.
A setup script can:
- Create standard layers.
- Name layers consistently.
- Add guides or basic construction paths.
- Apply standard colors and stroke settings.
- Prepare a document before you draw.
The script below assumes that an Illustrator document is already open.
// ExtendScript for Illustrator (.jsx): create the standard flat layers
var doc = app.activeDocument;
var names = ["Annotations", "Details", "Stitching", "Outline", "Croquis"];
for (var i = 0; i < names.length; i++) {
var layer = doc.layers.add();
layer.name = names[i];
}The variable doc stores the active document. The names array stores the layer names. The for loop repeats the same two commands for every name.
Each pass creates a layer and assigns its name. The order matters. The last name in the array ends up above the earlier layers, so Annotations is placed above Details, Stitching, Outline, and Croquis.
Choose a stable layer convention
A flat should separate visual information by purpose. A useful structure is:
| Layer | Typical content |
|---|---|
| Croquis | Faint body or proportion reference |
| Outline | Main garment silhouette |
| Stitching | Seams, topstitching, and construction lines |
| Details | Zippers, buttons, pockets, collars, and hardware |
| Annotations | Measurements, callouts, and production notes |
Keep the same order across garments and collections. A pattern maker can then find construction information quickly, and you can hide reference or annotation layers before exporting a presentation view.
Avoid common script errors
Run the script only after opening or creating a document. If no document is active, app.activeDocument causes an error.
A second run creates duplicate layers. This can be useful for testing, but it is usually undesirable in production. A safer version checks whether a layer already exists before adding it.
// ExtendScript for Illustrator (.jsx): create layers only when they do not exist
var doc = app.activeDocument;
var names = ["Annotations", "Details", "Stitching", "Outline", "Croquis"];
for (var i = 0; i < names.length; i++) {
var layerName = names[i];
var exists = false;
for (var j = 0; j < doc.layers.length; j++) {
if (doc.layers[j].name == layerName) {
exists = true;
break;
}
}
if (!exists) {
var layer = doc.layers.add();
layer.name = layerName;
}
}This version compares each requested name with the names already in the document. It adds a layer only when no match is found. The comparison is case-sensitive, so Outline and outline are treated as different names.
Exercise
Why should a setup script check for existing layers before creating new ones?
Show answer
Without the check, each run creates duplicate layers, which makes the document harder to navigate and can cause artwork to be placed on the wrong layer.