Export and trash all notes?

Hi, everyone!

Every day I select all drafts (which went from one tap to two in the latest update :expressionless:), export as text, e-mail them to myself, then send them all to the trash. I do this on iOS so I can access them on a computer that canā€™t run Drafts.

Is it possible to automate all of that? Iā€™m stuck on the first step of exporting all drafts. Iā€™ve poked around in the action editor, and searched the action database, but I can only find affordances for exporting individual drafts.

Thanks!

What platform are you looking to export on?

For example, on IOS, I can long press on Select on the draft list, tap Select All, then tap on Operations and select Export.

If you have a particular action, you could run that action against all of the selected drafts via the Run Action operation.

It would also be possible to build a script that could produce a tailored export where you could play about with the format for how the text is e-mailed to you to make processing the text at the other end easier.

Thanks. Iā€™m doing this on iOS, so when I select all drafts and hit Export, it exports them as a single text file, smushing all the drafts together. When I select all drafts and run an action (such as Mail or Send), it runs on all the drafts individually (one mail per draft, one share dialog per draft, etc), which would overwhelm the rest of my workflow because I usually have many drafts.

Can you describe in more detail what you want as an ideal?

It isnā€™t everything in one file e-mailed. It isnā€™t one e-mail per draft. So it is something in between or something very different.

Sure. My ideal is for it to all be in one e-mail, which is what I get now manually using steps like you described:

  • Open Drafts on iOS
  • Tap the paper icon in the upper left to open the list of notes
  • Long-press the ā€œSelectā€ button
  • Tap ā€œSelect Allā€
  • Tap ā€œOperationsā€
  • Tap ā€œExportā€
  • Tap ā€œDrafts Export (Text)ā€
  • Tap my e-mail app, which starts an e-mail with a text file attachment containing the content of all notes, one per line (I would prefer if the notes were in the body of the message, actuallyā€¦)
  • Type my own e-mail address as the recipient
  • Tap send
  • Back in Drafts, tap ā€œOperationsā€
  • Tap ā€œTrashā€

What I want is exactly that, but in less than 16 taps. If it were a Shortcut so I had a button on my home screen that did it all, that would be amazing!

However, if I make a custom action in Drafts, select several drafts, and invoke my action, it just runs that action once for every draft, which in the best case generates one e-mail per draft, but thatā€™s way too many e-mails.

Hope I make sense this time!

From this and the previous posts I think I get the gist, but let me double check.

You would like a one tap automation that gets the contents of all of your Drafts in the default inbox, and e-mails those to you with the contents being the body of the e-mail. Upon completion, all of those drafts would be trashed.

Sound right?

Yeah :slight_smile: If thatā€™s possible!

It is pretty straight forward to do what you have outlined with some script. There are other ways to do the e-mailing part certainly and even other ways to do the selecting and the scripting, but I think the below will work well for your needs.

Instructions

Put the script below into a script step in an action once you are happy with what it is doing and made the appropriate amendments. Iā€™ve commented the code, but Iā€™ll also explain the process.

Settings

At the top of the script are three settings that you should change to meet your own needs.

  • EMAIL_TO should be set to your e-mail address.
  • EMAIL_SUBJECT should be set to what you want the e-mail subject line to read. Iā€™ve set the default here to "From Drafts: " followed by the current timestamp.
  • EMAIL_VIA_DRAFTS is defaulted to trueand will let Drafts e-mail in the background. Set this tofalse` if you do not wish to use the Drafts e-mail service and want to view and confirm it before sending via your own e-mail client.
  • DRAFT_SEPARATOR is whatever you would like between your draft content in the e-mail to separate them. If you donā€™t want anything, just delete the content between the double quotes to set a ā€˜nullā€™ string.

Find the Drafts to Process

The next step gets an array (a list) of all of the drafts in your inbox. There are other options here about the ordering. If you want to change that, please refer to the query() documentation.

Get the Draft Content to Send

Next the script gets the content of each draft and builds a new array. This time it is an array of the content rather than the draft objects.

Prepare and Send the E-mail (Trashing DraftsOnly if it Worked)

Next, the script builds an e-mail to send. This utilises the settings from the start.

The last block of script attempts to send the e-mail. If it succeeds, the script will then reprocess each draft and set the isTrashed property effectively moving the draft to the trash.

Based on the outcome of sending the e-mail, a message will be displayed either telling you how many drafts were sent, or the e-mail failure status if the sending failed.

Add This to a Script Step in Your Action

//Settings
const EMAIL_TO = "your.address@domain.com";
const EMAIL_SUBJECT = "From Drafts: " + draft.processTemplate("[[time]]");
const EMAIL_VIA_DRAFTS = true;
const DRAFT_SEPARATOR = "\n\n%%% SEPARATOR %%%\n\n";


//Get the drafts to process in the order we want to proess them
let adraftToProcess = Draft.query("", "inbox", [], [], "modified", false, false);


//Build an array of draft content
let adraftContent = [];
adraftToProcess.map(draftCurrent => adraftContent.push(draftCurrent.content));


//Configure the e-mail
let email = Mail.create();
email.toRecipients = [EMAIL_TO];
email.subject = EMAIL_SUBJECT;
email.body = adraftContent.join(DRAFT_SEPARATOR);
email.sendInBackground = EMAIL_VIA_DRAFTS;

//Send the e-mail
if (email.send())
{
	//Mail sent successfully, trash the drafts
	adraftToProcess.map(draftCurrent => 
		{
			draftCurrent.isTrashed = true;
			draftCurrent.update();
		});
	
	//Display a confirmation message
	app.displayInfoMessage("Drafts sent and trashed: " + adraftToProcess.length);
}
else
{
	//Failed to send, display and log the status
	console.log(email.status);
	app.displayErrorMessage(email.status);
}

Triggering the Action Outside of Drafts

Once you create your action, in terms of running it, the simplest option would probably be to use a Drafts widget. You can set a widget target area/button to execute a Drafts action. The alternative would be to use a Shortcut and save it to your home screen, or use a Shortcuts widget. The Run Action with Text would probably be the best option, and you can pass in no text at all as the action isnā€™t processing content for a single draft, but rather a collection of existing drafts.

Again, there are other trigger options, but I think that one of these should suffice for your use case.

Final Points

I havenā€™t posted an action because this is sending information automatically, and is trashing drafts. Two things you should be acutely aware of when reading the above.

I want you to be happy with it all before running it. At the end of the day I am not responsible for what happens to your drafts, you are, and so I want you to understand what is going on rather than taking it on blind faith for this particular one. Retrieving drafts from the trash is probably no big deal, but sending off content automatically by e-mail, I would rather you were fully aware of what is going where.

Hopefully, it is everything you were after, but please just post an update on if it is a match or if there is anything that it is not doing that you expected that it would do.

1 Like

Perfect. It works perfectly with a home screen widget. Thank you so much for all the pointers!

Iā€™m really loving this. Thanks again.