Adobe 27510753 Scripting Guide - Page 22

Operators, Methods

Page 22 highlights

14 Scripting Basics Adobe InDesign CS2 Scripting Guide for(myCounter = 0; myCounter < app.activeDocument.pages.item(0).pageItems.length; myCounter ++){ var myPageItem = app.activeDocument.pages.item(0).pageItems.item(myCounter); var myPageItemType = myPageItem.getElements()[0].constructor.name; alert(myPageItemType); } Operators Operators use variables or values to perform calculations (addition, subtraction, multiplication, and division) and return a value. For example: MyWidth/2 returns a value equal to half of the content of the variable myWidth. You can also use operators to perform comparisons (equal to (=), not equal to(), greater than(>), or less than( myHeight returns the value true (or 1) if myWidth is greater than myHeight, or false (0), if it isn't. All the scripting languages provide additional utility operators. In AppleScript and VBScript, the ampersand (&) concatenates (or joins) two strings: "Pride " & "and Prejudice" returns the string: "Pride and Prejudice" In JavaScript, use a plus sign (+) to join the two strings: "Pride " + "and Prejudice" //returns the string: "Pride and Prejudice" Methods Methods (sometimes called commands or events) are the verbs of scripting. They're the parts of the script that make more-complex things happen than can be achieved simply by assigning property values. The type of the object you're working with determines the quantity and type of methods that you can use to manipulate the object. In AppleScript, the make command creates new objects, while set assigns values to properties or creates object variables. In VBScript, the Add method creates new objects, the Set statement assigns InDesign objects to VBScript variables, and the equal sign (=) assigns property values (in Visual Basic.NET, you can omit the Set). In JavaScript, the add() method creates new objects, and the equal sign (=) assigns objects to a variable. If you use more than one scripting language, it's easy to get confused as you switch among them. When you're using AppleScript, remember that the equal sign is used only in logical statements (If) and that set is used for all variable assignments. When you're using VBScript or Visual Basic (versions other than VB.NET), Set is used only when you are creating an object variable. In JavaScript, the equal sign is used for assigning values to a variable; use a double equal sign (==) to compare objects. Parameters are sometimes required; for example, when you create a group, InDesign needs to know which objects to include in the new group. Other parameters are optional; for example, when you create a new document, you can specify that InDesign base the new document on a document preset, but it's not required.

  • 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
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184

14
Scripting Basics
Adobe InDesign CS2 Scripting Guide
for(myCounter = 0; myCounter < app.activeDocument.pages.item(0).pageItems.length; myCounter ++){
var myPageItem = app.activeDocument.pages.item(0).pageItems.item(myCounter);
var myPageItemType = myPageItem.getElements()[0].constructor.name;
alert(myPageItemType);
}
Operators
Operators use variables or values to perform calculations (addition, subtraction, multiplication, and division)
and return a value. For example:
MyWidth/2
returns a value equal to half of the content of the variable
myWidth
.
You can also use operators to perform comparisons (equal to (=), not equal to(<>), greater than(>), or less
than(<)). For example:
MyWidth > myHeight
returns the value true (or 1) if
myWidth
is greater than
myHeight
, or false (0), if it isn’t.
All the scripting languages provide additional utility operators. In AppleScript and VBScript, the ampersand
(&) concatenates (or joins) two strings:
"Pride " & "and Prejudice"
returns the string:
"Pride and Prejudice"
In JavaScript, use a plus sign (+) to join the two strings:
"Pride " + "and Prejudice"
//returns the string: "Pride and Prejudice"
Methods
Methods (sometimes called
commands
or
events
) are the verbs of scripting. They’re the parts of the script that
make more-complex things happen than can be achieved simply by assigning property values. The type of
the object you’re working with determines the quantity and type of methods that you can use to manipulate
the object.
In AppleScript, the
make
command creates new objects, while
set
assigns values to properties or creates
object variables. In VBScript, the
Add
method creates new objects, the Set statement assigns InDesign
objects to VBScript variables, and the equal sign (=) assigns property values (in Visual Basic.NET, you can omit
the
Set
). In JavaScript, the
add()
method creates new objects, and the equal sign (=)
assigns objects to a
variable.
If you use more than one scripting language, it’s easy to get confused as you switch among them. When
you’re using AppleScript, remember that the equal sign is used only in logical statements (
If
) and that
set
is
used for all variable assignments. When you’re using VBScript or Visual Basic (versions other than VB.NET),
Set
is used only when you are creating an object variable. In JavaScript, the equal sign is used for assigning values
to a variable; use a double equal sign (==) to compare objects.
Parameters are sometimes required; for example, when you create a group, InDesign needs to know which
objects to include in the new group. Other parameters are optional; for example, when you create a new
document, you can specify that InDesign base the new document on a document preset, but it’s not required.