Ingesting and parsing long text files

The goal: take one very long text file and slice it into multiple drafts.

The stretch goal: have tags recognized and place each slice into a corresponding workspace.

I’m hoping someone can point me in the right direction here; I’m a Drafts rookie. I take all of my notes with pen and paper and transcribe them into text files for a given day. My hope is that Drafts can ingest them, separate them into individual entries in Drafts, and let me push them to their appropriate endpoints.

For example:

  • Send Alice an email regarding what to bring to the potluck. #email

  • Write a passage on managing screen time for a five-year-old. #blog

  • Had coffee with Pete: discussed how to sell an aging German muscle car. #day_one, #spending_time_with_friends

These all appear in a single text file. I want the first one to become an email draft, the second one to become another text file saved to a particular directory in, and the third to become an entry in a Day One journal named according to the last hashtag.

My fear is that this sort of thing will undoubtedly require learning more Javascript than I currently no, i.e. very, very little. My hope is that this is the sort of thing which Drafts on the Mac may help address, as I would like to reduce my iOS dependence!

Mahalo :slight_smile:

I would approach this using JavaScript.

  1. Grab the content of the draft and split it into an array - one element for each line of the draft. Take a look at the string split() function and draft object documentation.
  2. Process each array element.
    1. Split off the last hash tag in each line. I’d be tempted to do this using a split again, but a find and substring based on the returned position approach would suffice.
    2. Based on the hash tag decide what to do with the other part of the line. Just a few conditionals here really and perhaps a catch all to decide what to do if it is unclear from the line content.
      • e-mail it (or create a draft ready to be e-mailed)
      • save it to Day One via an API call or the app’s URL scheme
      • save it to cloud storage - note that iCloud save will be limited to the Drafts folder structure if fully automated. I’d be tempted by Dropbox if it is being accessed more widerly than by Drafts.
  3. Archive the original draft being processed … doesn’t require JavaScript for this bit, just the right setting on the action.

Hope that helps.

Thank you for the prompt reply @sylumer. The first place I went looking for some reading to do was the Drafts Scripting page only to find that the scripting reference dead ends at a GitHub 404. Any idea where the updated reference material lives (cc @agiletortoise)?

Not sure how you ended up on that page, it’s no longer linked on the site, I thought. The reference is here, and is the “reference” link in the sidebar of the site.

Will fix the bad link.

@sylumer I’m having trouble getting started with your recommendations. Specifically, I don’t know where to locate the draft object documentation. I’m starting my script attempt with just the simple act of splitting one long draft into pieces, with a delimiter of an empty line.

Any pointers would be extremely helpful.

It is referencable from Greg’s link above in the side navigation.

Indeed. I’ve gotten that far. Here’s where I’m at:

// Transcript Splitter v0.1
// Win Condition: turn one long draft into separate drafts.

var transcript = draft.content;
var blocks = transcript.split(“empty line”);
var d = draft.create();
d.content = blocks;

Please let me know if this level of education isn’t something your interested in at the moment!

Reading the Mozilla array docs and attempting to debug JavaScript on a 9.7” iPad screen might be my limiting factor here

Think about what you need to do with your split up text. At the moment you are creating a single draft and working with that for all of your blocks of text.

Think about “looping” through each block (in your blocks array) to process it. For each block of text, create a new draft and set its content equal to that block.

Maybe have a look at this:

Hopefully the above will gI’ve you an idea about what to do next. :grinning:

Well, I didn’t so much learn as hacked. @nahumck again did a lot of the heavy lifting. I’ve amended his action as follows:

// Split at specified delimiter and remove initial characters

var delim = “\n”;

var text = draft.content;
var chunks = text.split(delim);
for (var chunk of chunks) {
var trimmedChunk = chunk.substr(2);
var d = Draft.create();
d.content = trimmedChunk;

So my next question is how to tell JS to look for a hashtag and put the corresponding draft in the appropriate workspace… Can you offer another breadcrumb for me to follow @sylumer?

I did suggest a ‘split’ for that in my original response…

Once you have it, it is a set of conditional options to make your choice. unless that is your hash tag is exactly the same name as the desired workspace in which no translation would be required.

1 Like

Thank you @sylumer, as always. While I’m attempting to learn how to integrate the JS syntax with the Drafts object syntax, I wonder if you (or anyone) has a good recommendation on best practices for developing JS-based actions within Drafts?

I honestly can’t recommend anything beyond naming things clearly (/understandably) and consistently.

I have worked as a programmer in several languages, in years past, but not JavaScript; I taught myself Javascript back in university in ‘94 I think. but one thing I do know is programmers often debate the merits of how to format code and what conventions to use. Just ask a group of them tabs or spaces… And that’s just how to lay out text and what to call functions and variables! Often they are agreed amongst a group who share code. But if you are just learning, then fundamentally I think it comes down to you.

However, on the basis that you are looking for ideas, I would start with a Google … … and I’m sure there are some JavaScript programmers around here who could give you a steer on some of the more generally accepted (/uncontested) good practice and some pitfalls to watch out for. But it is potentially a big topic as it extends to all corners and uses of the language.