Adobe 65010248 - Illustrator CS4 - PC Manual Download Page 43

C

HAPTER

 5: Scripting with JavaScript

Accessing and referencing objects

     43

The following statements assign the name of the first graphic style in the collection to a variable. You can 
use these statements interchangeably.

var styleName = myStyles[0].name

var styleName = firstStyle.name

var styleName = app.activeDocument.graphicStyles[0].name

To get the total number of objects in a collection, use the 

length

 property:

alert ( myStyles.length );

The index of the last graphic style in the collection is 

myStyles.length-1

 (-1 because the collection 

starts the index count at 0 and the 

length

 property counts from 1):

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

Note that an expression representing the index value is enclosed in square brackets (

[]

) as well as quotes.

If you know the name of an object, you can access the object in the collections using the name surrounded 
by square brackets; for example:

var getStyle = myStyles[Ice Type];

Each element in the collection is an object of the desired type, and you can access its properties through 
the collection. For example, to get an object’s name, use the 

name

 property:

var styleName = app.activeDocument.graphicStyles[0].name;

To apply 

lastStyle

 to the first 

pageItem

 in the document, use its 

applyTo()

 method:

lastStyle.applyTo( app.activeDocument.pageItems[0] );

Creating new objects

You can use a script to create new objects. To create objects that are available from collection objects, or 

containers

, use the container object’s 

add()

 method:

var myDoc = app.documents.add()
var myLayer = myDoc.layers.add()

Some object types are not available from containers. To create an object of this type, define a variable, 
then use the 

new

 operator with an object constructor to assign an object as the value. For example, to 

create a new 

CMYKColor

 object using the variable name 

myColor

:

var myColor = new CMYKColor()

Summary of Contents for 65010248 - Illustrator CS4 - PC

Page 1: ...ADOBE ILLUSTRATOR CS4 ADOBE ILLUSTRATOR CS4 SCRIPTING GUIDE...

Page 2: ...r other countries Microsoft and Windows are either registered trademarks or trademarks of Microsoft Corporation in the United States and or other countries Apple Mac Macintosh and Mac OS are trademark...

Page 3: ...e VBScript object model 9 Executing scripts 9 Installing scripts in the Scripts menu 10 Executing scripts from the Other Scripts menu item 10 Startup scripts jsx scripts only 10 Changes since CS3 11 K...

Page 4: ...ocuments 32 4 Scripting with AppleScript 33 For more information 33 Your first Illustrator script 33 Adding features to Hello World 34 Object references 34 Obtaining objects from documents and layers...

Page 5: ...Illustrator script 48 Adding features to Hello World 49 Accessing and referencing objects 49 Obtaining objects from collections 49 Creating new objects 50 Working with selections 50 Working with text...

Page 6: ...o write short simple scripts that perform repetitive tasks for you As your scripting skills grow you can move on to more complex scripts Scripting also can enhance your creativity by quickly performin...

Page 7: ...omplete documentation of the methods and properties of JavaScript objects For information on accessing the ESTK and the Object Model Viewer see Viewing the JavaScript object model on page 8 ExtendScri...

Page 8: ...OS system drive Applications Utilities Adobe Utilities ExtendScript Toolkit CS4 2 In the ESTK choose Help Object Model Viewer 3 In the Object Model Viewer window select Adobe Illustrator CS4 Type Lib...

Page 9: ...ls Macro Visual Basic Editor 2 In the Visual Basic Editor choose Tools References 3 In the dialog that appears select the check box for Adobe Illustrator CS4 Type Library and click OK 4 Choose View Ob...

Page 10: ...le the script is executed Only files that are of one of the supported file types are displayed in the browse dialog For details see Script support in Adobe Illustrator CS4 on page 7 Startup scripts js...

Page 11: ...the Add method in the Document class Possible layouts are GridByRow GridByCol Row Column RLGridByRow RLGridByCol and RLRow for example AppleScript artboardLayout grid by row JavaScript DocumentArtboa...

