Photoshop 的 onChanging 函数的问题

Problems with Photoshop's onChanging function

我正在尝试根据 Photoshop 脚本 UI 复选框的状态更改一些标签文本。只是它根本没有更新。不知道为什么。

理想情况下,我希望它在勾选时显示 “一些文本”,在未选中时显示 “其他一些文本”。并动态变化。

这是我的代码

// DIALOG
// ======
var dlg = new Window("dialog");
   dlg.text = "Title"; 
   dlg.preferredSize.width = 180; 
   dlg.preferredSize.height = 100; 
   dlg.orientation = "column"; 
   dlg.alignChildren = ["center","top"]; 
   dlg.spacing = 10; 
   dlg.margins = 16; 

var statictext1 = dlg.add("statictext", undefined, undefined, {name: "statictext1"}); 
   statictext1.text = "Some static text"; 
   statictext1.justify = "center"; 

var checkbox1 = dlg.add("checkbox", undefined, undefined, {name: "checkbox1"}); 
   checkbox1.text = "Some text";
   checkbox1.value = true;
   

// GROUP1
// ======
var group1 = dlg.add("group", undefined, {name: "group1"}); 
    group1.orientation = "row"; 
    group1.alignChildren = ["left","center"]; 
    group1.spacing = 10; 
    group1.margins = 0; 

// add buttons
group1.add ("button", undefined, "OK");
group1.add ("button", undefined, "Cancel");

// Define behavior for when the slider value changes
dlg.checkbox1.onChanging = function() 
{
   var textArr = ["Some text", "Some other text"]
   var val = dlg.checkbox1.value;
   // Update the label text with the current checkbox value.
   dlg.checkbox1.text = textArr[val];
}

var myReturn = dlg.show();

Checkbox 控件触发 onClick 事件而不是 onChanging 事件。

触发 onChanging 事件的唯一控件是:

  • EditText
  • Scrollbar
  • Slider

因此考虑在您的脚本中更改此部分:

// Define behavior for when the slider value changes
dlg.checkbox1.onChanging = function() {
  // ...
}

改为:

// Define behavior for when the checkbox value changes
dlg.checkbox1.onClick = function() {
   var textArr = ["Some text", "Some other text"]
   var val = checkbox1.value ? textArr[0] : textArr[1];
   statictext1.text = val;
}

说明

onClick 事件的前一个函数体中,即读取的部分;

var val = checkbox1.value ? textArr[0] : textArr[1];

我们利用conditional (ternary) operator来检查checkbox1value属性。如果 checkbox1 被选中 (true) 我们将字符串 "Some text" 分配给 val 变量,否则如果 checkbox1 未被选中 (false) 我们将字符串 "Some other text" 分配给 val 变量。

其他更改:

还需要进行以下修改:

  1. 由于 checkbox1 的初始 value 设置为 true,即已选中,您需要从这里更改第 13 行:

    statictext1.text = "Some static text";
    

    对此:

    statictext1.text = "Some text";
    
  2. 同时考虑为 statictext1 设置 size 属性。正如您在下面的第 15 行中看到的,已添加以下内容:

    statictext1.size = [180, 20];
    

    这将确保它的宽度适合 "Some other text" 字符串。

修改后的脚本:

这是修改后的完整脚本:

// DIALOG
// ======
var dlg = new Window("dialog");
    dlg.text = "Title";
    dlg.preferredSize.width = 180;
    dlg.preferredSize.height = 100;
    dlg.orientation = "column";
    dlg.alignChildren = ["center","top"];
    dlg.spacing = 10;
    dlg.margins = 16;

var statictext1 = dlg.add("statictext", undefined, undefined, {name: "statictext1"});
    statictext1.text = "Some text"; // <--- Note: Also change this !
    statictext1.justify = "center";
    statictext1.size = [180, 20];   // <--- Note: Also set the width/height !

var checkbox1 = dlg.add("checkbox", undefined, undefined, {name: "checkbox1"});
    checkbox1.text = "Some text";
    checkbox1.value = true;


// GROUP1
// ======
var group1 = dlg.add("group", undefined, {name: "group1"});
    group1.orientation = "row";
    group1.alignChildren = ["left","center"];
    group1.spacing = 10;
    group1.margins = 0;

// add buttons
group1.add ("button", undefined, "OK");
group1.add ("button", undefined, "Cancel");

// Define behavior for when the checkbox value changes
dlg.checkbox1.onClick = function() {
   var textArr = ["Some text", "Some other text"]
   var val = checkbox1.value ? textArr[0] : textArr[1];
   statictext1.text = val;
}

var myReturn = dlg.show();