在 photoshop 对话框中将 Javascript 函数参数与 onClick 一起使用不起作用

Using Javascript function parameters with onClick in photoshop dialog not working

本文关键字:onClick 一起 不起作用 参数 函数 对话框 photoshop Javascript      更新时间:2023-09-26

我这里有这个脚本(很多,对不起):

    var dlg = new Window('dialog', 'Templates',[1100,540,1465,720]);
    dlg.btnPnl = dlg.add('panel', [20,15,345,155], 'Please select your template:');
    dlg.btnPnl.Shoes = dlg.btnPnl.add('button', [20,55,300,45], 'Shoes, Hats and Belts', {name:'ok'});
    dlg.btnPnl.Bags = dlg.btnPnl.add('button', [20,85,300,45], 'Bags and Pouches', {name:'ok'});
    dlg.btnPnl.Shoes.onClick = extendCanvas(0.035, 1.035)
    dlg.btnPnl.Bags.onClick = extendCanvas(0.221, 1.221)
    dlg.show();
function extendCanvas(negative, positive) {
    #target photoshop
var doc = activeDocument;
var docWidth = activeDocument.width;
var docHeight = activeDocument.height;
var docName = activeDocument.name;
var guides = activeDocument.guides;
// If width is longer than height, extend height
if(docHeight/ docWidth < 1.044) {
    app.preferences.rulerUnits = Units.PIXELS 
    app.preferences.typeUnits = TypeUnits.PIXELS
    var stepOne = docWidth * 1.044;
    var stepTwo = stepOne * negative; // NEGATIVE
    var stepThree = stepOne - stepTwo;
    var newHeight = stepThree
    doc.resizeCanvas(null, UnitValue(stepThree, "px"), AnchorPosition.BOTTOMCENTER)
    doc.guides.add(Direction.HORIZONTAL, stepThree)
    doc.resizeCanvas(null, UnitValue(stepOne, "px"), AnchorPosition.TOPCENTER)
    doAction('[White Layer & Move]','Line Action')

// If height is longer than width, extend width    
    } else if(docWidth/docHeight < 0.957) {
        app.preferences.rulerUnits = Units.PIXELS
        toSubtract = docHeight * negative; // NEGATIVE
        newHeight = docHeight - toSubtract;
        app.preferences.rulerUnits = Units.PIXELS 
        app.preferences.typeUnits = TypeUnits.PIXELS     
        doc.resizeCanvas(UnitValue(docHeight * 0.957, "px"), null, AnchorPosition.BOTTOMCENTER)
        doc.guides.add(Direction.HORIZONTAL, docHeight)
        var docWidth = doc.width;
        var newHeight = docHeight * positive; //POSITIVE
        var newWidth = docWidth * positive; //POSITIVE
        doc.resizeCanvas(UnitValue(newWidth, "px"), null, AnchorPosition.TOPCENTER)
        doc.resizeCanvas(null, UnitValue(newHeight, "px"), AnchorPosition.TOPCENTER)
        doAction('[White Layer & Move]','Line Action')

    } 
dlg.close();
}

当我没有只有一个函数 extendCanvas 及其参数,而是有两个单独的函数用于鞋子和包包并且每个 onClick 调用它们时,它的效果很好。但是出于某种原因,当我将它们合并为一个并在 onClick 中向函数添加参数时,当我运行脚本时,它只是完全忽略 dlg(它不显示)而只是运行函数(也是不正确的,因为最终画布大小略有偏差)。

我完全感到困惑,非常感谢一些帮助。

提前感谢!

像这样添加点击处理程序:

    dlg.btnPnl.Shoes.onClick = function() {extendCanvas(0.035, 1.035);};