Select drafts from workspace; second selection defaults to first

Hi all! I’ve got a script that prompts the user to select a draft from among a selection of drafts (filtered by tag), and then shows the window a second time to select a draft among another selection of drafts (filtered by a different set of tags).

I have it set up so that if the user presses “Cancel” in the first selection window, the script responds that the user didn’t choose a first draft and then it ends. But, for the second selection window, if the user presses “Cancel,” the script doesn’t end at that part — it seems to still have the first draft in memory as a default if the user doesn’t choose the second draft. We can see this both because, if the user doesn’t choose a second draft, the script will show the first draft in an alert box, and also because if the user doesn’t choose a second script, Drafts reflects that the UUID of the first selection and the second non-selection are the same — again suggesting that Drafts in some way has the first selection in memory as the default value of the second selection.

Highly, highly probable that there’s some user error here, but can anyone discern what’s happening in this script to cause this issue?

function combineDrafts() {
    const draftOne = selectDraft({
        tagFilter: "tag1, tag2",
        requireAllTags: true,
        errorMessage: "Draft one not selected.",
    });
    if (!draftOne) return false;

    alert(draftOne.content)

    const draftTwo = selectDraft({
        tagFilter: "tag1, !tag2",
        requireAllTags: true,
        errorMessage: "Draft two not selected.", // We never seem to see this error message
    });
    if (!draftTwo) return false;

    alert(draftTwo.content)

    if (draftOne.uuid === draftTwo.uuid) {
        alert("Draft one and draft two cannot be the same.");
        return false;
    }

    function selectDraft({ tagFilter, requireAllTags = false, errorMessage }) {
        const workspace = Workspace.create();
        workspace.tagFilter = tagFilter;
        workspace.tagFilterRequireAll = requireAllTags;

        const selectedDraft = app.selectDraft(workspace);
        if (!selectedDraft) {
            alert(errorMessage);
        }

        return selectedDraft;
    }
    let d = new Draft();
    d.content = `Draft One:\n${draftOne.content}\nDraft Two:\n${draftTwo.content}`
    d.addTag("tag1, tag2");
    d.update();

    alert("New draft created!")
    return true
}

if (!combineDrafts()) context.fail()

Thanks in advance for your feedback!

Okay, I see some strange behaviour here too.

I’ve honed in on the selection and have this code.

const draftSelected1 = app.selectDraft();
if (typeof draftSelected1 === "undefined") alert("Draft 1 Selection Cancelled");
else alert(draftSelected1.uuid);

const draftSelected2 = app.selectDraft();
if (typeof draftSelected2 === "undefined") alert("Draft 2 Selection Cancelled");
else alert(draftSelected2.uuid);

If you execute it you will be prompted to select a draft twice, and it is simple to put together a set of results based on some basic tests.

Test First Selection Second Selection First Result Second Result
1 Cancel Canel Cancellation message 1 Cancellation message 2
2 Cancel Select Cancellation message 1 Selected draft 2 UUID
3 Select Cancel Selected draft 1 UUID Selected draft 1 UUID
4 Select Select Selected draft 1 UUID Selected draft 2 UUID

Test 3 is of course the interesting one and matches your scenario. In it the second call of the selection is returning the first selected draft object even though it should be returning undefined.

It looks to me like there’s an initialisation issue in the app.selectDraft() function. Maybe when it is cancelled it isn’t clearing out the return. So when it is first called it is not populated, but any cancellation after a selection returns the last selection.

In fact, if you extend the above code to a third selection this does match to the retention of the last selected draft:

Test First Selection Second Selection Third Selection First Result Second Result Third Result
1 Cancel Canel Select Cancellation message 1 Cancellation message 2 Selected draft 3 UUID
2 Cancel Select Cancel Cancellation message 1 Selected draft 2 UUID Selected draft 2 UUID
3 Select Cancel Cancel Selected draft 1 UUID Selected draft 1 UUID Selected draft 1 UUID
4 Select Select Cancel Selected draft 1 UUID Selected draft 2 UUID Selected draft 2 UUID

@agiletortoise, when you get the chance, can you check this and see if you agree there’s an issue with that function?

1 Like

Confirmed. This seems to be a bug, will fix.

1 Like