Create tag based on draft title

Ok, I tried but can’t figure out how to create tag by drafts title.

I have a prompt action and after that creates draft via script action.
My answer on first prompt becomes title.

d.addTag("[[title]]");
Ads title as tag, and not real draft name.

Anyone can help?
I have also tried:
d.addTag(${a1}

I have basically no knowledge about scripting, just downloading actions from directory and for the most time find where I need replace values to get what I want to achieve.

The addTag method just takes a literal string, it does not do any template magic, so you would need to pass the literal value of the title. You could do that like:

d.addTag(draft.title);

Possible you might want to use draft.displayTitle if you want a stripped version.

If you do want to derive a value by passing through the template engine, the method you need is draft.processTemplate. Which you could use like:

let title = draft.processTemplate("[[title]]");
d.addTag(title);
1 Like

Tags are generally used as a way to associate groups of things. If you are creating tags based on titles, do you have multiple drafts with the same title or are you creating lots and lots of unique tags for some reason?

Drafts is used in such a myriad of ways, I’m just intrigued by what your use case might be for wanting to do this.

Fantastic! Thank you Greg!

I experimenting with Zettelkasten method.
So now when I will read nonfiction book, prompt will ask me for title of book and authors name, insert few markdown headings like facts, new words etc. Also script will insert current date and time to know when I started to read this book.

I have already read few books and get around 20-40 drafts/zettl per book. So just wanted to automate this process a bit😊

It work almost, but what happens is tag will be added based on which draft was loaded in editor before I run action.

script:
// evaluate the prompt entries
let t1 = draft.getTemplateTag(‘t1_text’);
let a1 = draft.getTemplateTag(‘a1_text’);

// create template
const template = `# ${t1}

Author: ${a1}

Started reading:

Facts

`;

// create the draft
var d = Draft.create();
d.content = d.processTemplate(template);
let title = draft.processTemplate("[[title]]");
d.addTag(title);
d.update()

// load in editor and focus for editing
editor.load(d);
editor.activate()

Found out now that this script was actually created by @sylumer

It’s not easy to follow exactly what you have got. If you post code, please put t inside a pair of triple back ticks (```), and do consider sharing a link to the action itself.

The code sometimes is referencing draft, which is the draft loaded when you ran the action, and sometimes is referencing d, which is the draft that you are creating mid-action.

For the title specifically, I would choose Greg’s first suggestion of the title attribute an change this…

let title = draft.processTemplate("[[title]]");
d.addTag(title);

… to this …

d.addTag(d.title);

You also appear to be processing the template variable, which is based on two template tags. t1_text and a1_text. Unless those tags contain Drafts template tags, do keep in mind that there is no need to process the template variable to set d.content. Given that the look to be a title and author, my guess is that they will not contain Drafts template tags.

I suspect that the code above could be amended to something like this:

let d = Draft.create();
d.content = `# ${draft.getTemplateTag(‘t1_text’)}

Author: ${draft.getTemplateTag(‘a1_text’)}

Started reading:

## Facts

`;
d.addTag(d.title);
d.update()
editor.load(d);
editor.activate();

Does this work for you?

Sorry for mess, here is link to action.

Okay, that does help.

Here’s a reworked action:

  • I’ve renamed the prompt variables to make them a little more meaningful.
  • I’ve taken much of the basis of the code I posted above an applied it to the code of the action.
  • I’ve switched it to using the display title (as per Greg’s earlier suggestion) as it then strips off any leading Markdown. It could just as easily reference the same template tag as used in the template of course.

Does that now cover what you wanted?


As an aside, for Zettelkasten, do not forget you can use intra-draft linking rather than tagging. Using these cross-links will give you immediate access in regards to navigation, and you can use actions to generate and check back-links too; see the ThoughtAsylum - Management (Linking action section) and Examples: Cross-Linking action groups.

There’s quite a good thread around Zettelkasten in Drafts here:

But, you can find many Zettelkasten-related threads here:

https://forums.getdrafts.com/search?q=zettelkasten

1 Like

Yes, does exactly what I wanted. Thank you so much!

Yes I have read most of these posts. I have also installed few of ThoughtAsylum action groups, but haven’t explored it so much. The one I must use is ‘blank drafts to trash’. So handy for me. I use Alfred workflow which lets you activate menu bar item by typing name of item. This way I don’t need to make extra keyboard shortcuts and forget about them day later.

I have both drafts community and actions directory in my RSS, so I will say that I’m trying to be updated about new features and workflows in drafts.

support for zettelkasten was added back in spring i guess, but I feel like I still learning how to implement this method in my workflow. Since I’m VoiceOver user, I was already used to make short drafts which makes it easier to navigate than long draft/document.