Photoshop scripts - An introduction
Author: Edvard Erlandsson
Categories: Scripts
This tutorial is an introduction to Photoshop scripts and will teach you the basics of scripting for Photoshop so that you can start creating your own scripts. This tutorial will be followed by a series of tutorials that will teach you everything you have to know to create both simple and advanced scripts.
Categories: Scripts
This tutorial is an introduction to Photoshop scripts and will teach you the basics of scripting for Photoshop so that you can start creating your own scripts. This tutorial will be followed by a series of tutorials that will teach you everything you have to know to create both simple and advanced scripts.
Requirements
Scripts were introduced in Photoshop CS and were greatly improved in the release of CS2, which gave access to many new options. To be able to create and use scripts its necessary that you have Photoshop CS or later, I have done all testing in CS2 so its highly recommended that you use it as well.Background
If you are familiar to Photoshop you probably know about actions and that tedious tasks can be automated with them. One could say that scripts are similar to actions in the sense that they can also be used to automate tasks, but scripts can do so much more then what was possible with actions. For starters scripts have conditional logic which makes it possible to make different things based on the input for the script.This series of tutorials will only deal with JavaScript since this is the only platform independent (will run on both Windows and Mac) scripting language supported by Photoshop. Available is also Visual Basic (only for Windows) and AppleScript (only for Mac).
Why use scripts
If you use Photoshop regularly and its part of your everyday work you have most certainly found yourself doing the same thing over and over again. Since Photoshop is supposed to be a creative tool its nice to have the ability to automate things you do on a regularly basis as a way of removing tedious task and save time. As an example I often create avatars for tutorials and articles I write, and even though I have a template this work could be done faster with scripts.Objects
Everything in Photoshop is represented as objects and it’s these objects that your scripts have to work with in order to create something. To better understand how the different objects in Photoshop relates to each other I have made a simple object model of the most common objects in Photoshop.
Now what does this picture mean? Well for starters none of the objects can exist without the document object and this is nothing strange since you have to create a document before you can create a layer. What we more can see is that the Art Layer can exist directly in the document or in a Layer Set, so the Layer Set is the ordinary grouping mechanism in Photoshop.
Collection of objects
When you create a new object it’s automatically added to a collection or array, each collection contains only objects of the same class. If you create a new Art Layer it’s automatically added to an Art Layer collection and can then be accessed through this collection. One thing that is important to note is that when accessing an object by the numeric array index you have to use the collection name (which is the class name in plural), while when accessing objects by their names you only use the class name. documents[0]
document[“MyDocument”]
document[“MyDocument”]
Variables
Variables is great for many things but the best use in your scripts is the ability to define a variable at one place and then use it in many places, this saves time when you want to change the value since you only have to do it at one place. And if you name the variables in a logic and self-explaining fashion it will be easier to understand the script two months later when you want to update it.Declaration and assignment of values to a variable can be done in two ways.
var borderWidth
borderWidth = 10
or
var borderWidth = 10
borderWidth = 10
or
var borderWidth = 10
Comments
As with all programming it’s a good idea to comment your script to make it easier for yourself and others to change it in the future. In JavaScript you can either make a single line comment or a multiple line comment. //This is a single line comment
/* This is a multiple
line comment
*/
/* This is a multiple
line comment
*/
Active object
When you work with Photoshop the ordinary way you have to make the layer you want to paint on the active layer, the same rule apply when painting from a script. Bellow I will demonstrate how to set the active document and the active layer with a very simple example. var doc1Ref = documents.add(500, 500)
var doc2Ref = documents.add(500, 500)
//Now we set document 1 as the active document
activeDocument = doc1Ref
//Here something should be painted in document 1
//Now we set document 2 as the active document
activeDocument = doc2Ref
var layer1Ref = doc2Ref.artLayers.add()
var layer2Ref = doc2Ref.artLayers.add()
//Now we set layer 1 as the active layer in document 2
doc2Ref.ActiveLayer = layer1Ref
//Here something should be painted in layer 1 in document 2
// Now we set layer 2 as the active layer in document 2
doc2Ref.ActiveLayer = layer2Ref
var doc2Ref = documents.add(500, 500)
//Now we set document 1 as the active document
activeDocument = doc1Ref
//Here something should be painted in document 1
//Now we set document 2 as the active document
activeDocument = doc2Ref
var layer1Ref = doc2Ref.artLayers.add()
var layer2Ref = doc2Ref.artLayers.add()
//Now we set layer 1 as the active layer in document 2
doc2Ref.ActiveLayer = layer1Ref
//Here something should be painted in layer 1 in document 2
// Now we set layer 2 as the active layer in document 2
doc2Ref.ActiveLayer = layer2Ref

