
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
O T E
:
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.