Note Toolbar button to easily swap toolbars (courtesy of a Templater script)

The new Note Toolbar plugin is an awesome addition to the Obsidian toolkit.

Currently, you can associate toolbars with notes using folder paths or properties. However, I wanted the flexibility to swap the toolbars in any given note for different workflows.

Therefore, I made the following Templater script for toolbar swapping at the click of a button, depending on what sort of workflow you are focusing on at the moment.

Example:
You have a global toolbar mapped to the vault root (*), so that it is present in all notes. But let’s say you want to override it for a moment, and use a research toolbar instead with related utilities up front. This script lets you swap out the toolbar at the click of a button:

Obsidian_YR1LzpyMAq

Setup

Prerequisites:

  • Templater plugin

Steps:

  1. Save the template below above into a template note in your template folder, as configured in Templater’s settings, and give it a descriptive name like swapToolbar

  2. Bind the template to a command in Templater’s settings:

  3. Add a “Swap toolbar” button to your toolbars, using the command type and then choose the command configured in step 2 as the command

VoilĂ ! Now you can easily swap out toolbars for different workflows at different times.

How it works

The script utilizes Note toolbar’s aforementioned ability to specify toolbars using properties.

It works by retrieving the names of all your configured toolbars directly from the Note toolbar settings, and presenting them in a suggester.

After you pick a toolbar in the suggester, a toolbarProp property (notetoolbar by default) is either added or updated in the current note to reflect your choice. This swaps the current toolbar to your chosen toolbar.

The user configured toolbarProp is also taken from the plugin’s settings, so you don’t have to configure anything. It just works!

To revert to the default toolbar, use the “Default folder mapping” in the suggester. This deletes the toolbarProp.

The template

<%*
// Check if anything is selected. If it is, return it, so it's not lost.
const selection = tp.file.selection();
if (selection.length > 0) {
	tR += selection;
}
tp.hooks.on_all_templates_executed(async () => {
	
	// Get all the configured toolbars and the toolbar property from the plugin settings
	const { toolbars, toolbarProp } = app.plugins.getPlugin("note-toolbar").settings;
	
	// Option to revert to default folder mapping
	const defaultOption = "Default folder mapping";
	
	// Extract only the toolbar names and add the default option
	const toolbarOptions = toolbars.map(toolbar => toolbar.name).concat(defaultOption);
	
	// Prompt the user to pick a toolbar
	const pickedToolbar = await tp.system.suggester((item) => item, toolbarOptions, true, "Swap to toolbar:");
	
	// Get the current file
	const currentFile = app.workspace.getActiveFile();

	// Update the frontmatter of the current file with the selected toolbar option
	await app.fileManager.processFrontMatter(currentFile, (frontmatter) => {
		
		// If the user picked the default option, delete the toolbar property
		if (pickedToolbar === defaultOption) {
			delete frontmatter[toolbarProp];
			return;
		}
		
		// Otherwise, set the toolbar property to the chosen toolbar
		frontmatter[toolbarProp] = pickedToolbar;
	});
});
_%>

8 Likes