[Solved] How to abort an action in the middle of a script?

This is probably super obvious but I can’t get my head around it.
I have an action which sets up a prompt (using Matt Gemmell’s ever useful MGCheckListPrompt Library) to get a title for a draft (I’m building - like everyone these days - a zettel like system with templates)
Now, if I cancel the prompt, I want to abort the entire action. Matt’s examples have a graceful structure to process the results, using context.fail() and context.cancel(). However, as stated in the reference guide:

It is important to understand that cancel() and fail() will not immediately stop script, just stop any further action steps from being performed.

So, when I cancel the prompt, subsequent actions are still taken and fail (prompt exit variables are not set, etc …) quite ungracefully.

I’ve tried exit() and break() but I’m not in a browser nor in the middle of a function …
I really need a way to say ‘I give up, please return to the draft having done nothing …’
Do I need to wrap my entire script in the contextual check for the prompt at the beginning?
As I write, it seems the obvious thing to do, but I’m wondering if it’s best practice.

Thanks for any help.

Let us start with your current script step. When you cancel in there, your code should be structured so that any further script processing you want to carry out is, and any that you do not want to carry out is not. Your condition check should drive this.

E.g. You might have an if cancelled do a-b-c else do x-y-z.

The script step will always continue to run to completion (including error) and you need to control that run flow in your code.

If you set the context in a-b-c as you describe, this will control subsequent action steps after the script step. They will not be executed on a cancel or fail.

Any previously queued actions will be run regardless. The current action does not affect the queue of actions.

Hope that helps.

@sylumer it’s still a little fuzzy, but I think I get the gist of it. I need to apply it to my particular case and it’ll make sense. Thanks!

I think some of the demo’s in the MGCheckListPrompt group show the principle. You need to have an if/then construct of some sort in your script to capture the cancel, like:

// Altered from "Demo: Recent Drafts" example
if (prompt.didShow) {
	if (selectedItems != null) {
		// do something		
	} else {
		app.displayInfoMessage("Prompt was cancelled.");
		// END ADDITION                
} else {
	app.displayErrorMessage("Something went wrong.");
	// END ADDITION                
1 Like