Adobe 23101764 Scripting Guide - Page 39

Your first Photoshop script

Page 39 highlights

Scripting Photoshop 3 Your first Photoshop script If you hold down the option key (alt for Windows), a debug window displays. NOTE: The "File>Scripts" menu displays JavaScripts only. 3.3 Your first Photoshop script The traditional first project in any programming language is to display the message "Hello World!" In this section, we'll create a new Photoshop document, then add a text item containing this message with examples in AppleScript, Visual Basic, VBScript and JavaScript. 3.3.1 AppleScript 1. Locate and open the Script Editor. 2. Below we're going to revisit the "Hello, World!" AppleScript example from Chapter 2 with comments included. (We'll expand on this sample code in the Advanced Scripting section that follows. ) For now, enter the following script. The lines preceded by "--" are comments. They're included to document the operation of the script and it's good style to include them in your own scripts. As you look through the script, you'll see how to create, then address, each object. The AppleScript command tell indicates the object that will receive the next message we send. -- Sample script to create a new text item and change its -- contents. tell application "Adobe Photoshop CS" -- Create a new document and art layer. set docRef to make new document with properties ¬ {width:3 as inches, height:2 as inches} set artLayerRef to make new art layer in docRef -- Change the art layer to be a text layer. set kind of artLayerRef to text layer -- Get a reference to the text object and set its contents. set contents of text object of artLayerRef to "Hello, World!" end tell 3. 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 35

  • 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
35
Scripting Photoshop
Your first Photoshop script
3
If you hold down the option key (alt for Windows), a debug window displays.
N
OTE
:
The "File>Scripts" menu displays JavaScripts only.
3.3 Your first Photoshop script
The traditional
rst project in any programming language is to display the message “Hello
World!” In this section, we’ll create a new Photoshop document, then add a text item
containing this message with examples in AppleScript, Visual Basic, VBScript and JavaScript.
3.3.1 AppleScript
1.
Locate and open the Script Editor.
2.
Below we’re going to revisit the "Hello, World!" AppleScript example from Chapter 2 with
comments included. (We’ll expand on this sample code in the Advanced Scripting section
that follows. )
For now, enter the following script. The lines preceded by “--” are comments. They’re
included to document the operation of the script and it’s good style to include them in your
own scripts. As you look through the script, you’ll see how to create, then address, each
object. The AppleScript command
tell
indicates the object that will receive the next
message we send.
-- Sample script to create a new text item and change its
-- contents.
tell application "Adobe Photoshop CS"
-- Create a new document and art layer.
set docRef to make new document with properties ¬
{width:3 as inches, height:2 as inches}
set artLayerRef to make new art layer in docRef
-- Change the art layer to be a text layer.
set kind of artLayerRef to text layer
-- Get a reference to the text object and set its contents.
set contents of text object of artLayerRef to "Hello, World!"
end tell
3.
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!”