Action

Add to Obsidian Daily Note

Posted by FlohGro, Last update almost 2 years ago

created by @FlohGro / more on my Website

Add to Obsidian Daily Note

This action will add the contents of the current draft to the todays Daily Note in Obsidian.
You can choose if the action should append or prepend the content and if you want to add a text prefix (e.g. to add a todo or a bullet point to the daily note). If you want to use different prefixes, you can duplicate the action and configure and rename the duplicate accordingly.

[Configuration]

Before you can use the action you need to configure it depending on your setup in Obsidian and personal preference.

All configuration parameters are adapted in the „Define Template Tag“ steps in the Action. To change the configurations edit them to match your setup - don’t change the names of the template tags since the action won’t work anymore afterwards. The following parameters must be adapted:

  • obsidianVaultBookmarkName: the name of the folder bookmark that will be created to access the files in your vault (see the Drafts documentation for more information about Folder Bookmarks). When you run the Action the first time it should prompt you to select a folder for that bookmark. Default: Obsidian Vault
  • dateFormat: the date format of your daily notes in Obsidian. You need to use the Drafts specific notation documented here Default: %Y-%m-%d which matches YYYY-MM-DD in Obsidian
  • dailyNotesFolderPath: the path where obsidian creates the daily notes (matches the setting for „New file location“ in the daily notes settings section). Default: empty
  • textAddPrefix: an optional prefix that will be prepended to the content of the Draft
  • textAddMode: you can choose if the action should prepend or append the draft to the daily note - only prepend or append are allowed. Default: append

IMPORTANT: If you‘re running the Action the first time and it shows an error that the file is not existing even when the file exists in Obsidian then please go into the Settings of Drafts, navigate to „Bookmarks“ and make sure that the configured bookmark name is not set to „-unknown-“. If it is set to Unknown then tap „Select Folder“ and navigate to your Obsidian Vault in the files UI, select it and tap „Done“ in the upper right corner.

[Usage]

The action directly accessed the files so Obsidian won’t be opened when adding something to the daily note. If you didn’t open obsidian on that day, the daily note file will not be created or synced into the folder. Make sure to open Obsidian on the device if you see the alert that the file is missing. This is to prevent sync issues and ensure that potential templates for daily notes are applied first.

Simply run the the Action to add the content of the current draft to your Daily Note in Obsidian. When you e.g. want to use the Action to add tasks to your Daily Note then you need to configure - [ ] as textAddPrefix. If you have different usecases, just duplicate the Action and configure it differently.


If you find this useful and want to support me you can

Buy Me A Coffee

Steps

  • defineTemplateTag

    name
    obsidianVaultBookmarkName
    template
    Obsidian Vault
  • defineTemplateTag

    name
    dateFormat
    template
    %Y-%m-%d
  • defineTemplateTag

    name
    dailyNotesFolderPath
    template
  • defineTemplateTag

    name
    textAddPrefix
    template
  • defineTemplateTag

    name
    textAddMode
    template
    append
  • script

    // add to obsidian daily note
    
    const obsidianVaultBookmarkName = draft.processTemplate("[[obsidianVaultBookmarkName]]")
    const dateFormat = draft.processTemplate("[[dateFormat]]");
    
    let dailyNoteFileName = draft.processTemplate("[[date|" + dateFormat + "]]") + ".md";
    
    let dailyNotesFolderPath = draft.processTemplate("[[dailyNotesFolderPath]]") + "/";
    
    dailyNoteFileName = dailyNotesFolderPath + dailyNoteFileName //"2022\.10\.25.md"
    let bookmark = Bookmark.findOrCreate(obsidianVaultBookmarkName);
    
    
    const configEntryPrefix = "- "
    const textAddPrefix = draft.processTemplate("[[textAddPrefix]]");
    const textAddMode = draft.processTemplate("[[textAddMode]]");
    
    let fm = FileManager.createForBookmark(bookmark);
    
    if (fm.exists(dailyNoteFileName)) {
        // file exists
        let fileContent = fm.readString(dailyNoteFileName)
        if (textAddMode == "append") {
            fileContent = fileContent + "\n" + textAddPrefix + draft.content
        } else if (textAddMode == "prepend") {
            fileContent = textAddPrefix + draft.content + "\n" + fileContent
        } else {
            console.log("invalid text add mode")
            alert("invalid text add mode configured - only \"append\" or \"prepend\" are allowed")
            context.fail()
        }
        if (!fm.writeString(dailyNoteFileName, fileContent)) {
            console.log("failed writing content to file")
            alert("failed writing content to file - this should not happen. please contact FlohGro via Twitter or the Drafts Forum.")
            context.fail()
        }
    
    } else {
        // file does note exist
        console.log("daily notes file does not (yet) exist")
        alert("daily notes file does not (yet) exist, create it in obsidian")
        context.fail()
    }

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.