In a previous tutorial, I showed the basics of creating dialogs. In this post, I’ll elaborate on how to respond to a dialog, specifically how to cease execution when the user has selected cancel.
Here’s the code sample that was included in the aforementioned tutorial:
if (result == null) {
//do nothing
} else {
//use the value of result to proceed with the script
}
This example reflects code for custom XMLUI panel, but this technique can also be used for alerts, prompts, and confirmations.
There are 2 easy ways to halt the script if the user has selected cancel and the result is null:
- Call a function only if the result is not null.
- If the conditional statement above occurs inside a function block, call return to exit the function.
Here’s a code sample for method 1:
var dom = fl.getDocumentDOM();
var xpanel = dom.xmlPanel(absoluteFileLocation);
if (result == null) {
//do nothing
} else {
run();
}
You’d then define a function that execute all of the requisite code:
function run(){
//all the action takes place here
}
If the result is null, the function is never triggered.
Here’s a sample for method 2:
run();
function run(){
var dom = fl.getDocumentDOM();
var xpanel = dom.xmlPanel(absoluteFileLocation);
if (result == null) return;
//continue executing otherwise
}
Essentially, the entire process is wrapped in a function, so it’s easy to jump right out when needed.
Either method will do, sometimes one method suits a particular script.