Adobe 23101335 Scripting Guide - Page 14

Variables, 2.5.1 Assigning values to variables

Page 14 highlights

Scripting basics 2 Variables 2.5 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.5.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 7.0 Scripting Guide 14

  • 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

Photoshop 7.0 Scripting Guide
14
Scripting basics
Variables
2
2.5 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.
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.5.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.