Repeat process with multiple drafts

I have a script from the Actions url website:
var text = editor.getText();
var output = text.replaceAll("- [ ]", “- [x]”)
editor.setText(output);
editor.save();

Works fine.

When I use the batch process for multiple drafts, (See: Tip: Performing Operations on Multiple Drafts), it only works on the first draft, not subsequent ones. Can someone help?

The editor object is used to manipulate text currently displayed in the editor - which is typically the current draft being edited.

When you run actions via operations (or some other ways, like via Shortcuts/URLs), the draft that the action is being performed on is not loaded in the editor…so you are re-processing the text in the editor multiple time (once for each draft you selected), but it has no affect on the other drafts.

The action would need to be written to manipulate the draft the action is being run on directly, in this case. This ought to accomplish what you want (and will still work on the current draft in the editor when run normally via the action list):

draft.content = draft.content.replaceAll("- [ ]", "- [x]")
draft.update();
1 Like

I really appreciate your help on this. Your explanation rings true, that is what I was imagining was he problem. but your suggestion does not work either. even for the case of a single draft note.

Oooops, my apologies, it works fine once I change the smart quotes to dumb quotes. Thanks for your help.

One more question — can I automate this further without a huge coding effort? All the drafts I want to change are in a workspace. I imagine there is a way to select a workspace, and do the batch process, but I am too much of a raw beginner to figure that out.

Fixed the quotes in my example.

This would be the code to loop over all the drafts in the inbox of a workspace named “MY-WORKSPACE”:

// find workspace and get inbox drafts in an array
let workspace = Workspace.find("MY-WORKSPACE");
let drafts = workspace.query("inbox");
// loop over array of drafts, processing each...
for (let d of drafts) {
    d.content = d.content.replaceAll("- [ ]", "- [x]")
    d.update();
}

That is with a specific workspace hard coded in the action. If you needed to select from your existing workspaces each time, it gets a little more involved to handle a prompt, but is also possible.

1 Like

Wow, that’s easy enough! Thank you.

Current workspace, though, would be easy (as I discovered yesterday for my “URL Factory” thread).