Sub topic 15
Creating and Styling Text Layers with a Script
Prerequisites and the scripting model
This chapter assumes that you can open a Photoshop document, select layers, and use the Layers panel. You should also understand the difference between point text and paragraph text. ExtendScript is Photoshop’s older JavaScript-based scripting language. It lets you repeat text-layer tasks with precise values.
A text layer stores editable content and properties such as font size, position, color, and text type. A script accesses these properties through the layer’s textItem object. The document must already be open before the script runs.
flowchart TD
A[Open Photoshop document] --> B[Create art layer]
B --> C[Set layer kind to text]
C --> D[Access textItem]
D --> E[Set contents and formatting]
E --> F[Save or continue editing]caption: Scripted text-layer workflow alt: A flowchart showing a document becoming a formatted text layer through several scripting steps.
Create a point-text layer
Point text begins at one position and expands as you type. It works well for short headlines, labels, and buttons. The following script creates a new text layer, places it at 100 by 200 pixels, and gives it a blue color.
// ExtendScript: run from File > Scripts > Browse...
if (app.documents.length === 0) {
throw new Error("Open a Photoshop document before running this script.");
}
var doc = app.activeDocument;
var layer = doc.artLayers.add();
layer.kind = LayerKind.TEXT;
layer.name = "Headline";
var item = layer.textItem;
item.contents = "Hello, Photoshop";
item.size = new UnitValue(48, "pt");
item.position = [
new UnitValue(100, "px"),
new UnitValue(200, "px")
];
var color = new SolidColor();
color.rgb.red = 30;
color.rgb.green = 30;
color.rgb.blue = 120;
item.color = color;artLayers.add() creates a new art layer. Setting kind to LayerKind.TEXT turns it into a text layer. The contents property holds the displayed words. Photoshop uses UnitValue objects so that the script states whether a number is measured in points or pixels.
The position is measured from the document’s upper-left corner. Text can appear shifted from the exact coordinate because the position refers to the text’s baseline and left edge rather than its visible top edge.
> callout note > Keep text editable while you expect wording, font, or layout to change. A script can update an editable text layer later, but it cannot restore editability after the layer has been rasterized.
Create a paragraph-text box
Paragraph text is contained inside a fixed box. Use it for body copy, captions, or any text that must wrap automatically. This example changes the active text layer into a centered paragraph box.
// ExtendScript: the active layer must be a text layer
if (app.documents.length === 0) {
throw new Error("Open a Photoshop document before running this script.");
}
var layer = app.activeDocument.activeLayer;
if (layer.kind !== LayerKind.TEXT) {
throw new Error("Select a text layer before running this script.");
}
var item = layer.textItem;
item.kind = TextType.PARAGRAPHTEXT;
item.width = new UnitValue(400, "px");
item.height = new UnitValue(200, "px");
item.justification = Justification.CENTER;Changing kind to TextType.PARAGRAPHTEXT gives the layer a constrained text area. The width and height properties define that area. Justification.CENTER centers each line inside the box. Other common values include Justification.LEFT, Justification.RIGHT, and Justification.FULLJUSTIFY.
A reliable scripting workflow
- Check that a document is open.
- Create a layer or verify that the selected layer has the required type.
- Set the text type before setting dimensions such as width and height.
- Assign the contents and formatting.
- Inspect the result at the document’s actual display size.
- Save a copy before running scripts that modify many layers.
Different Photoshop versions can expose different scripting features. Test a script on a duplicate document, especially when it changes existing layers.
Exercise
Which text type should you use for a short logo label, and which should you use for a paragraph that must wrap inside a fixed area?
Show answer
Use point text for the short logo label. Use paragraph text for the wrapping paragraph.