Photoshop CS2
Adobe Photoshop CS2 Scripting Guide
Scripting basics 14
Why Use Variables?
There are several reasons for using variables rather than entering values directly in the script.
●
Variables make your script easier to update or change. For example, if your script creates several 4 x 2
inch documents and later you want to change the documents’ size to 4 x 3 inches, you could simply
change the value of the variable
docHeight
from
2
to
3
at the beginning of your script and the entire
script would be updated automatically.
If you had used the direct value
2 inches
to enter the height for each new document, updating the
document sizes would be much more tedious. You would need find and change each statement
throughout the script that creates a document.
●
Variables make your scripts 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, and then make
decisions based on the content of the variables.
Data Contained in Variables
The data that a variable contains is the variable’s
value
. To assign a value to a variable, you use an
assignment statement
. A variable’s value can be a number, a
string
(a word or phrase or other list of
characters enclosed in quotes), an object reference, a mathematical expression, another variable, or a list
(including collections, elements, and arrays).
See
‘Using Operators’ on page 25
for information on using mathematical expressions or other
variables as values. See
‘Using Object Properties’ on page 18
for information about arrays.
Assignment statements require specific syntax in each scripting language. See
‘Creating Variables
and Assigning Values’ on page 14
for details.
Creating Variables and Assigning Values
This section demonstrates how to create two variables named
thisNumber
and
thisString
, and then assign
the following values:
Note:
When you assign a string value to a variable, you must enclose the value in straight, double quotes
(""). The quotes tell the script to use the value as it appears without interpreting or processing it. For
example, 2 is a number value; "2" is a string value. The script can add, subtract, or perform other
operations with a number value. It can only display a string value.
AS
In AppleScript, you must both create and assign a value to a variable in a single statement. You can create
a variable using either the
set
command or the
copy
command.
With the
set
command, you list the variable name (called an
identifier
in AppleScript) first and the value
second, as in the following example:
set thisNumber to 10
set thisString to "Hello, World"
With the
copy
command, you list the value first and the identifier second.
copy 10 to thisNumber
copy "Hello World" to thisString
Variable
Value
thisNumber
10
thisString
"Hello World"