Using JavaScript scripting in Adobe Photoshop, how to change the contents of the selected text layer

StackOverflow https://stackoverflow.com/questions/1287868

  •  18-09-2019
  •  | 
  •  

Question

Alternatively, how to change the contents of a TextItem by layer name.

note: I'm using Photoshop CS4 (11.0.1)

Was it helpful?

Solution

I just wrote my first Photoshop JSX script with this occasion :)

I'm using Photoshop CS3, but I'm guessing changing text is a core feature that might not change that easily from one version to another;

Here's my snippet:

//get the active document
var doc = app.activeDocument;
//get the active layer
var al = doc.activeLayer;
if(al.kind == LayerKind.TEXT) {
    //get the textItem
    var ti = al.textItem;
    //change contents
    ti.contents = "stackoverflow";
}

I'm not sure if I got exactly what you mean by changing the contents by layer name, but here's my go at it:

function changeTextByLayerName(layerName,newText){
    var layer = doc.layers.getByName(layerName);
    if(layer.kind == LayerKind.TEXT) layer.textItem.contents = newText;
}

Luckily there is a getByName method there so not manual looping needed, and then I'm just checking if the layer is actually a Text Layer.

Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top