0

I'm designing hundreds of posters that all have different text - but should all be at the same X,Y position - reference point being the top left: X= 213 px and Y= 41 px

Some are a little off and I'd love to get them corrected, quickly and through automation.

It's easy enough to create an action to transform text, but since the text content is different from file to file, I can't automate that portion.

So looking for a script that essentially selects the text layer. There's only one text layer in all of these documents so something like "function: gettextlayer" and then select that layer in the layer panel.

I can do the transform bit via action automation from there.

Been scratching my head at this one and have dug everywhere.

4

2 回答 2

0

如果文本图层从未在图层集中,您可以使用以下代码段来查找和移动它...

var doc = app.activeDocument;
for (var i = 0; i < doc.layers.length; i++) {
    var lyr = doc.layers[i];

    if (lyr.kind == LayerKind.TEXT) {
        //bounds order is top, left, bottom right
        var dx = 213 - lyr.bounds[0].as("px") ;
        var dy = 41 - lyr.bounds[1].as("px");

        lyr.translate(dx, dy);
    }

}

请注意,边界与文本框有关,而不是文本本身。因此,特别是在段落文本框的情况下,这可能不是您所期望的。如果您需要文本本身的边界,您需要栅格化文本图层的副本,然后在该图层上进行数学运算。

如果它可以在一个图层集中,它会变得有点困难,因为你必须递归地遍历每个图层集才能找到它。除非您从 xtools 中获取 stdlib.js 文件的副本。如果您要编写任何脚本,则可以在附近闲逛的工厂库。一旦你有了那个文件,你就可以使用...

#include "stdlib.js"
var doc = app.activeDocument;
var lyr = Stdlib.findLayerByProperty(doc, "kind", LayerKind.TEXT, false);
var dx = 213 - lyr.bounds[0].as("px") ;
var dy = 41 - lyr.bounds[1].as("px");
lyr.translate(dx, dy);

找到后,可以从图层的“textItem”属性访问其余的文本属性。例如:

lyr.textItem.contents = "some new text";
lyr.textItem.font = "fontname";

有关详细信息,请参阅 Photoshop 安装目录中的 Javascript 参考 pdf。

于 2015-01-15T21:04:22.193 回答
0

您可以像这样从 textLayers 和常规位置获取位置

function getPosition(layer) {
    if (layer.textItem) {
        var X = parseFloat(layer.textItem.position[0]);
        var Y = parseFloat(layer.textItem.position[0]);
    } else {
        var X = parseFloat(layer.bounds[0]);
        var Y = parseFloat(layer.bounds[1]);
    }
    return {x:X,y:Y}
}
于 2021-01-14T19:15:26.213 回答