Run A Merge on Selected Notes Via an Action

I’m trying to create an action that will merge multiple, manually selected Drafts.
The merged Draft is then copied to the clipboard and the merged Draft is then deleted.

I cannot find an example of how to run or include Merge in an action.

Can anyone point me in the right direction on this, please?

Thanks as always…

How about merging the content of selected drafts and foregoing creating and deleting a draft entirely?

Here’s some sample code. No error handling, just the basic approach in four lines of code, plus some comments to explain it line by line and a bit of whitespace to make it easier to read.

// Define something to separate drafts
const SEPARATOR="\n";

// Set up an empty array to hold the content of the selected drafts
let astrMerge = [];

// Populate the array with the content of the selected draft
app.currentWindow.selectedDrafts.map(draftSelected => astrMerge.push(draftSelected.content));

// Set the clipboard to the array of selected draft content, each set of content separated by the separator string 
app.setClipboard(astrMerge.join(SEPARATOR));

As for also pointing you in the right direction in terms of Drafts documentation, the key is to get to the selectedDrafts property.

With the exception of setting the clipboard, which I’m sure you must be generally familiar with at this point, the rest is vanilla JavaScript.

Hope that helps.

2 Likes

@sylumer very helpful! Digging in… thanks.

1 Like

A bit stuck now @sylumer on the “trashing” of selected Drafts.

I’ve reviewed the script language of TAD Actions to trash Drafts + the Drafts Script Reference, the community and Actions (and their script language) — few specifically trash selected drafts… and if they do it’s tied to a prompt.

I do not want a prompt - after the contents are on the clipboard I’d like to Trash the originally selected Drafts immediately.

The prompt scripting is throwing me off of how to follow your lead in the provided script.

Where should I be looking to achieve this desired behavior?

Also, I have “After success move to trash” enabled… but that is [1] only deleting the most recent Draft and [2] is leaving all other Drafts in a select state.

Many thanks.

Ah, you previously only mentioned deleting your merged draft after copying to the clipboard.

You just need to set the trashed property of each draft - no prompting.

// Define something to separate drafts
const SEPARATOR="\n";

// Set up an empty array to hold the content of the selected drafts
let astrMerge = [];

// Populate the array with the content of the selected draft and update each of the selected drafts to be trashed
app.currentWindow.selectedDrafts.map(draftX => {
		astrMerge.push(draftX.content);
		draftX.isTrashed = true;
		draftX.update();
});

// Set the clipboard to the array of selected draft content, each set of content separated by the separator string 
app.setClipboard(astrMerge.join(SEPARATOR));

 

That after success is for typical actions where you are operating on the draft loaded in the editor. It can work, but you would be using it as a batch operation to do so. In this case we’re bypassing that and interacting directly - this scripted operation was added not all that long ago (just a handful of versions back).

See the select & operations section here.

The script above is not going to leave the selection of drafts in the draft list because those drafts are going to move off into the trash folder. However, one of those selected drafts is probably going to have been loaded into the editor, and is going to remain there (even when trashed) until you load in another draft. You could consider whether to load an existing draft, or to create a new draft and load that (it won’t ‘save’ until you modify it, so it is just like most people’s default behaviour when openeing drafts).

Hopefiully that fills in your gaps.

1 Like

@sylumer you are correct - I overlooked that I’d have many drafts selected and some action needed to be taken on them.

I was eyeing these two lines of code:

draftX.isTrashed = true;
		draftX.update();

(I should have said) so - but again, they were part of a prompt and so I was unsure of how to use them.

This update works perfectly - thank you very much for the guidance!