Using Drafts and Hazel to build a research library

Here’s my project: I want to build a library of plain text files where I will capture various items that I come across in my research or general reading. For example, I may find a section of a book or blog post about a topic of interest and then want to capture that in a way that’s quick and easy and that I can find again later.

The solution I want to build uses Drafts in iOS and macOS, and Hazel.

What I want to do is capture the text in Drafts with a tag that places that text into a workspace. I then want Hazel to find those files and place them into one of a number of folders depending on key words.

But at the moment I can’t figure out how to do that. It seems to me that the drafts files are not in a folder that I can find and so I can’t tell Hazel to look at that folder. Also when I expect as plain text the tags are not part of that text.

So… am I on the wrong path here or is there a way to make this work?

1 Like

Drafts uses a database for drafts, not files. To work with Hazel, you would have to export each draft to a file. You could maybe automate some of that, but I think you should go back to what you want, what it will achieve for you (i.e. why you want it), and then back to what your capture and storage options are.

As said above, drafts runs on a database not folders. Why not come up with a drafts action that a) tags the note b) exports it to Dropbox or ICloud. Then your hazel rule could act on it to sort to a folder. Alternatively, shift to another program like NValt that will run from folders.

I wonder if there’s a way to automate searching the database using AppleScript to create the files I want.

I’m reluctant to change my capture method because I primarily capture on the iPhone and Drafts is my place to capture stuff. That said, I could have an action that creates a file, as aroddick suggests.

Is there any way to automatically run an action when a certain tag is present or on every file that has been updated since the last time the action was run?

Not currently. I don’t believe that there is currently any AppleScript support, and actions support, which will include JavaScript based scripting, is due to come into beta after the i*OS13/Catalina release updates and queires have quietened down.

You can tag the draft, but it might be better to set it as archive or do both. Archive is there for when you have finished processing. A tag would help if you need to identify drafts that have been processed in a particular way.

My question would be at that point, what is the benefit you are expecting from exporting it rather than simply using it within Drafts? There are pros and cons either way, but that’s why I suggested previously, going back to examining what it is fundamentally you are looking to get from this in terms of how you want to use it.

Note also, for parity you would need Drafts on Mac to support actions to fully go macOS capable for the action based export approach that would be employed on i*OS.

No, there isn’t currently a way to do this natively in Drafts. However, if you desperately wanted to do this, I’m 99% sure that a way could be devised using a Mac running 24x7, Keyboard Maestro (for scheduling and unattended key/click interaction), the current version of Drafts and the inbuilt, non-action based, export functionality.

The reason I want to export rather than just keep the stuff in Drafts is that these items will become part of a database at some point alongside other file types like images, PDFs, and audio/video files. These files would all be in a shared location used by my business partner as well.

My main reason for wanting to employ Hazel in this task is to avoid having to think about where items go. To that end a set of rules that read the content of a file and then simply place that file in the appropriate folder/folders is the main requirement. Creating the file itself is something I can easily do manually and it would be a good time to set aside a moment to consider which tags should be attached in the text in order to send the file to the right places.

I’ve found an action that takes tags and makes them into hashtags in the body of a note but for some reason that isn’t working. Maybe due to me being on a beta. Also there’s an action made by the developer to take items with a tag and place them in a folder as text files based on that tag.

So for the time being my plan is to use some basic hashtags and use that action to file items in a “database” folder. Then I run a hazel action to sort the files in that folder based on content (if it has the hashtag #datapoint place in the data folder.

Okay. The file system based hosting of your notes is key. The utilisation of Drafts is perhaps personal preference, but given you are willing to go a bit more manual than fully automated certainly makes a big difference in how workable this is.

You note the action you are using isn’t working. I’m on the beta, so if you share the details for the action, I can take a look; but I’m not currently aware of anything in the beta that would affect an action iterating through the tags and writing them back as hash tags into the draft or an export.

It only took a few minutes to try something out, so in lieu of your original action, perhaps this one will suffice? Works with latest 14.5.44 Drafts beta.

The way I resolve this is that I also flag all active notes. (And I use hashtags in the note, using a drafts action to convert to tags). Then I have a workspace in drafts that pulls in all tagged & flagged notes. I send them all over in bulk. Once processed, I drop the flag. If I change the note again, I flag it again. (In my case, all my notes are sent over to Devonthink via an action…but a copy remain in drafts).

This is the action that isn’t working.

Hashtag Action


That doesn’t appear to be a valid hyperlink. I’ve tried on a couple of devices and nothing happens when I select the link.

Have you taken a look at the action I posted above that applies internal content hash tags to an export?

It’s generally easier to share an action using the Drafts directory. You can make it unlisted.

The action works for me on the latest beta. It adds appends the tags as hash tags at the end of the tag.

The differences to mine is it stripes spaces in tags that have them (that’s an addition of a l replace in the loop in mine if preferred), and it updates the content of the draft; mine only passes it to the export.

I did have a look at that action. It does work but only on one draft at a time. Ideally I could use something that does multiples.

Thanks through! It’s really helpful. I guess the ongoing practice would always be to export each item as I create it. I just think I’m probably going to end up needing to do mass at some point.

Assume I can edit this to specify a save location automatically?

Sorry, I don’t follow as to why it wouldn’t work on multiple drafts for you. I just tried it and it worked fine for me.i selected multiple drafts, then to run an action operation, selected the action, and it ran for each selected draft.

Absolutely. It was only intended to demonstrate a technique with the broadest possible file based output at the end. Switch out the final action for whatever your preference might be, but take note of the template tags being used in the existing final step.

When I tried it with multiple items I just ended up with a txt file with the word hashtag: in it…

That said I did this last night when I was quite tired so it’s possible I did something odd. I’ll try again when I’m in the office later.

Hmm. If it had no file name and just a.txt extension, that suggests the action was run against a single, entirely empty draft, wih no associated tags.

I tried it again and took a screen recording. Apparently they can’t be uploaded here but it didn’t work as expected. Blank txt file.

Okay, well here’s an example of it working for me.

I start with three drafts. Each draft has a line of content describing which tags it has and each has two tags of “blog”, “javascript”, and “lab”.

View the three drafts

Draft 1

Draft 2

Draft 3

I then select the three drafts in the drafts list.

View the selection

Three Selected Drafts

Next I select Operations and select the action I wish to run - “Export to Text File incl. Tags to Hash Tags”. This is the same action that I posted the link to earlier in the thread.

View the processing intiation

Operations List: Run Action

Select Action to Run

The action then prompts me once for each draft to save it as a file. I chose to save each of them in my Temp folder in iCloud. This is the step I would change so as to involve no interaction if I had a common export location identified.

View the processing

Save Draft 1

Save Draft 2

Save Draft 3

I checked that the files had been saved with the correct file names in the Temp folder via the Files app.

View the three output file entries

File Listing

I then checked that the contents were correct; which they were.

View the contents of the three files

Draft 1 File Content

Draft 2 File Content

Draft 3 File Content

From this particular test I see the action as working. If the above doesn’t immediately flag up anything odd about how yours is running, please try and share the full details of exactly what you are doing in your test.

1 Like

Ok. That worked! When I did it I didn’t use the function then run action steps. I ran the action in the sheet on the right. Don’t know why that would work differently but it did.

Thanks. This is an excellent action. Very useful for creating archives of Drafts that can be accessed from other places.

If you could post a screenshot of what you mean by “the sheet on the right” and perhaps a step by step description of what you did, I, or someone else on the forum, might be able to explain why it worked differently.