Adobe 27510753 Scripting Guide - Page 20

Array variables, Converting values from one type to another, AppleScript

Page 20 highlights

12 Scripting Basics Adobe InDesign CS2 Scripting Guide Like VB.NET, JavaScript does not require Set when you assign values to variables, regardless of the type of the variable: var myDocument = app.documents.add(); var myString = "X"; Array variables AppleScript, VBScript, and JavaScript all support arrays, which is a variable type that is a list of values. In AppleScript, an array is called a list. AppleScript set myArray to {1, 2, 3, 4} VBScript JavaScript myArray = Array(1, 2, 3, 4) Rem In Visual Basic.NET: myArray = New Double (1, 2, 3, 4) myArray = [1, 2, 3, 4]; To refer to an item in an array, refer to the item by its index in the array. The first item in an array in VBScript and JavaScript is item 0; in AppleScript, the first item in an array is item 1. AppleScript VBScript JavaScript set myFirstArrayItem to item 1 of myArray myFirstArrayItem = myArray(0) myFirstArrayItem = myArray[0]; Note: The Visual Basic Option Base statement can be used to set the first item of an array to item 1. The examples in this book and on your InDesign CD assume that the first item in an array is item 0, not item 1, because that is the default. If you have set the Option Base to 1, you must adjust all the array references in the example scripts accordingly. Arrays can include other arrays, as shown in the following examples. AppleScript VBScript JavaScript set myArray to {{0, 0}, {72, 72}} myArray = Array(Array(0,0), Array(72, 72)) Rem In Visual Basic.NET: myArray = New Array(New Double(0,0), NewDouble (0,0)) myArray = [[0,0], [72,72]]; Converting values from one type to another All the scripting languages supported by InDesign provide ways to convert variable values from one type to another. The most common conversions involve converting numbers to strings (so that you can enter them in text or display them in dialog boxes) or converting strings to numbers (so that you can use them to set a point size or other numeric values). AppleScript --To convert from a number to a string: set myNumber to 2 set myString to (myNumber as string) --To convert from a string to a number: set myString to "2" set myNumber to (myString as integer) --if your string contains a decimal value, use "as real" rather than "as integer"

  • 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

12
Scripting Basics
Adobe InDesign CS2 Scripting Guide
Like VB.NET, JavaScript does not require
Set
when you assign values to variables, regardless of the type of the
variable:
var myDocument = app.documents.add();
var myString = "X";
Array variables
AppleScript, VBScript, and JavaScript all support
arrays
, which is a variable type that is a list of values. In
AppleScript, an array is called a
list
.
AppleScript
set myArray to {1, 2, 3, 4}
VBScript
myArray = Array(1, 2, 3, 4)
Rem In Visual Basic.NET: myArray = New Double (1, 2, 3, 4)
JavaScript
myArray = [1, 2, 3, 4];
To refer to an item in an array, refer to the item by its index in the array. The first item in an array in VBScript
and JavaScript is item 0; in AppleScript, the first item in an array is item 1.
AppleScript
set myFirstArrayItem to item 1 of myArray
VBScript
myFirstArrayItem = myArray(0)
JavaScript
myFirstArrayItem = myArray[0];
Note:
The Visual Basic
Option Base
statement can be used to set the first item of an array to item 1. The ex-
amples in this book and on your InDesign CD assume that the first item in an array is item 0, not item 1,
because that is the default. If you have set the Option Base to 1, you must adjust all the array references
in the example scripts accordingly.
Arrays can include other arrays, as shown in the following examples.
AppleScript
set myArray to {{0, 0}, {72, 72}}
VBScript
myArray = Array(Array(0,0), Array(72, 72))
Rem In Visual Basic.NET: myArray = New Array(New Double(0,0),
NewDouble (0,0))
JavaScript
myArray = [[0,0], [72,72]];
Converting values from one type to another
All the scripting languages supported by InDesign provide ways to convert variable values from one type to
another. The most common conversions involve converting numbers to strings (so that you can enter them in
text or display them in dialog boxes) or converting strings to numbers (so that you can use them to set a point
size or other numeric values).
AppleScript
--To convert from a number to a string:
set myNumber to 2
set myString to (myNumber as string)
--To convert from a string to a number:
set myString to "2"
set myNumber to (myString as integer)
--if your string contains a decimal value, use "as real" rather than "as integer"