Scripting Photoshop
3
Object references
3.4.2 Visual Basic and JavaScript
Object references in Visual Basic and JavaScript are fixed and remain valid until disposed or
until the host object goes away. The following example shows how to create 2 layers and then
rename the first one in Visual Basic.
Dim appRef As Photoshop.Application
Dim docRef As Photoshop.Document
Dim layer1Ref As Photoshop.ArtLayer
Dim layer2Ref As Photoshop.ArtLayer
' Set ruler units and create a new document.
Set appRef = New Photoshop.Application
originalRulerUnits = appRef.Preferences.RulerUnits
appRef.Preferences.RulerUnits = psInches
Set docRef = appRef.Documents.Add(4, 4, 72, "My New Document")
' Create 2 new layers and store their return references.
Set layer1Ref = docRef.ArtLayers.Add()
Set layer2Ref = docRef.ArtLayers.Add()
' Change the name of the first layer that was created.
layer1Ref.Name = "This layer was first"
'restore unit values
appRef.Preferences.RulerUnits = originalRulerUnits
To do a similar thing in JavaScript you could do:
// set ruler units and create new document
originalRulerUnits = preferences.rulerUnits
preferences.rulerUnits = Units.INCHES;
documents.add(4,4,72,"My New Document");
docRef = activeDocument;
layer1Ref = docRef.artLayers.add();
layer2Ref = docRef.artLayers.add();
layer1Ref.name = "This layer was first";
// restore unit setting
preferences.rulerUnits = originalRulerUnits;
43
Photoshop 7.0 Scripting Guide