Export multiple drafts as individual text files

Is there a straightforward way to export a selection of drafts as individual text or MD files to Files or Dropbox? I don’t know how to split the JSON or CSV file back into individual files.

I have about 450 entries that I want to export.

I’ve hunted around, because this seems to be something that should be self-evident. But apparently not.

1 Like

I would suggest a script for this - to be executed as a Drafts action. Something along the lines of:

let fmCloud = FileManager.createCloud();
let yourFolder = 'YOUR_FOLDER';
fmCloud.createDirectory(yourFolder, "/");

let draftsGroup = Draft.query(YOUR_QUERY_HERE);
draftsGroup.forEach(function(myDraft) {
    fmCloud.writeString(myDraft.processTemplate('safe_title') + '.md', myDraft.content);
});

You’ll need to define the query - https://reference.getdrafts.com/objects/Draft.html, and specify your folder. This saves to iCloud.

I’ve not had time to test my code, but it should hopefully point you in the right direction.


Alternatively, press and hold on the select button on iOS, and then execute one action on all of the selected drafts (every draft in your current workspace view will be selected). In hindsight that’s probably easier than the code I thought up :laughing:

1 Like

Thank you for the generous help, Rosemary. And yes, I’d already concluded that I could do what I wanted through an action on a multiple selection, and did so. But i appreciate the script suggestion.

Hello

Sorry to revive this topic but I tried to do the same thing (export multiple drafts) in Dropbox.
I cannot see why it doesn’t work. I’m new to scripting and I would really appreciate some help if it’s possible.
Here is my code:

let db = Dropbox.create();
let path = “/myPath/”;
let draftsGroup = Draft.query("", “all”, [“myTag”]);
draftsGroup.forEach(function(myDraft) {
db.write(path, myDraft.processTemplate("[[safe_title]].txt"), myDraft.content, “add”, false);
});

Thé code runs but it does nothing in my dropbox !
To try to understand, I read this thread and ran this code which worked fine.

Thanks for your help

The best thing to do is to see what it says in the log.

But from the script you posted and without seeing the log details:

  1. There were a lot of smart/curly quotes which should have been straight/dumb quotes.
  2. You had a problem in the write function where there should have been a “+” rather than a “,”.
  3. You need to ensure you have at least one draft tagged “my Tag”.
  4. You need to ensure that there is a folder called “myPath” in your Dropbox root.
  5. The script won’t work if your Dropbox credentials are outdated.

The amended script below worked for me when all of the prerequisites were in place.

let db = Dropbox.create();
let path = "/myPath/";
let draftsGroup = Draft.query("", "all", ["myTag"]);
draftsGroup.forEach(function(myDraft)
		{
			db.write(path + myDraft.processTemplate("[[safe_title]].txt"), myDraft.content, "add", false);
			});

Hope that helps.

Hello and happy new year !

Thank you very much @sylumer for your code. It works well. Thank you also for your explanations they will help me understand what was wrong in my code.

Drafts is a great app and has a great community !

1 Like

Thanks @RosemaryOrchard! I only had to do a couple of small tweaks to your example to make it work for me.

Here is my result:

let fmCloud = FileManager.createCloud();
let path = 'Export';

let draftsGroup = Draft.query('', 'all');
draftsGroup.forEach(function(myDraft) {
	const title = myDraft.processTemplate('[[safe_title]]');
	fmCloud.writeString(`${path}/${title}.md`, myDraft.content);
});

Does anyone know how to add creation date to an export like this? I now have a folder of text documents from the last several years all “created today.” Thanks!

Try updating in the processTemplate step to include the created date as a datestamp.

myDraft.processTemplate("[[created|%Y-%m-%d]]_[[safe_title]].txt")

Ref. date template tags.

Hope that helps.

1 Like

Thanks – I was able to do a sloppier version of this, so this will be helpful for cleaning up. But I was hoping to preserve the actual metadata. I emailed support and it seems not possible within Drafts and will require learning whole new skillsets (AppleScript or some other scripting), which I guess is nice from a personal growth perspective but not from a UX perspective.

I was impressed recently by both SimpleNotes (which preserved all metadata since 2011 and exported to text file in one button on my phone) and Ulysses (which lets you drag notes out into finder and also preserves metadata). These are both similarish apps, so I was sad to see the feature missing from Drafts.

Drafts has metadata that isn’t present for text files such as creation location, so a blanket export from the Drafts database to a file isn’t necessarily a round peg in a round hole sort of action.

What Drafts is really good at is being extensible to ways of working and personal requirements. It also has a strong community of helpful people, including the developer. That combination puts it head and shoulders above many other apps in terms of capabilities.

I’m sure that if you started a thread on what you want to do exactly in terms of your export, and how far you can get/have got on your own, others would be happy to help. In fact there’s a good chance that many others could find it useful too.

I’ve used and loved Drafts for years, but it’s always been in a weird tension with my ever growing folder of plaintext notes. I was considering going all in on Drafts until I saw that I couldn’t easily leave. Portability > extensibility for me, personally. Still one of the best apps going though.

Funny solution: I imported everything into Bear and then immediately exported everything from Bear. Problem solved in ~6 min (not including a couple hours of googling).

Intermediate drafts user here - I’m considering exporting my drafts as a JSON file (which seems to preserve much of the metadata) and parsing that file with some sort of script. Has anyone tried this option (or is it not possible because of something I’m missing)?

I want my drafts to end up as individual .md files (preferably with metadata) that I can archive somewhere like Devonthink.

The File action step can be configured to write as “.md” files, maintain creation/modification dates, and assign drafts tags as Finder tags.

I would suggest you just configure an action using the File step to write the files the way you want them to the destination you want them (within iCloud Drive). You can then bulk export using “Run Action” on selected drafts in the draft list, or create another batch processing action that queues the action to run on all drafts in a workspace, or that match a query if you need a more automated export option.

1 Like

I found out, to my dismay, that my “Save as .md” export action item, which is customized from Save to file as… , does not modify the file meta data, so I lost the creation dates when I export my drafts into iCloud Drive. I exported about 10 notes from Drafts today and all the “Date Created” were set to today :man_facepalming:

I searched the forum and File action has a parameter to “maintain” Create Date. I need Export so that I can choose the folder to save the file. If I use File, I don’t think it will pop up a dialog box to choose the location.
Update: I can use File with Bookmark to save to specific location but I’d REALLY like to be able to choose my folder.

Appreciate any guidance.

TL;DR - is there a way to use Export function and preserve Create Date?

The Export step cannot set the creation date or other metadata. It is a system function, like a Save as dialog on Mac, Drafts is handing off the content to the system, and the file selection/write process is all handled out of the app’s process because of sandbox security.

If you want to update that information, you have to use the File step or a scripted action, and it has to write to the “Drafts” folder in iCloud Drive, or a bookmarked folder, so that Drafts has security permissions to access that file.

You could, in theory, write a scripted action that provided a custom file-picker, but it would be limited in scope to only one of those destinations.

1 Like

Thanks for confirming. I’m thinking of a hack using Hazel to monitor my Obsidian data folder and grabbing the [[created]] date from the first line of an exported Drafts to set the file meta data. A bit of a stretch, but I guess that might work out. Thanks again.

I found myself with similar needs to the folks in this thread, and wrote a script today that bulk exports all the drafts currently visible in your drafts list. It preserves the created/modified timestamps on export to the file system, and will use the first line of each draft as the filename.

It has a couple cautionary checks to make sure you have reasonable filenames and to give you a preview of the results before executing.

This is my first Drafts script… I hope it’s helpful. Thanks to @RosemaryOrchard for the sample code above, which got me started, and @sylumer for the suggestion (in another thread) on how to preserve creation dates.

1 Like