Adobe 27510753 Scripting Guide - Page 21

Finding the value type of a variable, VBScript, AppleScript

Page 21 highlights

Adobe InDesign CS2 Scripting Guide Scripting Basics 13 VBScript Rem To convert from a number to a string: myNumber = 2 myString = cstr(myNumber) Rem To convert from a string to an integer: myString = "2" myNumber = cInt(myString) Rem If your string contains a decimal value, use "cDbl" rather than "cInt": myNumber = cDbl(myString) JavaScript //To convert from a number to a string: myNumber = 2; myString = myNumber + ""; //To convert from a string to an integer: myString = "2"; myNumber = parseInt(myString); //If your string contains a decimal value, use "parseFloat" rather than "parseInt": myNumber = parseFloat(myString); //You can also convert strings to numbers using the following: myNumber = +myString; Finding the value type of a variable Sometimes, your scripts must make decisions based on the value type of an object. If you're working on a script that operates on a text selection, for example, you might want that script to stop if the type of the selection is a page item. All of the scripting languages allow you to determine the type of a variable. AppleScript -- Given a variable of unknown type, "myMysteryVariable"... set myType to class of myMysteryVariable --myType will be an AppleScript type (e.g., rectangle) VBScript Rem Given a variable of unknown type, "myMysteryVariable"... myType = TypeName(myMysteryVariable) Rem myType will be a string corresponding to the variable type (e.g., "Rectangle") JavaScript //Given a variable of unknown type, "myMysteryVariable"... var myType =typeof (myMysteryVariable); //If the variable is an object, "typeof" will return "object". //To get the specific type of the object, add the following: if (myType == "object"){ myType = myMysteryVariable.constructor.name; } //myType will be a string corresponding to the JavaScript type (e.g., "Rectangle") When you are iterating through a collection, JavaScript insists that the type of the item is the type of the collection. When you iterate through a pageItems collection, for example, the type of each item will be PageItem, rather than Rectangle, Oval, Polygon, GraphicLine, or TextFrame. To get the specific type of an item when you are iterating through a collection of unlike items, use the getElements() method, as shown in the following example:

  • 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
13
VBScript
Rem To convert from a number to a string:
myNumber = 2
myString = cstr(myNumber)
Rem To convert from a string to an integer:
myString = "2"
myNumber = cInt(myString)
Rem If your string contains a decimal value, use "cDbl" rather than "cInt":
myNumber = cDbl(myString)
JavaScript
//To convert from a number to a string:
myNumber = 2;
myString = myNumber + "";
//To convert from a string to an integer:
myString = "2";
myNumber = parseInt(myString);
//If your string contains a decimal value, use "parseFloat" rather than "parseInt":
myNumber = parseFloat(myString);
//You can also convert strings to numbers using the following:
myNumber = +myString;
Finding the value type of a variable
Sometimes, your scripts must make decisions based on the value type of an object. If you’re working on a
script that operates on a text selection, for example, you might want that script to stop if the type of the
selection is a page item. All of the scripting languages allow you to determine the type of a variable.
AppleScript
-- Given a variable of unknown type, "myMysteryVariable"...
set myType to class of myMysteryVariable
--myType will be an AppleScript type (e.g., rectangle)
VBScript
Rem Given a variable of unknown type, "myMysteryVariable"...
myType = TypeName(myMysteryVariable)
Rem myType will be a string corresponding to the variable type (e.g., "Rectangle")
JavaScript
//Given a variable of unknown type, "myMysteryVariable"...
var myType =typeof (myMysteryVariable);
//If the variable is an object, "typeof" will return "object".
//To get the specific type of the object, add the following:
if (myType == "object"){
myType = myMysteryVariable.constructor.name;
}
//myType will be a string corresponding to the JavaScript type (e.g., "Rectangle")
When you are iterating through a collection, JavaScript insists that the type of the item is the type of the
collection. When you iterate through a
pageItems
collection, for example, the type of each item will be
PageItem
, rather than
Rectangle
,
Oval
,
Polygon
,
GraphicLine
, or
TextFrame
. To get the specific type
of an item when you are iterating through a collection of unlike items, use the
getElements()
method, as
shown in the following example: