Action

Add to Obsidian Note

Posted by FlohGro, Last update 28 days ago

created by @FlohGro / more on my Blog

Add to Obsidian Note

This action adds the content of the current draft to a selectable note in your Obsidian vault.
You can configure the notes which should be the options in the prompt very easily.

Follow the instructions to configure the action before you use it.

[Configuration]

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

The 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
  • configurationDraftTitle: the title of the Draft which is used to configure the Notes for this Action (see further details below) - this is just the title of the Draft which the Action will search for. Make sure this is unique if you need to change it. Default: Add to Obsidian Note Action Configuration
  • 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
  • textAddPrefix: an optional prefix that will be prepended to the content of the Draft. Default: empty

The first time you run the action, it will create a new draft titled Add to Obsidian Note Action Configuration (Default setting). Think of this draft as the engine or library behind the Drafts Action. (You can tag and archive this draft in the Drafts after configuration to hide it in your day to day work.)

Now you can configure to which Obsidian Note this Action can add content to:

  • Open your Obsidian Vault and search the note you want to add content to
  • open the command palette and search for Copy file path - run that command
  • Navigate back to the Add to Obsidian Note Action Configuration Draft and paste the copied file path as new bullet point
  • Remove the „.md“ at the end of the line

IMPORTANT: If you‘re running the Action the first time after you configured it 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.

Here is a small example to illustrate how a configuration Draft could look like:

# Add to Obsidian Note Action Configuration

- lists/movies to watch
- lists/books to read
- Drafts Action Ideas
- Gratitude
- 2 areas/FlohGro/Appreciations

ADDITIONAL NOTES

You might have different use cases like adding tasks, bullet points, text only,… to different notes or even different vaults.

You can of course duplicate this Action in Drafts and configure it differently for those use-cases.

If you want to use different vaults then in the duplicate you must change the configuration parameter obsidianVaultBookmarkName and create another bookmark for that vault in drafts. You might also want to have different Configuration Drafts for the related notes. Therefore change the parameter configurationDraftTitle to a different title (please ensure you change the name to something unique in both actions; if you only append a „2“ to the title this will not work for the original action if you don‘t add a „1“ there). I suggest to use titles you can easily distinguish like adding the vault name.

If you e.g. want to add tasks or just bullet points to the same configured notes then you can just duplicate the Action and change the textAddPrefix depending on your preferences.

If you struggle with more complex setups then just reach out to me in the Drafts forum or on Twitter

[Usage]

The action directly accesses the files so Obsidian won’t be opened when adding something to the configured notes.

Simply run the the Action to add the content of the current draft to a note of your choice. A prompt will appear with all the paths to the notes you configured. Select the note where you want to add the content to. The Action will then add (append/prepend) the content with the configured textAddPrefix as prefix.

This action supports you for many different use cases. You can e.g. append / prepend notes and thoughts to a list of gift ideas, a list of places you want to visit or a list of things you are grateful for. You can also create a note in Obsidian for regular meetings you have and add things you want to mention in the meetings to this document by using this action.


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
    configurationDraftTitle
    template
    Add to Obsidian Note Action Configuration
  • defineTemplateTag

    name
    textAddMode
    template
    append
  • defineTemplateTag

    name
    textAddPrefix
    template
  • script

    const configurationDraftTitle = draft.processTemplate("[[configurationDraftTitle]]")
    const obsidianVaultBookmarkName = draft.processTemplate("[[obsidianVaultBookmarkName]]")
    
    let bookmark = Bookmark.findOrCreate(obsidianVaultBookmarkName);
    
    
    const configEntryPrefix = "- "
    const textAddPrefix = draft.processTemplate("[[textAddPrefix]]");
    const textAddMode = draft.processTemplate("[[textAddMode]]");
    let foundDraft = Draft.queryByTitle(configurationDraftTitle)
    
    if (foundDraft.length == 0) {
        // the configuration draft is currently not existing, we need to create it for the user so that he/she can configure it later
        let newDraft = new Draft()
        newDraft.content = "# " + configurationDraftTitle + "\n\n- "
        newDraft.update()
    
        console.log("created configuration draft with title: " + configurationDraftTitle + " since it was not existing")
    
        alert("created configuration draft with title: " + configurationDraftTitle + " since it was not existing\n\nplease configure it according to the actiondescription and run the action again")
        editor.load(newDraft);
        context.cancel()
    
    } else if (foundDraft.length > 1) {
        // there are several drafts with the specified title. The user needs to cleanup the mess to run this again.
        console.log("more than one configuration draft with title: " + configurationDraftTitle + " exist")
    
        alert("more than one configuration draft with title: " + configurationDraftTitle + " exist\n\nplease clean up the mess (merge these two Drafts and run the action again")
        context.cancel()
    } else {
        // the configuration draft draft exists, which is great. We can work with that
    
        let p = new Prompt()
    
        p.title = "add to which note?"
        p.message = "\"" + draft.title + "\""
        let entryCount = 0;
        let lines = foundDraft[0].content.split("\n")
        lines.splice(0,1)
        for (line of lines) {
    	    if (line.length > configEntryPrefix.length) {
    // there is data in that line
    line = line.slice(2)
    
    entryCount++;
    p.addButton(line)
    }
        }
        if (entryCount == 0) {
            // no matches where found, the prompt won't contain any useable button, the user needs to insert at least one Craft Markdown Deeplink
            console.log("no markdown entries for Obsidian Notes where found in the configuration draft with title: " + configurationDraftTitle)
            alert("no markdown entries for Obsidian Notes where found in the configuration draft with title: " + configurationDraftTitle + "\n\nplease add entries for Obsidian Notes as described in the description and run the action again")
            context.cancel()
        } else {
            // there one or more matches, show the prompt and proceed according to the users selection
    
            if (p.show()) {
    	       let selectedNote = "/" + p.buttonPressed + ".md"
    	       // textAddMode 
    	       // textAddPrefix
    	       let fm = FileManager.createForBookmark(bookmark);
    	       
    	       if(fm.exists(selectedNote)){
    	       // file exists
    	       let fileContent = fm.readString(selectedNote)
    	       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(selectedNote,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("configured and selected file does note exist")
    	       alert("the selected file \"" + selectedNote.substring(1) + "\" doesn't exist in the configured bookmark.\nmake sure to include the full subpath of the file in the configuration note")
    	       context.fail()
    	       }
    
           } else {
                // no block selected by the user
                console.log("user did not select an Obsidian note")
                context.cancel()
            }
        }
    }
    
    
    

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.