Adobe InDesign CS4 Scripting Tutorial
Scripting and the InDesign object model
12
Converting values from one type to another
All 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 you can enter them in
text or display them in dialog boxes) or converting strings to numbers (so you can use them to set a point
size or page location). See the examples below.
Apple-
Script
--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"
JavaScript
//To convert from a number to a string:
myNumber = 2;
myString = my "";
//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;
Value type
What it is
Example
Boolean
Logical True or False.
True
Integer
Whole numbers (no decimal
points). Integers can be positive
or negative. In VBScript, you can
use the long data type for
integers. In AppleScript, you
also can use the fixed or long
data types for both integers and
real numbers.
14
Double (VBScript),
fixed or real
(AppleScript),
floating point
(JavaScript)
A high-precision number that
can contain a decimal point.
13.9972
String
A series of text characters.
Strings appear inside (straight)
quotation marks.
"I am a string"
Array (VBScript,
JavaScript)
or list (AppleScript)
A list of values (the values can
be any type).
AppleScript:
{"0p0", "0p0", "16p4", "20p6"}
VBScript:
Array("0p0",
"0p0",
"16p4",
"20p6")
JavaScript:
["0p0",
"0p0",
"16p4",
"20p6"]