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

drafts5://action?data=%7B%22uuid%22:%22F865A30E-0AB1-4F3D-AD7D-78A4D618EACA%22,%22steps%22:%5B%7B%22platforms%22:3,%22data%22:%7B%22script%22:%22%5C/*%20global%20draft%20*%5C/%5Cn%5Cn%5C/**%5Cn%20*%20Tags%20to%20Hashtags%5Cn%20*%5Cn%20*%20Converts%20Drafts%20tags%20to%20hashtags%20and%20appends%20them%20to%20the%20content%20if%20the%5Cn%20*%20hashtags%20aren%E2%80%99t%20already%20present.%5Cn%20*%5Cn%20*%20Has%20the%20same%20single-word%20hashtag%20limitation%20as%20similar%20scripts.%20Any%20Drafts%5Cn%20*%20tags%20containing%20spaces%20will%20be%20converted%20to%20a%20hashtag%20with%20no%20spaces.%20Why%5Cn%20*%20would%20you%20want%20a%20hashtag%20with%20spaces%20anyway?%5Cn%20*%5Cn%20*%20Basically%20does%20the%20opposite%20of%20@derekvan's%20%5C%22Tag%20drafts%20by%20hashtag%5C%22%20%3Chttp:%5C/%5C/actions.getdrafts.com%5C/a%5C/1Q7%3E%5Cn%20*%5Cn%20*%20Based%20on%20@kjaymiller's%20%5C%22Add%20Hashtags%20from%20Inside%20of%20Draft%5C%22%20action%20%3Chttp:%5C/%5C/actions.getdrafts.com%5C/a%5C/1ME%3E%5Cn%20*%5Cn%20*%20@author%20Chris%20Montgomery%20%3Cchris@montchr.io%3E%5Cn%20*%20@link%20https:%5C/%5C/actions.getdrafts.com%5C/a%5C/1Uu%5Cn%20*%5C/%5Cn%5Cnconst%20%7B%20content,%20tags%20%7D%20%3D%20draft;%5Cn%5Cnif%20(tags%20%26%26%20tags.length%20!%3D%3D%200)%20%7B%5Cn%20%20const%20spacelessTags%20%3D%20tags.map(tag%20%3D%3E%20tag.replace('%20',%20''));%5Cn%20%20let%20newTags%20%3D%20spacelessTags;%5Cn%5Cn%20%20const%20re%20%3D%20%5C/%23%5B%5C%5Cw%5C%5Cd%5D+%5C/g;%5Cn%20%20const%20hashtags%20%3D%20content.match(re);%5Cn%5Cn%20%20%5C/%5C/%20Get%20the%20draft%20tags%20that%20don't%20already%20have%20hashtag%20equivalents%20in%20the%20content%5Cn%20%20if%20(hashtags)%20%7B%5Cn%20%20%20%20const%20hashlessHashtags%20%3D%20hashtags.map(tag%20%3D%3E%20tag.replace('%23',%20''));%5Cn%20%20%20%20newTags%20%3D%20spacelessTags.filter(tag%20%3D%3E%20!hashlessHashtags.includes(tag));%5Cn%20%20%7D%5Cn%5Cn%20%20%5C/%5C/%20Append%20the%20new%20tags%20to%20the%20content%5Cn%20%20if%20(newTags.length%20%3E%200)%20%7B%5Cn%20%20%20%20const%20newHashtags%20%3D%20newTags.map(tag%20%3D%3E%20%60%23$%7Btag%7D%60);%5Cn%20%20%20%20draft.content%20+%3D%20%60%5C%5Cn%5C%5Cn$%7BnewHashtags.join('%20')%7D%60;%5Cn%20%20%7D%5Cn%7D%5Cn%22%7D,%22type%22:%22script%22,%22isEnabled%22:true,%22uuid%22:%22105826BF-8688-4577-AE5F-BB6ECD2CF017%22%7D%5D,%22backingPlatforms%22:3,%22shortName%22:%22%22,%22shouldConfirm%22:false,%22disposition%22:3,%22keyCommand%22:%7B%22optionKey%22:false,%22input%22:%22%22,%22controlKey%22:false,%22commandKey%22:false,%22type%22:%22action%22,%22discoverabilityTitle%22:%22Tags%20to%20Hashtags%22,%22shiftKey%22:false%7D,%22logLevel%22:2,%22groupDisposition%22:0,%22notificationType%22:2,%22tintColor%22:%22none%22,%22actionDescription%22:%22Converts%20Drafts%20tags%20to%20hashtags%20and%20appends%20them%20to%20the%20content%20if%20the%5Cnhashtags%20aren%E2%80%99t%20already%20present.%5Cn%5CnHas%20the%20same%20single-word%20hashtag%20limitation%20as%20similar%20scripts.%20Any%20Drafts%5Cntags%20containing%20spaces%20will%20be%20converted%20to%20a%20hashtag%20with%20no%20spaces.%20Why%5Cnwould%20you%20want%20a%20hashtag%20with%20spaces%20anyway?%5Cn%5CnBasically%20does%20the%20opposite%20of%20@derekvan's%20%5C%22Tag%20drafts%20by%20hashtag%5C%22%20%3Chttp:%5C/%5C/actions.getdrafts.com%5C/a%5C/1Q7%3E%5Cn%5CnBased%20on%20@kjaymiller's%20%5C%22Add%20Hashtags%20from%20Inside%20of%20Draft%5C%22%20action%20%3Chttp:%5C/%5C/actions.getdrafts.com%5C/a%5C/1ME%3E%22,%22keyUseIcon%22:true,%22icon%22:%22hashtag%22,%22visibility%22:480,%22backingIsSeparator%22:false,%22groupUUID%22:%220DCAC915-37EB-403D-951A-1241896D8C0C%22,%22assignTags%22:%5B%5D,%22name%22:%22Tags%20to%20Hashtags%22%7D

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.