
40
5
Scripting with JavaScript
This chapter uses script examples and explanations to help you to become familiar with Illustrator
scripting using JavaScript.
For more information
Several extended sample scripts are in the
/Scripting/Sample
Scripts
folder in your Illustrator CS4
installation directory.
For information about individual classes, objects, properties, methods, and parameters, as well as script
samples that demonstrate how to use many of these items, see
Adobe Illustrator CS4 Scripting Reference:
JavaScript
, in the
/Scripting/Documentation
folder in your Illustrator CS4 installation directory. You also
can use the Illustrator dictionary, which you access from the Object Model Viewer in the ESTK. For
information on using the ExtendScript Toolkit and the Object Model Viewer, see
“Viewing the JavaScript
object model” on page 8
or
JavaScript Tools Guide
.
If you do not understand the concepts and terms used in this chapter, read
Adobe Introduction to Scripting
.
Your first Illustrator script
The traditional first project in any programming language is displaying the message “Hello World!” In this
example, you create a new Illustrator document, then add a text frame containing this message. Follow
these steps:
1.
Using any text editor (including Adobe
InDesign
®
or the ESTK), enter the following text:
//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!"
For information on locating the ExtendScript Toolkit, see
“Viewing the JavaScript object model” on
page 8
.
2.
To test the script, do either of the following:
➣
If you are using the ESTK, select Adobe Illustrator CS4 from the drop-down list in the upper-left
corner, select Yes to start Illustrator, then choose Debug > Run in the ESTK to run the script.
➣
If you are using a different text editor than the ESTK, save the file as text-only in a folder of your
choice, using the file extension
.jsx
, then start Illustrator. In Illustrator, choose File > Scripts >
Other Scripts, and navigate to and run your script file.
T
IP
:
To add the script to the Illustrator Scripts menu (File > Scripts), save the script in the Scripts folder. The
script will appear on the menu the next time you start Illustrator. For details, see
“Installing scripts in the
Scripts menu” on page 10
.