Page 12: ...type FiltersPreservePolicy which defines whether to preserve appearance and editability of filters when saving to FXG Available options are ExpandFilters RasterizeFilters and KeepFiltersEditable Apple...

Page 13: ...example AppleScript alternate glyphs jis90 JavaScript AlternateGlyphsForm JIS90 VBScript AiAlternateGlyphsForm aiJIS90Form New properties were added to the Application class build number and locale f...

Page 14: ...t swatch group 1 of document 1 JavaScript app activeDocument swatchGroups 0 VBScript App ActiveDocument SwatchGroups 1 A new collection SwatchGroups contains all SwatchGroup objects in a document It h...

Page 15: ...cript RasterizationColorModel GRAYSCALE VBScript AiRasterizationColorModel aiGrayscale A new type AntiAliasingMethod controls the type of rasterization method used in the rasterization none art optimi...

Page 16: ...he ESTK Scripters need to be very careful about variable initialization and namespace conflict when pushing a batch of Illustrator scripts repeatedly for execution in Illustrator via the ESTK in one I...

Page 17: ...ng with the application object Note that the layer and group item classes can contain nested objects of the same class which can in turn contain additional nested objects In addition to this applicati...

Page 18: ...rties methods and object are in a monospaced font Top level containing objects Use these objects to access global information about the Illustrator application or an individual document Application Th...

Page 19: ...document object s methods allow your scripts to act on the document for example Save to an Illustrator file or save as the various supported file formats Activate or close a document Print the documen...

Page 20: ...mple to create a new path item object AppleScript set myPathItem to make new path item in current document JavaScript var myPathItem activeDocument pathItems add VBScript Set myPathItem appRef ActiveD...

Page 21: ...ge 0 0 to 255 0 for the each of the three individual color values The CMYK color object using the percentage values 0 0 through 100 0 for each of the four individual color values The grayscale color o...

Page 22: ...ct which is specified using the text frame object s text path property The text path defines the text frame s position and orientation horizontal or vertical on the artboard while the text frame objec...

Page 23: ...property Text styles Text style elements like font capitalization and justification are represented by paragraph attribute and character attribute objects These attribute objects are properties of th...

Page 24: ...to the Symbols panel for the target document A symbol item is an instance of a symbol object in a document Each symbol item is linked to its symbol definition so changing the definition of a symbol up...

Page 25: ...or details search for interapplication messaging or JavaScript messaging framework in JavaScript Tools Guide VBScript In VBScript there are several ways to create an instance of Illustrator CreateObje...

Page 26: ...cation appRef Quit Working with objects Getting the frontmost document or layer To refer to the selected document use the application object s current document property in AppleScript or the active do...

Page 27: ...r path item 1 in layer 1 in document 1 JavaScript To refer to a path items object in a document documents 0 pathItems 1 To refer to a path items object in a layer documents 0 layers 0 pathItems 0 CMYK...

Page 28: ...object Selecting art items You can select an art object like graph items mesh items raster items and symbol items by setting its selected property to true In AppleScript selected is a property of the...

Page 29: ...r details see JavaScript Tools Guide Em space units Values that use em units instead of points are measured in thousandths of an em An em is proportional to the current font size For example in a 6 po...

Page 30: ...efines the lower left corner of the printable region of the document as a fixed point Also each page item object has width and height properties The maximum value allowed for the width or height of a...

Page 31: ...a script is interacting with an application a dialog brings the execution of the script to a halt until the dialog is dismissed This can be a serious problem in an automation environment where there i...

Page 32: ...full capabilities of Illustrator printing some of which may not be accessible through the application s user interface Illustrator supports at most one print session at a time because of limits in th...

Page 33: ...st 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 contai...

Page 34: ...trator returns object references by index position or name For example this is a reference to the first path in layer 2 path item 1 of layer 2 of document 1 An object s index position may change when...

Page 35: ...need to access at a later time as there is no guarantee you are accessing the objects you expect when accessing them by index Obtaining objects from documents and layers This script references an obj...

Page 36: ...dth 100 set myAreaText to make new text frame in current document with properties kind point text contents Text Frame 1 Threaded frames As in the Illustrator application you can thread area text frame...

Page 37: ...following script specifies three pairs of x y coordinates to create a path with three points tell application Adobe Illustrator set docRef to make new document set stroked to true so we can see the pa...

Page 38: ...following script sample creates a path with three points by combining the entire path property with a path point object tell application Adobe Illustrator set docRef to make new document set stroked t...

Page 39: ...t corner of the of the rectangle is inset 4 inches 288 points from the bottom of the page and 5 inches 360 points from the left edge of the page The lower left corner of the rectangle is inset 1 inch...

Page 40: ...s 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 H...

Page 41: ...at 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 param...

Page 42: ...plication object use the app global variable The following two statements appear identical to the JavaScript engine var frontMostDocument activeDocument var frontMostDocument app activeDocument Access...

Page 43: ...t in the collections using the name surrounded by square brackets for example var getStyle myStyles Ice Type Each element in the collection is an object of the desired type and you can access its prop...

Page 44: ...me of a specific type in JavaScript use the textFrames method whose name corresponds to the text frame s type for example var rectRef docRef pathItems rectangle 700 50 100 100 use the areaText method...

Page 45: ...hod of the pathItems object The following script specifies three pairs of x y coordinates to create a path with three points var myDoc app activeDocument var myLine myDoc pathItems add set stroked to...

Page 46: ...0 320 newPoint2 pointType PointType CORNER Combining path point types The following script sample creates a path with three points var myDoc app activeDocument var myLine myDoc pathItems add myLine st...

Page 47: ...The left edge is 2 inches 144 points from the left edge of the page The rectangle is 1 inch 72 points wide and 3 inches 216 points long Creating a polygon Consider the following sample var myDocument...

Page 48: ...y programming language is displaying the message Hello World Follow these steps 1 Start any text editor for example Notepad 2 Type the following code Rem Hello World Set appRef CreateObject Illustrato...

Page 49: ...Left 0 2 Run the script Accessing and referencing objects When you write a script you must first decide which file or Document the script should act on Through the Application object the script can c...

Page 50: ...eateObject Illustrator CMYKColor Working with selections When the user makes a selection in a document the selected objects are stored in the document s selection property To access all selected objec...

Page 51: ...nts Add Set myPathItem1 myDoc PathItems Rectangle 244 64 82 76 Set myTextFrame1 myDoc TextFrames AreaText myPathItem1 myTextFrame1 Contents This is two text frames linked together as one story with te...

Page 52: ...erty as an array of page coordinates in the format Array x y If all three properties of a PathPoint object have the same coordinates and the properties of the next PathPoint in the line are equal to e...

Page 53: ...nd other characteristics like the number of sides in a polygon Remember The scripting engine processes all measurements and page coordinates as points For details see Measurement units on page 29 x an...

Page 54: ...The property uses the aiTextOrientation enumeration which has two possible values aiHorizontal and aiVertical To find the numeral values of enumerations use either of the following The object browser...

Page 55: ...em positioning 29 printing 32 E em space units 29 enumeration values 54 executing scripts 9 10 ExtendScript file extension 7 F file extensions for valid scripts 7 fixed points 29 fixed rectangles 30 f...

Page 56: ...out 21 settings options 32 Q Qs unit conversion 29 quitting Illustrator 25 R rectangles creating 53 fixed 30 references object See object references right direction 31 S script examples creating a cur...

Page 57: ...V variables deleting 24 using 24 VBScript enumeration values 54 file extension 7 naming conventions 18 type library 9 versions of Illustrator specifying 25 visible bounds 30 W width maximum value all...

Reviews: