Adobe 23101764 Scripting Guide - Page 41

Dim appRef As New Photoshop.Application

Page 41 highlights

Scripting Photoshop 3 Your first Photoshop script For now, enter the following code. The lines preceded by ' (single quotes) are comments, and will be ignored by the scripting system. They're included to describe the operation of the script. As you look through the script, you'll see how to create, then address each object. Private Sub Command1_Click() ' Hello World Script Dim appRef As New Photoshop.Application ' Remember current unit settings and then set units to ' the value expected by this script Dim originalRulerUnits As Photoshop.PsUnits originalRulerUnits = appRef.Preferences.RulerUnits appRef.Preferences.RulerUnits = psInches ' Create a new 4x4 inch document and assign it to a variable. Dim docRef As Photoshop.Document Dim artLayerRef As Photoshop.ArtLayer Dim textItemRef As Photoshop.TextItem Set docRef = appRef.Documents.Add(4, 4) ' Create a new art layer containing text Set artLayerRef = docRef.ArtLayers.Add artLayerRef.Kind = psTextLayer ' Set the contents of the text layer. Set textItemRef = artLayerRef.TextItem textItemRef.Contents = "Hello, World!" ' Restore unit setting appRef.Preferences.RulerUnits = originalRulerUnits End Sub 5. Save the form. 6. Start Photoshop. 7. Return to Visual Basic and run the program. If you created a form, click the button you created earlier. 8. Run the script. Photoshop will create a new document, add a new art layer, change the art layer's type to text and set the text to "Hello, World!" Photoshop CS Scripting Guide 37

  • 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
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105

Photoshop CS Scripting Guide
37
Scripting Photoshop
Your first Photoshop script
3
For now, enter the following code. The lines preceded by ' (single quotes) are comments,
and will be ignored by the scripting system. They’re included to describe the operation of
the script. As you look through the script, you’ll see how to create, then address each
object.
Private Sub Command1_Click()
' Hello World Script
Dim appRef As New Photoshop.Application
' Remember current unit settings and then set units to
' the value expected by this script
Dim originalRulerUnits As Photoshop.PsUnits
originalRulerUnits = appRef.Preferences.RulerUnits
appRef.Preferences.RulerUnits = psInches
' Create a new 4x4 inch document and assign it to a variable.
Dim docRef As Photoshop.Document
Dim artLayerRef As Photoshop.ArtLayer
Dim textItemRef As Photoshop.TextItem
Set docRef = appRef.Documents.Add(4, 4)
' Create a new art layer containing text
Set artLayerRef = docRef.ArtLayers.Add
artLayerRef.Kind = psTextLayer
' Set the contents of the text layer.
Set textItemRef = artLayerRef.TextItem
textItemRef.Contents = "Hello, World!"
' Restore unit setting
appRef.Preferences.RulerUnits = originalRulerUnits
End Sub
5.
Save the form.
6.
Start Photoshop.
7.
Return to Visual Basic and run the program. If you created a form, click the button you
created earlier.
8.
Run the script. Photoshop will create a new document, add a new art layer, change the art
layer's type to text and set the text to “Hello, World!”