Adobe 23101335 Scripting Guide - Page 15

Using variables to store references, 2.5.3 Naming variables

Page 15 highlights

Scripting basics 2 Variables 2.5.2 Using variables to store references Variables can also be used to store references to objects. In AppleScript, a reference is returned when you create a new object in an Photoshop document as shown below: set thisLayer to make new art layer in current document Or you can fill the variable with a reference to an existing object: set thisLayer to art layer 1 of current document Visual Basic works similarly, however, there is an important distinction to note. If you are assigning an object reference to a variable you must use the Set command. For example, to assign a variable as you create a layer, use Set: Set thisLayer = appRef.Photoshop.ActiveDocument.ArtLayers(1) or in reference to an existing layer, since it is also an object reference, use Set: Set thisLayer = appRef.Photoshop.ActiveDocument.ArtLayers(1) If you are trying to assign a value to a variable in Visual Basic that is not an object reference, do not use Set. Use Visual Basic's assignment operator, the equals sign: thisNumber = 12 JavaScript looks similar to Visual Basic. To assign a reference to an object, you would write: var docRef = activeDocument; and to assign a value use the following: var thisNumber = 12 2.5.3 Naming variables It's a good idea to use descriptive names for your variables-something like firstPage or corporateLogo, rather than x or c. You can also give your variable names a standard prefix so that they'll stand out from the objects, commands, and keywords of your scripting system. Variable names must be a single word, but you can use internal capitalization (such as myFirstPage) or underscore characters (my_first_page) to create more readable names. Variable names cannot begin with a number, and they can't contain punctuation or quotation marks. Photoshop 7.0 Scripting Guide 15

  • 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
15
Scripting basics
Variables
2
2.5.2 Using variables to store references
Variables can also be used to store references to objects. In AppleScript, a reference is returned
when you create a new object in an Photoshop document as shown below:
set thisLayer to make new art layer in current document
Or you can fill the variable with a reference to an existing object:
set thisLayer to art layer 1 of current document
Visual Basic works similarly, however, there is an important distinction to note. If you are
assigning an
object reference
to a variable you must use the
Set
command. For example, to
assign a variable as you create a layer, use
Set
:
Set thisLayer = appRef.Photoshop.ActiveDocument.ArtLayers(1)
or in reference to an existing layer, since it is also an
object reference,
use
Set
:
Set thisLayer = appRef.Photoshop.ActiveDocument.ArtLayers(1)
If you are trying to assign a value to a variable in Visual Basic that is not an object reference,
do not use
Set
. Use Visual Basic’s assignment operator, the equals sign:
thisNumber = 12
JavaScript looks similar to Visual Basic. To assign a reference to an object, you would write:
var docRef = activeDocument;
and to assign a value use the following:
var thisNumber = 12
2.5.3 Naming variables
It’s a good idea to use descriptive names for your variables—something like
firstPage
or
corporateLogo
, rather than
x
or
c
. You can also give your variable names a standard prefix
so that they’ll stand out from the objects, commands, and keywords of your scripting system.
Variable names must be a single word, but you can use internal capitalization (such as
myFirstPage
) or underscore characters (
my_first_page
) to create more readable names.
Variable names cannot begin with a number, and they can’t contain punctuation or quotation
marks.