Adobe 65009333 Scripting Guide - Page 12

Operators, Conditional statements, Array variables, Finding the value type of a variable

Page 12 highlights

Getting Started Scripting terminology and the InCopy object model 12 Try to use descriptive names for your variables, like 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 one word, but you can use internal capitalization (like myFirstPage) or underscore characters (my_first_page) to create more readable names. Variable names cannot begin with a number, and they cannot contain punctuation or quotation marks. Array variables An Array object is a container for a series of values: myArray = [1, 2, 3, 4]; To refer to an item in an array, refer to its index in the array.In JavaScript, the first item in an array is item 0: var myFirstArrayItem = myArray[0]; Arrays can include other arrays, as shown in the following examples: var myArray = [[0,0], [72,72]]; Finding the value type of a variable Sometimes, your scripts must make decisions based on the value type of an object. If you are working on a script that operates on a text selection, for example, you might want that script to stop if nothing is selected. //Given a variable of unknown type, "myMysteryVariable"... myType = myMysteryVariable.constructor.name; //myType will be a string corresponding to the JavaScript type (e.g., "Rectangle") Operators Operators use variables or values to perform calculations (addition, subtraction, multiplication, and division) and return a value. For example: MyWidth/2 returns a value equal to half of the content of the variable myWidth. You also can use operators to perform comparisons (equal to (=), not equal to(), greater than (>), or less than ( myHeight returns the value true (or 1) if myWidth is greater than myHeight; otherwise, false (0). In JavaScript, use the plus sign (+) to join the two strings: "Pride " + "and Prejudice" //returns the string: "Pride and Prejudice" Conditional statements "If the size of the selected text is 12 points, set the point size to 10 points." This is an example of a conditional statement. Conditional statements make decisions; they give your scripts a way to evaluate something (like the color of the selected text, number of pages in the document, or date), then act according to the result. Most conditional statements start with if.

  • 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
12
Try to use descriptive names for your variables, like
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 one word, but you can use internal capitalization (like
myFirstPage
) or
underscore characters (
my_first_page
) to create more readable names. Variable names cannot begin
with a number, and they cannot contain punctuation or quotation marks.
Array variables
An Array object is a container for a series of values:
myArray = [1, 2, 3, 4];
To refer to an item in an array, refer to its index in the array.In JavaScript, the first item in an array is item 0:
var myFirstArrayItem = myArray[0];
Arrays can include other arrays, as shown in the following examples:
var myArray = [[0,0], [72,72]];
Finding the value type of a variable
Sometimes, your scripts must make decisions based on the value type of an object. If you are working on a
script that operates on a text selection, for example, you might want that script to stop if nothing is
selected.
//Given a variable of unknown type, "myMysteryVariable"...
myType = myMysteryVariable.constructor.name;
//myType will be a string corresponding to the JavaScript type (e.g., "Rectangle")
Operators
Operators use variables or values to perform calculations (addition, subtraction, multiplication, and
division) and return a value. For example:
MyWidth/2
returns a value equal to half of the content of the variable
myWidth
.
You also can use operators to perform comparisons (equal to (
=
), not equal to(
<>
), greater than (
>
), or less
than (
<
)). For example:
MyWidth > myHeight
returns the value
true
(or 1) if
myWidth
is greater than
myHeight
; otherwise,
false
(0).
In JavaScript, use the plus sign (
+
) to join the two strings:
"Pride " + "and Prejudice"
//returns the string: "Pride and Prejudice"
Conditional statements
“If the size of the selected text is 12 points, set the point size to 10 points.” This is an example of a
conditional statement
. Conditional statements make decisions; they give your scripts a way to evaluate
something (like the color of the selected text, number of pages in the document, or date), then act
according to the result. Most conditional statements start with
if
.