Jud Dagnall Photography Blog

Photography, technology and occasional rants!

Scripting Photoshop: Text basics

Posted on May 11th, 2005 in , by jud || No Comment

I’ve been working with photoshop javascript scripting, which is relatively simple. For example, as an experiment, I’m in the process of automating the way I frame images for the web. This little script is step one in the processes, and provides a bit more detail about manipulating text. Copy the code to a file called Sign.js, and place this file in your *photoshop*/Presets/Scripts folder, and (after restarting photoshop) you can run it from the File->Scripts menu as “Sign”.

Note: I tested this code using Photoshop CS. It should work on CS2, and on Photoshop 7 with the scripting component installed, too.

// Sign.js - put your name in the lower right corner of the document
// preserve the existing settings before changing them
var strtRulerUnits = app.preferences.rulerUnits;
var strtTypeUnits = app.preferences.typeUnits;

// set our units to what we expect
app.preferences.rulerUnits = Units.PIXELS;
app.preferences.typeUnits = TypeUnits.POINTS;

// creates a new document, 400x600 pixels @ 72DPI, with a title "Hello World"
var docRef = app.documents.add( 400, 600, 72, "Hello, World!");

app.displayDialogs = DialogModes.NO

// Create a text color (red)
var textColor = new SolidColor;
textColor.rgb.red = 255;
textColor.rgb.green = 0;
textColor.rgb.blue = 0;

// Create the text layer that we'll manipulate
var artLayerRef = docRef.artLayers.add();
artLayerRef.kind = LayerKind.TEXT;

var textItem = artLayerRef.textItem;
textItem.justification = Justification.RIGHT;
textItem.font = "Papyrus"; // this is a Mac default font. Try "Arial" for cross-platform compat
textItem.contents = "Jud Dagnall";
textItem.size = 10;
textItem.color = textColor;
textItem.position = Array(400, 575);

// The next two steps aren't terrible relevant, but are part of the greater plan :)
// flatten the image
// make the background layer edit-able,
docRef.layers[0].isBackgroundLayer = false;

// clean up and restore settings
app.preferences.rulerUnits = strtRulerUnits;
app.preferences.typeUnits = strtTypeUnits;
docRef = null;
textColor = null;
newTextLayer = null;

Leave a Reply

Your email address will not be published. Required fields are marked *