更好的解决方案:
您可以使用 scriptUI 弹出一个带有图像的新窗口,然后将事件侦听器附加到该图像,不幸的是,您无法对文档本身执行此操作。在回调函数中将 X 坐标添加到数组中,如果数组长度大于 2,则隐藏窗口并运行处理函数。这样,您的第一次单击设置第一个点,第二次单击设置第二个点。第三次点击运行处理函数。如果你想变得非常花哨,如果你搞砸了,你可以使用右键重置整个事情,但我会留给你。
#target photoshop
function processImage(image, partWidth) {
// Your function to resize the image based on the part width
alert(partWidth); // For testing purposes
}
// Save the current unit preferences (optional)
var startRulerUnits = app.preferences.rulerUnits
var startTypeUnits = app.preferences.typeUnits
// Set units to PIXELS
app.preferences.rulerUnits = Units.PIXELS
app.preferences.typeUnits = TypeUnits.PIXELS
var doc = app.activeDocument; // or the next file in your file array if doing multiple
var clicks = [];
var width = 0;
var w = new Window("dialog", "The Image");
var img = w.add("image", undefined, File(doc.fullName));
img.addEventListener("click", function(k) {
if (clicks.length < 2) {
clicks.push(k.clientX);
}
else {
// absolute value so it doesn't matter if
// we click right or left side first
width = Math.abs(clicks[0] - clicks[1]);
w.hide;
processImage(doc, width);
}
});
w.show();
// Reset to previous unit prefs (optional)
app.preferences.rulerUnits = startRulerUnits;
app.preferences.typeUnits = startTypeUnits;
您需要做的就是使用类似 Photoshop Script 之类的东西循环浏览一堆图像- 调整文件夹(对话框)中的图像大小,然后将此代码放入循环中以批量执行此操作。
(不幸的是,如果图像大于屏幕尺寸并且缩小图像或添加滚动条对于 scriptUI 来说非常困难和hacky,您可能会遇到问题)
跛脚解决方案:
一种方法是让用户使用矩形选框工具(方形选择框)选择零件的宽度。然后您可以访问选择的维度。选择范围以如下形式存储在数组中。[top-leftX, top-leftY, bottom-rightX, bottom-rightY]
要获得选择的宽度,您可以从第二个 X 值中减去第一个 X 值。
示例片段:
#target photoshop
var bounds = app.activeDocument.selection.bounds;
var selectionWidth = bounds[3] - bounds[0];
alert(selectionWidth);
唯一需要注意的是用户必须在运行脚本之前进行选择。
使用 scriptUI 事件侦听器可能还有另一种方法。问题是它们必须应用于一个 scriptUI 对象,如窗口或面板。您可以尝试制作一个不透明度为 0 的大窗口,并向窗口添加一个单击事件以捕获鼠标坐标。老实说,使用 java 或 C# 或其他东西在 photoshop 之外更容易做到这一点。