I ran into this funny "gotcha" that is very subtle and could easily catch people off guard.
I'm working on a PS script that makes many changes to the document history, and I only want it to have a single history state change for the entire script. Photoshop has a function called suspendHistory which is defined in the JS Reference Guide as:
suspendHistory (historyString, javaScriptString)
Provides a single entry in history states for the entire script provided by javaScriptString. Allows a single undo for all actions taken in the script. The historyString parameter provides the string to use for the history state. The javaScriptString parameter provides a string of JavaScript code to execute while history is suspended.
Here is a simple example:
function main() {
alert('Hello world')
}
app.activeDocument.suspendHistory("My Script", "main()")
However, this doesn't work, and main() never runs. After scratching my head for a few minutes debugging my script and double checking my implementation I realized that it is caused by the transpiler prepending underscores to function names which breaks references:
var _main = function _main() {
try {
alert('Hello world');
} catch (_e) {
if (!_e.stack) _e.stack = "";
_e.stack += "at " + "main" + "\\n";
throw _e;
}
};
app.activeDocument.suspendHistory("My Script", "main()"); // ERROR
I'm not sure if renaming the functions during transpilation is necessary, but if it is, it would be nice to warn people of this behavior in the README so they don't get frustrated trying to figure out why basic code isn't working. This is a problem in any area where string references to functions are used.
I ran into this funny "gotcha" that is very subtle and could easily catch people off guard.
I'm working on a PS script that makes many changes to the document history, and I only want it to have a single history state change for the entire script. Photoshop has a function called
suspendHistorywhich is defined in the JS Reference Guide as:Here is a simple example:
However, this doesn't work, and
main()never runs. After scratching my head for a few minutes debugging my script and double checking my implementation I realized that it is caused by the transpiler prepending underscores to function names which breaks references:I'm not sure if renaming the functions during transpilation is necessary, but if it is, it would be nice to warn people of this behavior in the
READMEso they don't get frustrated trying to figure out why basic code isn't working. This is a problem in any area where string references to functions are used.