Photoshop 脚本 - 更改特定文本图层内容 [英] Photoshop Script - Change specific text layer contents

查看:101
本文介绍了Photoshop 脚本 - 更改特定文本图层内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 photoshop 脚本文件,它打开了一个模板 psd 文件:

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)

一旦打开,我想要将名为LAYER1"的特定图层的文本更改为TEST"的代码.

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.

推荐答案

有必要遍历所有层(包括 Layer Groups 中的层),以找到您特定命名的 文本层(例如LAYER1),然后才能更改其文本内容.为此,我建议在您的脚本中添加一个自定义函数.

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.

以下代码示例会将名为 LAYER1Text Layer(s) 的文本内容更改为 Hello World.

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');

<小时>

说明

调用函数:

  1. 上面最后一行代码如下:

  1. The last line of code above which reads:

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

是调用 changeTextLayerContent 函数的地方.

is where the changeTextLayerContent function gets invoked.

我们向函数传递三个参数,如下所示:

We pass three arguments to the function as follows:

  • docRef - 这是要更改其图层的文档的对象引用.
  • 'LAYER1' - 这是要更改其内容的文本层的名称.
  • 'Hello World' - 这是应用于文本层(在本例中,应用于文本层)的新文本字符串(即内容) 名为 LAYER1).
  • 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).

假设我们要调用函数如下:

Let's say we were to invoke the function as follows:

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

这会将名为 MainTitleText Layer 的文本内容设置为 The quick brown fox.

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

注意:如果您的文档/模板包含多个名为 MainTitle文本层,那么它们的内容都将更改为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.

changeTextLayerContent 函数:

The changeTextLayerContent function:

  1. 该函数首先利用一个for 语句 循环遍历 Photoshop 图层调色板中列出的每个顶级图层.

然后检查图层typename是否为ArtLayer.

  • 如果它的 typenameArtLayer 它随后检查层 name 等于 layerName 你层kind是否等于LayerKind.TEXT.如果这些条件检查都为真,那么它才会通过以下行为 Text Layer 设置新的文本内容:

  • 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;

  • 或者,如果图层 typename 不是 ArtLayer,那么它必须是 LayerSet(即图层组).在这种情况下,该函数通过以下行重新调用自身:

  • 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); 
    

    然而,这次它传递了 layerRef 作为第一个参数,这会导致函数遍历组/集合中的所有层并检查它们.

    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.

    这篇关于Photoshop 脚本 - 更改特定文本图层内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