Photoshop脚本开发疑问:“cancel”是否为关键字或保留字?
Great question! Let's break down what's happening here:
The core reason
Photoshop's ExtendScript dialog system (using the Window("dialog") class) has built-in logic for standard button text values. When you set a button's text to one of these predefined strings—like "OK", "Cancel", "Yes", or "No"—the system automatically attaches default behavior to the button:
- A button labeled
"OK"will automatically close the dialog and makedlg.show()return1 - A button labeled
"Cancel"will automatically close the dialog and makedlg.show()return2
When you change the button text to "Can", it's no longer recognized as a standard cancel button. The system doesn't apply that default close/return logic anymore, which is why the button stops working unless you add a custom onClick handler.
Is "Cancel" a reserved keyword?
Nope—"Cancel" isn't a JavaScript or ExtendScript reserved keyword. It's just a special text string that Photoshop's dialog system watches for to trigger default button behaviors. Think of it as a convention the scripting engine uses to map common button labels to expected actions.
Example breakdown
Your original code works because:
var button2 = group1.add ("button", undefined); button2.text = "Cancel";
The engine sees "Cancel" and knows "this button should act as the dialog's cancel control".
After changing to "Can", you have to explicitly define the behavior:
button2.onClick = function() { dlg.close(); dialogResponse = "cancel"; }
This manually replicates the default logic that the system would have provided for the "Cancel" label.
内容的提问来源于stack exchange,提问作者Ghoul Fool

