Automatically inserting the title of a Markdown file inside the file itself: any way?

Greetings,

because I usually create a Markdown document and insert the filename as the first line of the text inside the document, I wonder if there is a way to do it automatically, and how, in order to reduce the steps needed to create the file.

This is my ideal solution:

  1. I hit a shortcut (my present shortcut to create a new MD is CMD + Shift + M)
  2. A new Markdown file is created
  3. The filename is a title of my choice + the date time YYYYMMDDHHmm
  4. The first line of the text is:

filename YYYYMMDDHHmm

I add the hashtag so that the title of the file (the first line) is a H1.

I suppose I can play with placeholders and templates, but what’s the best way to reduce the steps?

Thank you!

1 Like

This thread might be useful:

1 Like

I was doing the exact same thing. My current workflow is an AppleScript that I invoke via Alfred. Here’s the script in Alfred.

on alfred_script(q)
	tell application id "DNtp"
		set myHome to system attribute "HOME"
		set templatePath to (myHome & "/Dropbox/DevonThink/Templates/Zettlekasten Note.md")
		set thisGroup to incoming group
		set thisText to (q)
		set theDate to do shell script "date +%Y%m%d%H%M%S"
		set itemName to (thisText & " - " & theDate)
		set theRecord to import templatePath name itemName placeholders {|%recordName%|:itemName} to thisGroup
		try
			open window for record theRecord
			activate
		on error error_message number error_number
			display alert "Had trouble opening the created record"
			return
		end try
	end tell
end alfred_script

This lets me invoke it with a quick keyboard shortcut for Alfred and then a Keyword configuration.

2 Likes

FWIW, I also use a script for creating new files from templates, invoked via a Keyboard Maestro shortcut. The use of a script is an opportunity to do additional substitutions. I also have it pop up a list of templates from which I choose the one I want in a given situation. Here it is, in case it’s useful for ideas:

Note: the code is not brilliant, and could use redesign and refactoring.