Home / Articles / Adobe Photoshop / Learn How to Script Adobe Photoshop CS2

Learn How to Script Adobe Photoshop CS2

Chapter Description

This chapter demonstrates several techniques for creating scripts to use specifically with Adobe Photoshop CS2. More importantly, you'll learn how to use the Adobe Photoshop CS2 scripting references to find the objects, classes, properties, methods, and even some values (called constants or enumerations) you can use to create JavaScripts for Adobe Photoshop CS2.

Setting the active object

To work on a an object in the Adobe Photoshop CS2 application, you must make the object the front-most, or active object. For example, to work in a layer, you must first bring the layer to the front.

In scripting, the same rule applies. If your script creates two or more documents, the commands and methods in your script are executed on the active document. Therefore, to ensure that your commands are acting on the correct document, it is good programming practice to designate the active document before executing any commands or methods in the script.

To set an active object, use the activeObject property of the parent object (such as activeDocument or activeLayer).

For sample scripts that set active objects, see the following sections.

  • Setting the active document
  • Setting the active layer
  • Setting the active channels

Setting the active document

The following example demonstrates how to set the active document.

// Create 2 documents
var docRef = app.documents.add( 4, 4)
var otherDocRef = app.documents.add (4,6)

//make docRef the active document
app.activeDocument = docRef
//here you would include command statements
//that perform actions on the active document. Then, you
//make a different document the active document

//use the activeDocument property of the Application
object to
//bring otherDocRef front-most as the new active document
app.activeDocument = otherDocRef

Setting the active layer

The following example demonstrates how to use the current layer (ActiveLayer/activeLayer) property of the Document object to set the active layer.

docRef.activeLayer = docRef.layers["Layer 1"]

Look up the activeLayer property in the Properties table of the Document object in “Chapter 5, “JavaScript Object Reference” in Part 2 of this book.

Setting the active channels

More than one channel can be active at a time.

Set the active channels to the first and third channel using a channel array:

theChannels = new Array(docRef.channels[0],
docRef.activeChannels = theChannels

Alternatively, select all component channels by using the componentChannels property of the Document object:

app.activeDocument.activeChannels =
4. Opening a document | Next Section Previous Section