Exit Out of Script Midway Using Cancel Button?

Not sure this is possible - but thought I would ask before I bang my head on the keyboard anymore. I have a script that throws up one menu, takes the input, throws up another menu, takes the input, then runs the last portion of the script using both inputs from the easier part of the script. Ideally I would like the ability to press cancel on either of the menus and have it drop out of the script. I looked into the Context command, but the problem with using context.fail or context.cancel is they both continue to run the rest of the script.

I use context.cancel(); currently, but this just moves on to the next menu creation and stores a null value in the variable for the prior menu.

Is an exit out of the script entirely possible? Thanks…

You could write your script so that you only do any further processing if the result of the show() is true. The script still runs to the end, but has no processing to do if the user selected cancel.

A script step is not a function in an of itself, so it cannot just return, you have to use functions or flow control statements (if, etc.) in Javascript to create a exit path.

In simple cases, just test if the prompt was successful based on the return value of the show() method, like:

let p = Prompt.create();
// setup prompt...
if (p.show()) {
   // do something
}
else {
   // user hit cancel button
   context.cancel()
}

If you have more complex flow, it’s often good to wrap your code in a function, so that you can use return statements to exit early and avoid too many nested if statements, something like…

let f = () => {
   let p = Prompt.create();
   // setup prompt...
   if (!p.show()) { // user cancelled
      return false;
   }
   
   // do something
   return true;
};

if (!f()) {
   context.cancel();
}

There are other ways to structure it, but that might help get you started.

Thanks to both of you - little late on the reply, but the tip of wrapping the code in a function is the direction I am headed here. If I get it working and it is clever enough, will post the code back here.