
C
HAPTER
5: Scripting with JavaScript
Working with methods in JavaScript
41
Adding features to “Hello World”
Next, we create a new script that makes changes to the Illustrator document you created with your first
script. Our second script demonstrates how to:
➤
Get the active document.
➤
Get the width of the active document.
➤
Resize the text frame to match the document’s width.
If you already closed the Illustrator document, run your first script again to create a new document, before
proceeding with this exercise.
Follow these steps:
1.
Choose File > New in your text editor, to create a new script.
2.
Enter the following code:
var docRef = app.activeDocument;
var docWidth = docRef.width
var frameRef = docRef.textFrames[0]
frameRef.width = docWidth
3.
Run the script.
Working with methods in JavaScript
When you work with methods that have multiple parameters, you may omit optional parameters at the
end of the parameter list, but you may not omit parameters in the middle of the list. If you do not want to
specify a particular parameter in the middle of the list, you must insert the value
undefined
to use the
parameter’s default value. For example, the following definition describes the
rotate()
method for an art
object.
rotate
(angle
[,changePositions]
[,changeFillPatterns]
[,changeFillGradients]
[,changeStrokePattern]
[,rotateAbout])
In the definition, taken from
Adobe Illustrator CS4 Scripting Reference: JavaScript
, optional parameters are
enclosed in square brackets (
[]
).
To rotate the object 30 degrees and change the
fillGradients
, you would use the following script
statement:
myObject.rotate(30, undefined, undefined, true);
You need to specify
undefined
for the
changePositions
and
changeFillPatterns
parameters. You do
not have to specify anything for the two optional parameters following
changeFillGradients
, since they
are at the end of the parameter list.