Action That Copies and Deletes the First Line of Text in a Draft

Hi all,
I am a relatively new Drafts user and I have minimal coding experience and no JavaScript experience. I have a personal workflow where I regurgitate thoughts with one thought per line. I then want to take each line and put do different things with it e.g. create a trello card, add a task to MS to do, insert a note to Evernote. To accomplish this, I am building an action that will copy and remove the first line of text from a draft to the clipboard. How would I do that?

This, from the action directory, may get you somewhere close to where you want to be. Admittedly, not exactly as specified.

https://actions.getdrafts.com/a/1I3

This script would do what you describe:

let lines = draft.content.split("\n"); // split into lines
app.setClipboard(lines[0]); // put first line in clipboard
lines.shift(); // array function to remove first line
draft.content = lines.join("\n"); // put remaining lines back together
draft.update(); // save changes to draft.

Hope this helps!

1 Like

I am a similar boat when it comes to being new to Drafts and new to Javascript ā€“ though I find it readable since I work (as a researcher not a programmer!) with Python.

What I want is a way to take a Drafts text, grab the first line, use that as the name of the file, and then delete that line from the saved text. E.g.:

2022-12-30-new-file
---
title: A New Post in a GitHub Pages Blog
layout: post
---

Lorem impsum

Would not have 2022-12-30-new-file at the beginning of the file.

I have searched this forum and I have tried searching the Drafts Directory, but it says every one of my searches is too specific, no matter when I get them down to one word.

You can do that without any JavaScript - just using the file action and Drafts template tags.

You can set the file extension of .txt to .md, etc. as you require.

You can use those tags in JavaScript too should you actually want to do more complex processing and saving within a script step:

let strFilename = draft.processTemplate("[[safe_title]].md");
let strContent = draft.processTemplate("[[body]]);

You can also just work with the draft content property and use standard JavaScript array manipulation techniques.

let strFileName = draft.lines[0];
let strContent = draft.lines.slice(1).join('\n');

The documentation on FileManager has examples for reading and writiong files if you need to know more about how to do that in Drafts and JavaScript.

Hope that helps.

1 Like

Thanks! Iā€™m happy to work with the built-in functionality! (I find javascript a bit daunting.) This will give me a good working start.