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.

Working with units

Adobe Photoshop CS2 provides two rulers for documents. You can set the measurement units for the rulers in your script. The rulers are:

  • A graphics ruler used for most graphical layout measurements or operations on a document where height, width, or position are specified.

    You set measurement unit types for the graphics ruler using the rulerUnits property.

  • A type ruler, which is active when using the type tool

    You set measurement unit types for the type ruler using the typeUnits property.

Unit values

All languages support plain numbers for unit values. These values are treated as being of the type currently specified for the appropriate ruler.

To ensure that your scripts produce the expected results you should check and set the ruler units to the type appropriate for your script. After executing a script the original values of the rule settings should be restored if changed in the script. See “Setting ruler and type units in a script” on page 49 for directions on setting unit values.

Please refer to Adobe Photoshop CS2 Help for information about available unit value types.

Special unit value types

The unit values used by Adobe Photoshop CS2 are length units, representing values of linear measurement. Support is also included for pixel and percent unit values. These two unit value types are not, strictly speaking, length values but are included because they are used extensively by Adobe Photoshop CS2 for many operations and values.

Using unit values in calculations

To use a unit value in a calculation it is necessary to first convert the value to a number (unit value cannot be used directly in calculations). To multiply an inch value write:

set newValue to (inchValue as number) * someValue

Unit value usage

The following tables list the properties of the classes/objects that are defined to use unit values. Unit values for these properties, unless otherwise indicated in the table, are based the graphics ruler setting.

To use this table, look up the property in the object’s Properties table in Part 2 of this book.

Class/Object

JavaScript Properties

Document

height
width

EPS open options

height
width

PDF open options

height
width

lens flare open options

height
width

offset filter

horizontalOffset
verticalOffset

Text Item

baselineShift [*]
firstLineIndent [1]
height
hyphenationZone [1]
leading [1]
leftIndent [1]
position
rightIndent [1]
spaceBefore [1]
spaceAfter [1]
width

The following list contains the methods that use unit values as parameters or arguments. In some cases the parameters are required. The methods are preceded by the object to which they belong. Look up the method in the Methods table of the object in the “Interface” chapter in Part 2 of this book.

  • document.crop(bounds, height, width)
  • document.resizeCanvas(height, width)
  • document.resizeImage(height, width)
  • selection.contract(by)
  • selection.expand(by)
  • selection.feather(by)
  • selection.selectBorder(width)
  • selection.translate(deltaX, deltaY)
  • selection.translateBoundary(deltaX, deltaY)

Setting ruler and type units in a script

The unit type settings of the two Adobe Photoshop CS2 rulers control how numbers are interpreted when dealing with properties and parameters that support unit values. Be sure to set the ruler units as needed at the beginning of your scripts and save and restore the original ruler settings when your script has completed.

In JavaScript ruler units and type units are properties of the Preferences, accessed through the Application object’s preferences property as shown below.

app.preferences.rulerUnits = Units.INCHES
app.preferences.typeUnits = TypeUnits.PIXELS
app.preferences.pointSize = PointType.POSTSCRIPT
13. Sample workflow automation JavaScripts | Next Section Previous Section