background image

GettingStarted

 Illustrator JavaScript     4

5. If no document is open, set the message to reflect that case.

else
{

msg = "No document Open.";

}

6. Print the msg.

alert ( msg );

7. Save the file.

To run this example:

1. Launch Illustrator.

2. Open a document with some objects in it or create a document and create some objects in it.

3. Run the script, as described in 

Executing JavaScript Scripts

 above. 

The alert box appears and shows the types of all the page items.

Example 3: Accessing Objects in Collections

Often you want to access particular objects, or objects of particular types, rather than all items in a page or 
selected items. For example, you might want to access and manipulate all graph items or all raster items in 
a page. Different types of objects in a document are collected into collection objects for those types. A 
collection object contains an array of the objects which you can access by index number or by name.

The following example shows how to access objects from their respective collections

1. Open a new blank document in your text editor, and save it as 

Example3.js

.

2. Get all brushes in the brushes collection in the document. 

var myBrushes = app.activeDocument.brushes;

3. All array references are zero-based; that is, the first element of the collection has the index [0]. To access 

the first brush in this collection:

var firstBrush = myBrushes[0];

4. To get the total number of objects, use the 

length

 property:

alert ( "Total Brushes in this document: " + myBrushes.length ) ; 

5. Similarly, let’s access all the graphic styles in a document through the graphicStyles collection object:

var myStyles = app.activeDocument.graphicStyles;

6. If you know the name of an object, you can access it in the collection using the name. Otherwise, you 

can use its 0-based index. For example:

var getStyle = myStyles[’Ice Type’];

7. Index of the last style would be myStyles.length-1.

var lastStyle = myStyles[ myStyles.length - 1 ];

Reviews: