Adobe 65010248 Scripting Guide - Page 43

Creating new objects, in the document, use its

Page 43 highlights

CHAPTER 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()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

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()