- Viewing Photoshop CS2 objects, commands, and methods
- Creating new objects in a script
- Setting the active object
- Opening a document
- Saving a document
- Setting application preferences
- Suppressing dialog boxes
- Working with the Adobe Photoshop CS2 Object Model
- Working with color objects
- Working with filters
- Understanding clipboard interaction
- Working with units
- Sample workflow automation JavaScripts
- Advanced scripting
Advanced scripting
This section demonstrates how to use the information contained in the previous sections of this chapter to create scripts that do the following:
- Configure document preferences.
- Apply color to text items. In this section, you will also learn how to do the following:
- Create a reference to an existing document.
- Create a layer object and make the layer a text layer.
- Rasterize text so that wrap and blur processing can be applied to words. In these sections you will also learn how to do the following:
- Select and work with a specific area of a layer by creating a selection object.
- Apply wave and motion blur filters to selected text.
Working with document preferences
The sample scripts in this section activate a Adobe Photoshop CS2 Application object and then save the default configuration settings into variables so that they can be restored later when the script completes. These are the default configurations you most probably set up in the Preferences dialog when you initially installed and configured Adobe Photoshop CS2.
Next, the scripts set the following preferences to the following values:
Preference |
Set to |
Description |
---|---|---|
rulers |
inches |
Uses inches as the unit of measurement for graphics. |
units |
pixels |
Uses pixels as the unit of measurement for text (type). |
dialog modes |
never |
Suppresses the display of dialog boxes so that your script executes without the user being asked for input (such as clicking an OK button) at various stages of the process. |
Next, variables are declared that store document dimensions in inches and document resolution in pixels. A display resolution is declared and the text “Hello, World!” is assigned to a string variable.
Finally, an if statement checks whether a Document object has been created and then creates a new Document object if none exists.
• To work with document preferences:
- Create the following script.
//create and assign variables for default preferences startRulerUnits = app.preferences.rulerUnits startTypeUnits = app.preferences.typeUnits startDisplayDialogs = app.displayDialogs //change settings app.preferences.rulerUnits = Units.INCHES app.preferences.typeUnits = TypeUnits.PIXELS app.displayDialogs = DialogModes.NO //create and assign variables for document settings docWidthInInches = 4 docHeightInInches = 2 resolution = 72 //use the length property of the documents object to //find out if any documents are open //if none are found, add a document if (app.documents.length == 0) app.documents.add(docWidthInInches, docHeightInInches, resolution) //restore beginning preferences app.preferences.rulerunits = startRulerUnits app.preferences.typeunits = startTypeUnits app.displayDialogs = startDisplayDialogs
- Name the script HelloWorldDoc.jsx and save it in the Scripts folder.
- Open Adobe Photoshop CS2 and choose File > Scripts > HelloWorldDoc to run the script.
- Choose Edit > Preferences > Units & Rulers to verify that your preferences have been returned to your original settings.
- After viewing the document in Adobe Photoshop CS2, close the document without saving it.
- To prepare the script for the next section, comment the statements that restore the beginning preferences by adding slashes
as follows:
//app.preferences.rulerunits = startRulerUnits //app.preferences.typeunits = startTypeUnits
- Save the script.
Applying color to a text item
In this section, we will add a layer to the HelloWorldDoc script, then change the layer to a text object that displays the text Hello, World! in red.
Before you begin, do the following:
- Quit Adobe Photoshop CS2.
- Open the script file HelloWorldDoc in your script editor application.
• To create and specify details in a text item:
- Type the following code into the HelloWorldDoc script immediately before the commented statements that restore original preferences.
//create a reference to the active document docRef = app.activeDocument //create a variable named textColor //create a SolidColor object whose color is red //assign the object to textColor textColor = new SolidColor textColor.rgb.red = 255 textColor.rgb.green = 0 textColor.rgb.blue = 0 helloWorldText = "Hello, World!" //create a variable named newTextLayer //use the add() method of the artLayers class to create a //layer object assign the object to newTextLayer newTextLayer = docRef.artLayers.add() //use the kind property of the artLayer class to make the //layer a text layer newTextLayer.kind = LayerKind.TEXT newTextLayer.textItem.contents = helloWorldText newTextLayer.textItem.position = Array(0.75, 1) newTextLayer.textItem.size = 36 newTextLayer.textItem.color = textColor
- Save the script, and then open Adobe Photoshop CS2 and select the script from the Scripts menu (choose File > Script > HelloWorldDoc). Be patient while Adobe Photoshop CS2 executes your commands one by one.
- After viewing the document in Adobe Photoshop CS2, close Adobe Photoshop CS2 without saving the document.
Applying a wave filter
In this section we’ll apply a wave filter to the word Hello in our document. This entails the following steps:
- Set the document width and height to pixels and then rasterize the text object in the Text Layer.
- Select the area of the layer to which we want to apply the wave filter.
- Apply a wave filter to the selection.
Defining the area of a selection object
To define the area of a selection object, we will create an array of coordinates, or points specified in pixels within the document. The array indicates the coordinates that define the outside corners of a rectangular area that begins at the top left corner of the document and extends half way across the document.
The array values in order are:
- Upper left corner of the selection: 0,0
- 0 indicates the left-most column in the document.
- 0 indicates the top row in the document.
- Upper right corner of the selection: theDocWidthInPixels / 2, 0
- theDocWidthInPixels / 2 indicates the column in the middle of the document; that is, the column whose coordinate is the total number of columns in the document divided by 2.
- 0 indicates the top row in the document.
- Lower right corner: theDocWidthInPixels / 2, theDocHeightInPixels
- theDocWidthInPixels / 2 indicates the middle of the document.
- theDocHeightInPixels indicates the bottom row in the document; that is row whose coordinate is the total number of rows in the document.
- Lower left corner: theDocWidthInPixels / 2, 0
- theDocWidthInPixels / 2
- 0
- Upper left corner of the selection: 0,0
• To select an area and apply a wave filter to it:
- Type the following code into the script file HelloWorldDoc just above the commented statements that restore original preferences:
//create new variables to contain doc width and height //convert inches to pixels by multiplying the number of inches by //the resolution (which equals number of pixels per inch) docWidthInPixels = docWidthInInches * resolution docHeightInPixels = docHeightInInches * resolution //use the rasterize method of the artLayer class newTextLayer.rasterize(RasterizeType.TEXTCONTENTS) //create a variable to contain the coordinate values //for the selection object selRegion = Array(Array(0, 0), Array(docWidthInPixels / 2, 0), Array(docWidthInPixels / 2, docHeightInPixels), Array(0, docHeightInPixels), Array(0, 0)) //use the select method of the selection object //to create an object and give it the selRegion values //as coordinates docRef.selection.select(selRegion) // newTextLayer.applyWave(1, 1, 100, 5, 10, 100, 100, WaveType.SINE, UndefinedAreas.WRAPAROUND, 0)
- Save the script, and then open Adobe Photoshop CS2 and select the script from the Scripts menu (choose File > Script > HelloWorldDoc).
- After viewing the document in Adobe Photoshop CS2, close Adobe Photoshop CS2 without saving the document.
Applying a motionblur filter
In this section, we will apply a different filter to the other half of our document.
Additionally, because this is the last exercise in this that deals with our Hello World document, we will uncomment the statements that reset our original application preferences for rulers and units.
• To apply a motionblur filter to HelloWorldDoc:
- Type the following code into the script file HelloWorldDoc just above the commented statements that restore original preferences.
//change the value of selRegion to the other half of the document selRegion = Array(Array(docWidthInPixels / 2, 0), Array(docWidthInPixels, 0), Array(docWidthInPixels, docHeightInPixels), Array(docWidthInPixels / 2, docHeightInPixels), Array(docWidthInPixels / 2, 0)) docRef.selection.select(selRegion) newTextLayer.applyMotionBlur(45, 5) docRef.selection.deselect()
- Delete the slashes from the commented statements above the end tell statement as follows:
app.preferences.rulerUnits = startRulerUnits app.preferences.typeUnits = startTypeUnits
- Save the script, and then open Adobe Photoshop CS2 and select the script from the Scripts menu (choose File > Script > HelloWorldDoc).