Adobe 23101764 Scripting Guide - Page 24

Variables

Page 24 highlights

Scripting basics 2 Variables 2.6 Variables Variables are containers for data. A variable might contain a number, a string, a list (or array), or an object reference. Variables have names, and you refer to a variable by its name. To put data into a variable, assign the data to the variable. The file name of the current Photoshop document or the current date are both examples of data that can be assigned to a variable. By using variables the scripts you write will be reusable in a wider variety of situations. As a script executes, it can assign data to the variables that reflect the state of the current document and selection, for example, and then make decisions based on the content of the variables. NOTE: In AppleScript, it is not important to declare your variables before assigning values to them. In Visual Basic and JavaScript, however, it is considered good form to declare all of your variables before using them. To declare variables in Visual Basic, use the Dim keyword. To declare variables in JavaScript, use the var keyword. 2.6.1 Assigning values to variables The remainder of this section shows how to assign values to variables. AS set thisNumber to 10 set thisString to "Hello, World!" VB Option Explicit Dim thisNumber As Long Dim thisString As String thisNumber = 10 thisString = "Hello, World!" The Dim statement assigns a value type to the variable, which helps keep scripts clear and readable. Memory is also used more efficiently if variables are declared before use. If you start your scripts in Visual Basic with the line Option Explicit, you will have to declare all variables before assigning data to them. You will not have to declare them the next time they are used. JS var x = 8; x = x + 4; var thisNumber = 10; var thisString = "Hello, World!"; The var keyword identifies variables the first time that you use the variable. The next time you use the variable you should not use the var keyword. Photoshop CS Scripting Guide 20

  • 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

Photoshop CS Scripting Guide
20
Scripting basics
Variables
2
2.6 Variables
Variables are containers for data. A variable might contain a number, a string, a list (or array),
or an object reference. Variables have names, and you refer to a variable by its name. To put
data into a variable, assign the data to the variable. The
le name of the current Photoshop
document or the current date are both examples of data that can be assigned to a variable.
By using variables the scripts you write will be reusable in a wider variety of situations. As a
script executes, it can assign data to the variables that re
ect the state of the current document
and selection, for example, and then make decisions based on the content of the variables.
N
OTE
:
In AppleScript, it is not important to declare your variables before assigning values
to them. In Visual Basic and JavaScript, however, it is considered good form to
declare all of your variables before using them. To declare variables in Visual Basic,
use the
Dim
keyword. To declare variables in JavaScript, use the
var
keyword.
2.6.1
Assigning values to variables
The remainder of this section shows how to assign values to variables.
AS
set thisNumber to 10
set thisString to "Hello, World!"
VB
Option Explicit
Dim thisNumber As Long
Dim thisString As String
thisNumber = 10
thisString = "Hello, World!"
The
Dim
statement assigns a value type to the variable, which helps keep scripts clear and
readable. Memory is also used more ef
ciently if variables are declared before use. If you start
your scripts in Visual Basic with the line
Option Explicit
, you will have to declare all
variables before assigning data to them. You will not have to declare them the next time they
are used.
JS
var x = 8;
x = x + 4;
var thisNumber = 10;
var thisString = "Hello, World!";
The
var
keyword identi
es variables the
rst time that you use the variable. The next time you
use the variable you should not use the var keyword.