Photoshop scripts - The environment

Photoshop script - Environment avatar
Author: Edvard Erlandsson
Categories: Scripts
If you have read my introduction to Photoshop scripts you know the basics of scripts in Photoshop and it’s time to introduce Adobes ExtendScript Toolkit which is the environment which you will work in to create scripts for Photoshop CS2.
In this tutorial I will introduce Adobe ExtendScript Toolkit, reference documents that you will need when writing scripts and in the end I will guide you through a hello world program for Photoshop scripts.
ExtendScript Toolkit
If you have Photoshop CS2 you probably already have the ExtendScript Toolkit, to test if you have it create a new file and name it test.jsx. When double-clicking the file ExtendScript Toolkit should open.

If another program starts you can find the program in:
c:\Program Files\Adobe\Adobe Utilities\ExtendScript Toolkit\

If you don’t have the ExtendScript Toolkit in that folder you can download it from Adobe:
ExtendScript Toolkit 1.0.3 for Windows
ExtendScript Toolkit 1.0.3 for Mac

There is only two things you have too know about the ExtendScript Toolkit program right now:
Select target application, ExtendScript must know which application you are writing the script for. In the top left dropdown menu select Adobe Photoshop CS2 and if asked if you want top start it choose yes.
Running script, to run a script you have written you press the button that looks like a play button.
Reference documents
All objects have different properties that can be set and methods that can be executed and right now you probably don’t know any of these, this is where the reference documents come in. The reference document for JavaScript contains information about all objects available in your scripts.

The Photoshop CS2 JavaScript reference guide can be found on Adobe Scripting documentation page, download it and the Photoshop CS2 Scripting Guide document.

Open the JavaScript Reference Guide and click the ArtLayer bookmark to the left in Adobe Reader, now you can first se all properties available for ordinary layers and after that is all the methods available for it.
Hello world
Now it’s time to try your wings by creating and running your first script for Photoshop. As always the first program you create in a new programming language should always output the Hello world sentence.

//1. Set the units in photoshop to pixels
preferences.rulerUnits = Units.PIXELS
//2. Create a new document that is 800pixels by 400pixels
var docRef = documents.add(800, 400)
//3. Add a new layer in the new document
var artLayerRef = docRef.artLayers.add()
//4. Specify that the layer is a text layer
artLayerRef.kind = LayerKind.TEXT

//5. This section defines the color of the hello world text
textColor = new SolidColor();
textColor.rgb.red = 66;
textColor.rgb.green = 162;
textColor.rgb.blue = 191;

//6. Get a reference to the text item so that we can add the text and format it a bit
textItemRef = artLayerRef.textItem
textItemRef.contents = "Hello world!"
textItemRef.color = textColor
textItemRef.size = 30.0
textItemRef.position = new Array(300, 100) //pixels from the left, pixels from the top
This script is very simple and you probably understand most of it already but I will here explain in more detail what happens.

1. If you look up the Preference object (page 153) you will see that it have a property called rulerUnits (page 155). This property specifies which unit all values will have in this script. In the value type column you see that it takes only Units as input. Units is a constant so we must find it in the reference and see what different units is available, Units is found on page 279.

2. In this step we want to create a new document which is done through the collection object for document (which is the class name in plural), and in the documents object in the reference (page 99) we can se that there is a method called add. Add takes a lot of parameters but we only need width and height in this example so skip the others.

3. This is almost the same as step 2 but instead we want to add a layer and so we have to go to the artLayer collection object instead to find the add() method.

4. Now we want to specify that the new layer is a text-layer. In the object reference for artLayer (page 57) you can se that there is a property called kind which takes a LayerKind constant.

5. I want the text to have a nice color instead of the default black which is used if you don’t specify a specific color. Look up the textItem object in the reference and look at the property color, you will see that it takes a SolidColor as value. Look up SolidColor object and see what you need to do to create a new color.

6. Ok we want some text in our text layer, if you look in the artLayer object you will find a property called textItem, this is a reference to the layers textItem object which we need to be able to add some text to the layer. Store the reference in a variable and add the text and color, I have also specified the size and position of the text. The position is equal to the point where you click with the Text tool in Photoshop.
Installing Photoshop Scripts
You can run your scripts from the ExtendScript Toolkit, but once the script is finished and tested you don’t need to use it anymore, intead you can run your scripts directly from Photoshop.

To install a script you simply copy it to Photoshop’s scripts folder which in windows is located in:
c:\Program Files\Adobe\Photoshop CS2\Presets\Scripts\
Before you can use it from Photoshop you have to quit Photoshop and restart it.
Once you have done this the script is available under File > Scripts


Photoshop scripting tutorial series index
Part 1: Photoshop scripts - An introduction
Part 2: Photoshop scripts - The Environment