Adobe 27510753 Scripting Guide - Page 19

Variable names, Assigning values to variables, In VBScript or Visual Basic versions other than VB.NET

Page 19 highlights

Adobe InDesign CS2 Scripting Guide Scripting Basics 11 The same operation in JavaScript is more similar to VBScript than it is to AppleScript (in InDesign JavaScript, app is a reference to the InDesign application). To refer to an existing frame: var myTextFrame = app.documents[0].spreads[0].textFrames[0]; //or var myTextFrame = app.documents.item(0).spreads.item(0).textFrames.item(0); or to assign a variable as you create a frame: var myTextFrame = app.documents[0].spreads[0].textFrames.add(); //or var myTextFrame = app.documents.item(0).spreads.item(0).textFrames.add(); Note: In JavaScript, all variables that are not preceded by var are considered global by default-that is, they are not bound to a specific function; var is not required, but we recommend that you use var in any script with more than a single function. In AppleScript and VBScript, variables are local unless specifically defined as global variables. This means that the variables do not persist outside the function in which they are created. In general, in any of the languages, it's best to use local variables and pass specific values to functions/ subroutines/handlers rather than defining and using global variables. In JavaScript, you define a variable as a local variable using var, as shown in the preceding example. To specify that several variables in a JavaScript function are local variables, you can list them at the beginning of the function; for example: function myFunction(){ var myNumber, myString; } Variable names Try to use descriptive names for your variables, such as firstPage or corporateLogo, rather than x or c. This makes your script easier to read. Longer names do not affect the execution speed of the script. Variable names must be a single word, but you can use internal capitalization (such as myFirstPage) or underscore characters (my _ first _ page) to create more readable names. Variable names cannot begin with a number, and they can't contain punctuation or quotation marks. You might also want to give your variable names a prefix so that they'll stand out from the objects, commands, and keywords of your scripting system. All of the variables used in this book, for example, begin with my. Assigning values to variables In AppleScript, use set to assign any value type to a variable: set myDocument to active document set myString to "X" In VBScript or Visual Basic versions other than VB.NET, use Set to assign object references to variables, but not to string, array, or number variable types: Set myDocument = myInDesign.Documents.Add myString = "X" In VB.NET, you do not need to use Set for object variables: myDocument = myInDesign.Documents.Add myString = "X"

  • 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

Adobe InDesign CS2 Scripting Guide
Scripting Basics
11
The same operation in JavaScript is more similar to VBScript than it is to AppleScript (in InDesign JavaScript,
app
is a reference to the InDesign application). To refer to an existing frame:
var myTextFrame = app.documents[0].spreads[0].textFrames[0];
//or
var myTextFrame = app.documents.item(0).spreads.item(0).textFrames.item(0);
or to assign a variable as you create a frame:
var myTextFrame = app.documents[0].spreads[0].textFrames.add();
//or
var myTextFrame = app.documents.item(0).spreads.item(0).textFrames.add();
Note:
In JavaScript, all variables that are not preceded by
var
are considered global by default—that is, they
are not bound to a specific function;
var
is not required, but we recommend that you use
var
in any
script with more than a single function. In AppleScript and VBScript, variables are local unless specifi-
cally defined as global variables. This means that the variables do not persist outside the function in
which they are created.
In general, in any of the languages, it’s best to use local variables and pass specific values to functions/
subroutines/handlers rather than defining and using global variables. In JavaScript, you define a variable as a
local variable using
var
, as shown in the preceding example. To specify that several variables in a JavaScript
function are local variables, you can list them at the beginning of the function; for example:
function myFunction(){
var myNumber, myString;
}
Variable names
Try to use descriptive names for your variables, such as
firstPage
or
corporateLogo
, rather than
x
or
c
.
This makes your script easier to read. Longer names do not affect the execution speed of the script.
Variable names must be a single word, but you can use internal capitalization (such as
myFirstPage
) or
underscore characters (
my _ first _ page
) to create more readable names. Variable names cannot begin
with a number, and they can’t contain punctuation or quotation marks.
You might also want to give your variable names a prefix so that they’ll stand out from the objects,
commands, and keywords of your scripting system. All of the variables used in this book, for example, begin
with
my
.
Assigning values to variables
In AppleScript, use
set
to assign any value type to a variable:
set myDocument to active document
set myString to "X"
In VBScript or Visual Basic versions other than VB.NET, use
Set
to assign object references to variables, but not
to string, array, or number variable types:
Set myDocument = myInDesign.Documents.Add
myString = "X"
In VB.NET, you do not need to use
Set
for object variables:
myDocument = myInDesign.Documents.Add
myString = "X"