Wait for action to complete

I’m writing an action which uses a Script step to find an existing ‘Mustache’ formatted draft, load it into the editor and then run another action (which processes the loaded draft).

This works fine, but I want ‘pause’ the script until the action completes, then do more with the new draft created by the Mustache processing action. Am I right in thinking that I need to use async?

let d = Draft.find("0DE132A1-EC9A-4EB0-987F-B4B64FB6694F");
editor.load(d);

let a = Action.find("Mustache Prompt");
app.queueAction(a, d);

// I want to add extra code here to do more things with the new draft created by the "Mustache Prompt" action, but need to know how to wait for that action to complete (so that the new draft is present in the editor

Create one script step that loads the draft, a second “Include Action” step that runs the other processing action, then a third step for the remaining script.

Many thanks, I’ll give that a try!

I’ve given this a try and am further along than I was, but have hit an issue. I now have three action steps:

  1. Script: Loads the template draft into the editor (using it’s UUID)
  2. Include Action: Runs the Mustache Prompt action, which prompts for values for the Mustache variables in the template draft, then loads the resulting draft (a Taskpaper-formatted ‘project’) into the editor.
  3. Include Action: Runs the Things Parser action to create a new Things project from the Taskpaper draft.

Steps 1 and 2 work fine, the Taskpaper draft appears in the editor, but step 3 doesn’t seem to run at all. I get no success or error message in Drafts either.

Actions run on a draft, which is accessed via the global draft variable in scripts. In most cases – e.g. when you run an action from in Drafts, via the action list or bar – the draft in context for the action is the draft loaded in the editor at the time you trigger the action.

Loading a different draft in the editor during the action does not change the draft that is in context for the action.

Both of the actions you are including are written to read the content from the draft variable, so loading the draft in the editor in your script doesn’t work.

If you modified those actions to use editor functions instead, it would work. Look for where they use draft.content and change those to use editor.getText() and editor.setText() and this would work.

1 Like

I understand, thanks for the explanation. I’ve substituted draft.content with editor.getText() in the last Script step of the Things Parser action and that has done the trick.

I’m going to see if I can somehow copy the URL of the newly-created Things project next but I’m happy that I now have a working action.

Thanks again.

1 Like