Adobe 65009333 Scripting Guide - Page 11

Variables, Logical True or False

Page 11 highlights

Getting Started Scripting terminology and the InCopy object model 11 Value Type Boolean Integer Number String Array What it is Example Logical True or False True Whole numbers (no decimal 14 points). Integers can be positive or negative. A high-precision number that can 13.9972 contain a decimal point. A series of text characters. Strings appear inside (straight) quotation marks. "I am a string" A list of values (the values can be any type). ["0p0", "0p0", "16p4", "20p6"] Converting values from one type to another JavaScript provides ways to convert variable values from one type to another. Most commonly, numbers are converted to strings (so you can enter them in text or display them in dialogs) or strings are converted to numbers (so you can use them to set a point size or page location). //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; Variables A variable is a container for a value. They are called "variables" because the values they contain might change. A variable might hold a number, a string of text, or a reference to an InCopy object. Variables have names, and you refer to a variable by its name. To put a value into a variable, you assign the data to the variable. In all examples and tutorial scripts that come with InCopy, all variables start with my. This enables you to easily differentiate variables we created in a script from scripting-language terms. Assigning a value to a variable Assigning values or strings to variables is fairly simple, as shown below: var myNumber = 10; var myString = "Hello, World!"; var myTextFrame = myDocument.pages.item(0).textFrames.add(); NOTE: In JavaScript, all variables not preceded by var are considered global by default; that is, they are not bound to a specific function. While var is not required, we recommend you use it in any script with more than one function.

  • 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

Getting Started
Scripting terminology and the InCopy object model
11
Converting values from one type to another
JavaScript provides ways to convert variable values from one type to another. Most commonly, numbers
are converted to strings (so you can enter them in text or display them in dialogs) or strings are converted
to numbers (so you can use them to set a point size or page location).
//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;
Variables
A
variable
is a container for a value. They are called “variables” because the values they contain might
change. A variable might hold a number, a string of text, or a reference to an InCopy object. Variables have
names, and you refer to a variable by its name. To put a value into a variable, you
assign
the data to the
variable.
In all examples and tutorial scripts that come with InCopy, all variables start with
my
. This enables you to
easily differentiate variables we created in a script from scripting-language terms.
Assigning a value to a variable
Assigning values or strings to variables is fairly simple, as shown below:
var myNumber = 10;
var myString = "Hello, World!";
var myTextFrame = myDocument.pages.item(0).textFrames.add();
N
OTE
:
In JavaScript, all variables not preceded by
var
are considered global by default; that is, they are not
bound to a specific function. While
var
is not required, we recommend you use it in any script with more
than one function.
Value Type
What it is
Example
Boolean
Logical True or False
True
Integer
Whole numbers (no decimal
points). Integers can be positive or
negative.
14
Number
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
A list of values (the values can be
any type).
["0p0", "0p0", "16p4", "20p6"]