From d2d5d747df71744a63ad26e382846f0b376f7cc2 Mon Sep 17 00:00:00 2001 From: tianmo Date: Tue, 15 Sep 2026 18:30:57 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=BB=E5=B8=83=E5=B0=BA=E5=AF=B8=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jsx/scripts/画布尺寸修改.jsx | 171 ++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 Mac工具/Yeomai-Toolbox-CEP/jsx/scripts/画布尺寸修改.jsx diff --git a/Mac工具/Yeomai-Toolbox-CEP/jsx/scripts/画布尺寸修改.jsx b/Mac工具/Yeomai-Toolbox-CEP/jsx/scripts/画布尺寸修改.jsx new file mode 100644 index 0000000..fc4431c --- /dev/null +++ b/Mac工具/Yeomai-Toolbox-CEP/jsx/scripts/画布尺寸修改.jsx @@ -0,0 +1,171 @@ +#target photoshop + +/* + Uniformly resize the current canvas area. + Scale range: 0.1 to 5 + Default scale: 2 + + Layer contents are not scaled. The canvas expands or crops + equally from its center according to the entered scale. +*/ + +(function () { + function showMessage(text) { + alert(text); + } + + if (app.documents.length === 0) { + showMessage("请先打开一个文档。"); + return; + } + + var document = app.activeDocument; + var currentWidth = Math.round(document.width.as("px")); + var currentHeight = Math.round(document.height.as("px")); + + var dialog = new Window("dialog", "修改画布大小"); + dialog.orientation = "column"; + dialog.alignChildren = "fill"; + + // Method 1: uniformly resize with a scale slider and numeric input. + var scalePanel = dialog.add("panel", undefined, "方式一:等比例缩放"); + scalePanel.orientation = "column"; + scalePanel.alignChildren = "fill"; + + var scaleMode = scalePanel.add("radiobutton", undefined, "使用缩放倍率"); + scaleMode.value = true; + + var scaleGroup = scalePanel.add("group"); + scaleGroup.orientation = "row"; + + var scaleSlider = scaleGroup.add("slider", undefined, 2, 0.1, 5); + scaleSlider.preferredSize.width = 220; + + var scaleInput = scaleGroup.add("edittext", undefined, "2"); + scaleInput.characters = 6; + + scaleGroup.add("statictext", undefined, "倍(0.1 - 5)"); + + // Method 2: enter the target canvas width and height independently. + var sizePanel = dialog.add("panel", undefined, "方式二:指定宽高"); + sizePanel.orientation = "column"; + sizePanel.alignChildren = "left"; + + var sizeMode = sizePanel.add("radiobutton", undefined, "使用指定宽度和高度"); + + var widthGroup = sizePanel.add("group"); + widthGroup.orientation = "row"; + widthGroup.add("statictext", undefined, "宽度:"); + var widthInput = widthGroup.add("edittext", undefined, String(currentWidth)); + widthInput.characters = 10; + widthGroup.add("statictext", undefined, "px"); + + var heightGroup = sizePanel.add("group"); + heightGroup.orientation = "row"; + heightGroup.add("statictext", undefined, "高度:"); + var heightInput = heightGroup.add("edittext", undefined, String(currentHeight)); + heightInput.characters = 10; + heightGroup.add("statictext", undefined, "px"); + + var buttonGroup = dialog.add("group"); + buttonGroup.alignment = "right"; + var cancelButton = buttonGroup.add("button", undefined, "取消", { name: "cancel" }); + var okButton = buttonGroup.add("button", undefined, "确定", { name: "ok" }); + + var resultWidth = null; + var resultHeight = null; + + function parseNumber(text) { + return parseFloat(text.replace(",", ".")); + } + + function formatScale(value) { + return String(Math.round(value * 100) / 100); + } + + function updateControlState() { + scaleSlider.enabled = scaleMode.value; + scaleInput.enabled = scaleMode.value; + widthInput.enabled = sizeMode.value; + heightInput.enabled = sizeMode.value; + } + + scaleMode.onClick = function () { + scaleMode.value = true; + sizeMode.value = false; + updateControlState(); + }; + + sizeMode.onClick = function () { + sizeMode.value = true; + scaleMode.value = false; + updateControlState(); + }; + + scaleSlider.onChanging = function () { + scaleInput.text = formatScale(scaleSlider.value); + }; + + scaleInput.onChange = function () { + var value = parseNumber(scaleInput.text); + if (!isNaN(value) && value >= 0.1 && value <= 5) { + scaleSlider.value = value; + scaleInput.text = formatScale(value); + } + }; + + okButton.onClick = function () { + if (scaleMode.value) { + var scaleValue = parseNumber(scaleInput.text); + + if (isNaN(scaleValue) || scaleValue < 0.1 || scaleValue > 5) { + showMessage("请输入 0.1 到 5 之间的缩放倍率。"); + scaleInput.active = true; + return; + } + + resultWidth = Math.max(1, Math.round(currentWidth * scaleValue)); + resultHeight = Math.max(1, Math.round(currentHeight * scaleValue)); + } else { + var widthValue = parseNumber(widthInput.text); + var heightValue = parseNumber(heightInput.text); + + if (isNaN(widthValue) || widthValue <= 0) { + showMessage("请输入有效的画布宽度。"); + widthInput.active = true; + return; + } + + if (isNaN(heightValue) || heightValue <= 0) { + showMessage("请输入有效的画布高度。"); + heightInput.active = true; + return; + } + + resultWidth = Math.round(widthValue); + resultHeight = Math.round(heightValue); + } + + dialog.close(1); + }; + + cancelButton.onClick = function () { + dialog.close(0); + }; + + updateControlState(); + + if (dialog.show() !== 1 || resultWidth === null || resultHeight === null) { + return; + } + + try { + document.resizeCanvas( + new UnitValue(resultWidth, "px"), + new UnitValue(resultHeight, "px"), + AnchorPosition.MIDDLECENTER + ); + } catch (error) { + showMessage("修改画布失败:\n" + error.message); + } +}());