Scripting in Illustrator: a beginner's guide

Like many Creative Suite 5 applications, Illustrator is heavily scriptable – but creating scripts can seem daunting to the novice. Here I’ll take you line by line through the script we’re using in this tutorial, to help understand how it works. You can find the script Random_Fill_With_Swatch_Time_Curve.jsx in our Download Zone, and view it in any text editor.

if ( app.documents.length > 0 && app.activeDocument.pathItems.length > 0 ){ 

This is a standard line you would use to kick off most scripts. It’s needed to simply check to see that there is a document open and that at least one pathItem is selected.

thisDoc = app.activeDocument; 

This assigns a variable (thisDoc) to the active document, which is used as a shortcut name later on.

for ( i = 0; i < thisDoc.selection.length; i++ ){

This is a looping FOR statement, which we will use to cycle through all selected objects.

selectedObject=thisDoc.selection[i];

This will assign an array for each object.

if(selectedObject.typename==”PathItem” && selectedObject.closed==true){

This is another standard line of error checking which is used to simply check for the correct class name and that it is a closed path which can be filled.

selectedObject.filled = true;

Set fill settings as TRUE

swatchIndex = Math.round((thisDoc.swatches.length - 3) * Math.random() + 2);

This is a random number generator. The numbers correspond to swatches. It also removes 0 and 1 from the possible random numbers generated, as these are default black and white swatches that we don’t want to use.

selectedObject.fillColor = thisDoc.swatches[ swatchIndex ].color; 

This then applies the random swatch colour from the corresponding number to the object.

         }

    }

}

These close off the FOR and IF statements we have added above.

Simon C Page

Who: Simon C Page is a self-taught graphic designer from the UK. He loves many aspects of design but his main passions are typography art, illustration and geometric design.

“I would find it hard to choose a favourite,” he says “and often I will try and combine these creative fields in to my design work.”

Simon likes his work to evolve naturally and not stay too static. He often challenges myself with new self-initiated works for his website, regularly showing variations in his creative process.
Contact: simoncpage.co.uk
Software: Adobe Illustrator CS5, Adobe InDesign CS5
Time to complete: 45 minutes
Download Zone: Download the resources for this tutorial here. (Free registration required)