Photoshop Script - Change specific text layer contents

允我心安 提交于 2021-02-11 13:32:18

问题


I have a photoshop script file which opens up a template psd file:

var fileRef = new File("z:\psd.psd")
var docRef = app.open (fileRef)

Once this is open, i would like code which changes the text of a specific layer called "LAYER1" to "TEST".

I have researched and carried out numerous tests but i am having issues and errors with undefined variables.


回答1:


It it will be necessary to loop over all layers, (including layers within Layer Groups), to find your specific named Text Layer (e.g. LAYER1) before it's text content can be changed. To achieve this I recommend adding a custom function to your script.

The following code example will change the text content of the Text Layer(s) named LAYER1 to Hello World.

var fileRef = new File('z:\psd.psd');
var docRef = app.open(fileRef);

/**
  * Change text content of a specific named Text Layer to a new text string.
  *
  * @param {Object} doc - A reference to the document to change.
  * @param {String} layerName - The name of the Text Layer to change.
  * @param {String} newTextString - New text content for the Text Layer.
  */
function changeTextLayerContent(doc, layerName, newTextString) {
  for (var i = 0, max = doc.layers.length; i < max; i++) {
    var layerRef = doc.layers[i];
    if (layerRef.typename === "ArtLayer") {
      if (layerRef.name === layerName && layerRef.kind === LayerKind.TEXT) {
        layerRef.textItem.contents = newTextString;
      }
    } else {
      changeTextLayerContent(layerRef, layerName, newTextString);
    }
  }
}

changeTextLayerContent(docRef, 'LAYER1', 'Hello World');

Explanation

Invoking the function:

  1. The last line of code above which reads:

    changeTextLayerContent(docRef, 'LAYER1', 'Hello World');
    

    is where the changeTextLayerContent function gets invoked.

    We pass three arguments to the function as follows:

    • docRef - which is a object reference of the document in which to change its layers.
    • 'LAYER1' - which is the name of the Text Layer to change its contents.
    • 'Hello World' - which is the new text string (i.e. content) to apply to the Text Layer (in this case, to the Text Layer named LAYER1).
  2. Let's say we were to invoke the function as follows:

    changeTextLayerContent(docRef, 'MainTitle', 'The quick brown fox');
    

    This would set the text content of the Text Layer named MainTitle to The quick brown fox.

    Note: If your document/template included multiple Text Layers named MainTitle then they would all have their content changed to The quick brown fox.

The changeTextLayerContent function:

  1. The function firstly utilizes a for statement to loop over each top level Layer or Group which is listed in Photoshop's Layers Palette.

  2. It then checks whether the layers typename is ArtLayer.

    • If its typename is ArtLayer it subsequently then checks the layers name equals the layerName you provided and whether the layers kind is equal to LayerKind.TEXT. If these conditional checks are both true, only then will it set the new text content for the Text Layer via the line which reads:

      layerRef.textItem.contents = newTextString;
      
    • Alternatively, if the layers typename is not a ArtLayer then it must be a LayerSet (i.e. a Layer Group). In this scenario the function re-invokes itself via the line reading:

      changeTextLayerContent(layerRef, layerName, newTextString); 
      

      However, this time it passes the layerRef as the first argument, which causes the function to loop over all layers in the group/set and check them too.




来源:https://stackoverflow.com/questions/50574218/photoshop-script-change-specific-text-layer-contents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!