3

This question Photoshop Script to create text in a bitmap image in Photoshop already has answer how to create text layer in photoshop, but as result in history tab you can see 8-10 history state changes. Is there a way to do the same job, but while changing history only once?

Obviously problem is that mentioned answer first adds a layer to document and then edits it 8-10 times and solution would be to create a layer and edit it's properties and then after it's ready add it to document.

I have searched in Photoshop CC 2014 Javascript Scripting Reference and it lists only 3 methods for ArtLayers methods: add(), getByName() and removeAll().

Function add() creates new layers, adds it and then returns it. So according to Javascript Scripting Reference I don't see how it would be possible to first create a layer and then add it.

I am still asking because possibly I have missed something or there is an official way to do it, but has not made it into the official docs for some reason.

Community
  • 1
  • 1
Marko Avlijaš
  • 1,579
  • 13
  • 27
  • What you mean is to speed up the layer creation process by not rendering the text creation step by step, but do everything and then render the final result? That would be quicker and less memory-hungry (as with many history steps, Photoshop keeps each state until the limit or you purge the memory). – Grzegorz G. Apr 09 '17 at 15:03

1 Answers1

0

Better late than never, but yes it is possible. You just have to suspend the history state with suspendHistory

Just pass your function as a string in the form of app.activeDocument.suspendHistory("string", "myFunction()");

The first parameter, as far as I'm aware is the history state string.

As in the example,

// Switch off any dialog boxes
displayDialogs = DialogModes.ERROR; // OFF 

app.activeDocument.suspendHistory ("history state", "createText('Arial-BoldMT', 48, 0, 128, 0, 'Hello World', 100, 50)");


function createText(fface, size, colR, colG, colB, content, tX, tY)
{

  // Add a new layer in the new document
  var artLayerRef = app.activeDocument.artLayers.add()

  // Specify that the layer is a text layer
  artLayerRef.kind = LayerKind.TEXT

  //This section defines the color of the hello world text
  textColor = new SolidColor();
  textColor.rgb.red = colR;
  textColor.rgb.green = colG;
  textColor.rgb.blue = colB;

  //Get a reference to the text item so that we can add the text and format it a bit
  textItemRef = artLayerRef.textItem
  textItemRef.font = fface;
  textItemRef.contents = content;
  textItemRef.color = textColor;
  textItemRef.size = size
  textItemRef.position = new Array(tX, tY) //pixels from the left, pixels from the top

  activeDocument.activeLayer.name = "Text";
  activeDocument.activeLayer.textItem.justification = Justification.CENTER;
}


// Set Display Dialogs back to normal
displayDialogs = DialogModes.ALL; // NORMAL
Ghoul Fool
  • 6,249
  • 10
  • 67
  • 125