Can I add to a .txt file at a specified insertion point?

Hi, I have what might be quite a niche use case that I’m hoping some smart person here can help me solve for. Here’s the tl;dr, and after that I’ll go into a few more specifics:

I want to be able to add the text of a draft to a Dropbox-hosted .txt file at a predetermined point

Phrased another way: I almost want to prepend to an existing file, but I need to leave in place the first four lines of that file.


OK, a bit of context. I use a blogging tool called Blot which operates by mirroring a Dropbox folder, and turning anything in that folder into a blog post or page. (It’s super neat, surprisingly versatile, and I enjoy using it a great deal.) In order to take advantage of some of Blot’s functions, I need to use the first few lines of an uploaded text file for metadata. For example, a typical blog post .txt file will start something like this:

Title: Title of Post
Date: YYYY-mm-dd
Permalink: permalink
Tags: tag1, tag2

(Side note: Drafts is fantastic in combination with Blot. I’ve built simple actions that push drafts, posts, and pages to Dropbox (and hence to my site) all with no problem. I basically have a Workspace set up in Drafts that acts as a dedicated writing space for my blog, with one-touch publishing actions.)

Now I’m trying to work out a way of updating a page on my site, rather than adding one. Let’s say I have a file named Wire.txt and it begins like this:

Title: Wire
Permalink: wire
Menu: no

1 Jan 2020, 12:00
Hello World

I’m trying to build a Drafts action that will add the text of a draft before ‘1 Jan 2020, 12:00’, but which will leave the three lines of metadata at the top of the file. What I’m looking for essentially is a command that tells Drafts to go to line 5 of the .txt file and insert:

[[date|%d %b %Y, %H:%M:%S]]  
[[draft]]  

It would be easy to append to the file, but I’m looking to keep a reverse-chronological log, so I need it to go at the top. If I choose prepend it pushes the metadata down, and the file stops working as intended at Blot’s end.

Any and all assistance greatly appreciated. If I’ve not made something clear by all means just ask.


For additional context:

  1. I was hopeful this might be solvable with the [[line|n]] content tag, but as far as I can see that’s referring to lines in the draft itself, not lines in the file you’re altering.

  2. I looked at the /replaceRange parameter to see if I could figure out an x-callback-url implementation, but that requires a defined length for the insertion, and I’m looking for the freedom to add a draft of any given length.

You would need to:

  1. Read in the Dropbox file (see Drafts scripting reference for Dropbox).
  2. Split the file content by line break.
  3. Join the first 4 lines of file content with the draft content and the remaining lines of file content (in that order - there are various ways to do this in JavaScript).
  4. Write that new set of content back to the Dropbox file to overwrite it.

Hope that helps.

2 Likes

I’m wondering if a general action that takes the new doc links, including a “file:” or similar reference, and injects the text would be handy.

Thanks for your considered input, it certainly provides me a path to work on.
(Look for my follow-up post where I get hopelessly lost!)

1 Like

If you do happen to get lost, here’s some more help :wink:

Expand for a script that might do it
//Initialise
const SPLIT_AFTER_LINE = 4;
let dbx = Dropbox.create();
let strFilePath = "/test/test.txt";

//Read file and split into an array
let strContent = dbx.read(strFilePath);
let astrContent = strContent.split('\n');

//Insert the draft content into the array
astrContent.splice(SPLIT_AFTER_LINE, 0 , draft.content);

//Recombine the array into text and overwrite the file
strContent = astrContent.join('\n');
dbx.write(strFilePath, strContent, "overwrite", false);

The above I don’t think will quite do everything for you, but if you also lookup draft.processTemplate(), and make an amendment or two, I think it might.

1 Like

Wow, this is stellar! Thanks so much for your help. To be honest I had got almost nowhere in an hour of trying and had all but given up on the idea.

I’ll plug this in this afternoon and give it a go!

1 Like

@sylumer Thank you so much, with the most minor of tweaks I have it working just as intended. Literally never would have figured it out without your help.

1 Like