I wish to place text after a ## tag in an obsidian file

I have a daily note file that has a ## Journal tag in the midst of it. I wish drafts to find that tag and add its contents after the entries after the tag. there will always be blank spaces after the journal entry.

so my obsidian daily has

Journal

hh:mm I did this
hh:mm I did that

So I want drafts to find all this, find the space under the last entry and enter my text with a time stamp. Thanks

When you say “in the midst”, what determines the end of the journal section? It could be after the last time stamped entry, but presumably there is no guarantee there will be any.

What file name format is your daily note? yyyy-MM-dd.md?

Is it in the root of your Obsidian vault or a subfolder?

Do you want Obsidian to be opened to update it, or just update the file directly?

the file is my daily note in Daily/YYY-MM-DD.md
I would like to put the entry after the last time stamp. there will always be a couple of blank lines after the last time stamp entry. I and give any other information you thin you be helpful. I would like drafts to open the file. Obsidian may or may not be open in the dock when I do this. this is on MacOS Monterey

I think this action should do what you want.

Set Up

In order for the action to work, it has to know the location of the obsidian vault you want to use.

You need to have a Drafts Bookmark set up that points to the root of your Obsidian vault. in the script, the name of the bookmark is specified as “obsidian_drafts”. You can name your bookmark the same, but if you name it something different, you MUST change the OBSIDIAN_FOLDER_BOOKMARK constant to match the name that you wish to use.

Features

  • Works directly with the Markdown file, so is not opening Obsidian and relying on URL schema to drive the changes.
  • Automatically trims whitespace from content.
  • Will add multiple lines of content as multiple timestamped entries.
  • Basic options are available as constants in the insertintoDailyNote() function.
  • The default action does no processing of the draft after completion, but you could modify the standard action options to archive or trash a draft once processed if required.

Action Steps

The action consists of a single script step.

function insertIntoDailyNote(p_strJournalAddition)
{
	// Settings
	const ENTRY_SPACER = " ";
	const OBSIDIAN_FOLDER_BOOKMARK = "obsidian_drafts";
	const JOURNALPATH = "/Daily/";
	const REGEX_SECTIONS = /(.*)(## Journal\n)(.*)(\n\n\n)(.*)/gs;

	// Set up file access and get file content
	let bmObisidian = Bookmark.findOrCreate(OBSIDIAN_FOLDER_BOOKMARK);
	let fmObsidian = FileManager.createForBookmark(bmObisidian);
	let strDailyNotePath = JOURNALPATH + draft.processTemplate("[[date]]") + ".md";
	let strDailyNoteContent = fmObsidian.readString(strDailyNotePath);

	// If the daily note does not exist, log it and bail out
	if (strDailyNoteContent === undefined)
	{
		console.log(strDailyNotePath + " not found");
		return false;
	}

	// Build new content and add it to the daily note
	// - Input is processed per line and whitespace is trimmed
	// - Inserting is before the two blank lines first found after the H2 Journal heading (Markdown)
	// - Boolean success of the file updating is returned.
	let strContentToInsert =  p_strJournalAddition.split('\n').map(s => draft.processTemplate("\n[[date|%R]]") + ENTRY_SPACER + s.trim()).join("");;
	strDailyNoteContent = strDailyNoteContent.replace(REGEX_SECTIONS, "$1$2$3" + strContentToInsert + "$4$5");
	return fmObsidian.writeString(strDailyNotePath, strDailyNoteContent);
}

// Add current draft to dail note in Obsidian as journal entries
if (insertIntoDailyNote(draft.content)) app.displaySuccessMessage("Daily Note Updated");
else app.displayErrorMessage("Failed to Update Daily Note");

Testing

I have successfully tested it with a draft that meets the criteria you have specified. This has included processing both single line draft content and multi-line draft content.


Hopefully, that covers everything.

Seems that it does not work on my iPhone but only on Mac. How can we edit the script to make it work on iPhone Drafts APP?

I believe it should work. As you can hopefully see, there was nothing platform specific.

  1. Did you set up your bookmark?
  2. Is the bookmark pointing to the correct location?
  3. Are there any errors?

I made an action that probably also does what you want.

I have to add that adding to a section is kind of „complex“ due to the variety of user setups. As @sylumer already asked some relevant questions.

If his action works for you, just keep using it :slight_smile:

You can try this one if you want: Add to Obsidian Daily Note Section | Drafts Directory