通过 Photoshop 获取图层的绘制区域尺寸 Javascript

Get painted area dimensions of a layer through Photoshop Javascript

如何通过 Photoshop Javascript 检查图层的绘制尺寸(不是 canvas 或图像大小)?到目前为止,我试图在 Photoshop Javascript 手册/参考指南中找到这个 属性,但运气不佳。下面是一张供您参考的图像,它为您提供了绘制区域尺寸(高度和宽度),但不知何故,我想通过脚本获取此信息,以便在从图像中获取此信息后必须稍后执行的某个操作。谢谢!

您可以使用 ArtLayer 边界 属性 获取包含边界框坐标的数组,然后使用这些值生成图层属性面板显示的宽度和高度。例如,我有以下文档,属性选项板显示宽度和高度为 4.17 英寸。

使用以下代码,我可以将坐标捕获为 x1、y1、x2 和 y2,然后从 x2 中减去 x1,从 y2 中减去 y1,以获得与“属性”选项板的输出相匹配的宽度和高度:

var activeDoc = app.activeDocument;
var layerBounds = activeDoc.activeLayer.bounds;
// this particular document, activeLayer.bounds returns:
// 0.41666666666667 in, 0.41666666666667 in, 4.5833333333333 in, 4.5833333333333 in
// x1, y1, x2, y2

// we specify the vlaue property so we only get the number without the ruler unit
var x1 = layerBounds[0].value;
var x2 = layerBounds[2].value;
var y1 = layerBounds[1].value;
var y2 = layerBounds[3].value;

// finally subtract x1 from x2 and y1 from y2 to get the widht and height and 
// fix the size to 2 decimal units to match the Properties palette
var layerPaintedWidth = (x2 - x1).toFixed(2); 
var layerPaintedHeight = (y2 - y1).toFixed(2);

// display the results in an alert dialog as 'W = 4.17, H = 4.17'
alert("W = " + layerPaintedWidth + ", H = " + layerPaintedHeight);

设置选择(例如:命令 + 选择图层),然后在跟踪选择尺寸的信息面板中填充宽度和高度值。