Note Toolbar API - chrisgurney/obsidian-note-toolbar GitHub Wiki

Note

This documentation is for version 1.23.

The Note Toolbar API provides basic toolbar access, and the ability to show UI (e.g., suggesters, prompts, and modals). The latter enables Dataview JS, JS Engine, or Templater scripts to ask for information, or to show helpful text.

Using the ntb object, below are the functions that can be called in scripts that are executed from Note Toolbar items.

I would appreciate your feedback, which you can leave in the discussions.

Warning

While you could also directly access Note Toolbar's settings or toolbar items via app.plugins.getPlugin("note-toolbar").settings, be aware that these are subject to change and may break your scripts. The API will be the official way to access and change information about toolbars.

Copy developer ID for items

In each item's More actions... menu, use Copy developer ID to copy the unique identifier (UUID) for any toolbar item to the clipboard.

From code you can then target the item and make changes to it:

const item = ntb.getItem('112c7ed3-d5c2-4750-b95d-75bc84e23513');
item.setIcon('alert');

// or fetch the HTML element (for non-floating-button toolbars)
const itemEl = activeDocument.getElementById('112c7ed3-d5c2-4750-b95d-75bc84e23513');

ntb API

clipboard()

clipboard: () => Promise<null | string>

Gets the clipboard value.

Returns

Promise<null | string>

The clipboard value or null.

Example

// gets the clipboard value
const value = await ntb.clipboard();

new Notice(value);

fileSuggester()

fileSuggester: (options?) => Promise<null | TAbstractFile>

Shows a file suggester modal and waits for the user's selection.

Parameters

Parameter Type Description
options? { class: string; filesonly: boolean; foldersonly: boolean; limit: number; placeholder: string; rendermd: boolean; } Optional display options.
options.class? string Optional CSS class(es) to add to the component.
options.filesonly? boolean If set to true, only files are shown. If not provided, defaults to false.
options.foldersonly? boolean If set to true, only folders are shown. If not provided, defaults to false.
options.limit? number Optional limit of the number of items rendered at once (useful to improve performance when displaying large lists).
options.placeholder? string Optional text inside text field; defaults to preset message.
options.rendermd? boolean Set to false to disable rendering of suggestions as markdown. Default is true.

Returns

Promise<null | TAbstractFile>

The selected TAbstractFile.

Example

const fileOrFolder = await ntb.fileSuggester();
new Notice(fileOrFolder.name);
// show only folders
const folder = await ntb.fileSuggester({
 foldersonly: true
});
new Notice(folder.name);

getActiveItem()

getActiveItem: () => undefined | IItem

Gets the active (last activated) toolbar item.

Returns

undefined | IItem

The active (last activated) item.

Remarks

This does not work with Note Toolbar Callouts.


getItem()

getItem: (id) => undefined | IItem

Gets an item by its ID, if it exists.

Parameters

Parameter Type Description
id string The ID of the item.

Returns

undefined | IItem

The item, or undefined.


getProperty()

getProperty: (property) => undefined | string

Gets the value of the given property in the active note.

Parameters

Parameter Type Description
property string The property to get the frontmatter for.

Returns

undefined | string

The frontmatter value for the given property, or undefined if it does not exist.

Example

const createdDate = ntb.getProperty('created');

getToolbars()

getToolbars: () => IToolbar[]

Gets all toolbars.

Returns

IToolbar[]

All toolbars.


modal()

modal: (content, options?) => Promise<void>

Shows a modal with the provided content.

Parameters

Parameter Type Description
content string | TFile Content to display in the modal, either as a string or a file within the vault.
options? { class: string; title: string; webpage: boolean; } Optional display options.
options.class? string Optional CSS class(es) to add to the component.
options.title? string Optional title for the modal, rendered as markdown.
options.webpage? boolean If true, the modal will show the web page URL in content using the Web Viewer core plugin (if enabled); defaults to false.

Returns

Promise<void>

Examples

// shows a modal with the provided string
await ntb.modal("_Hello_ world!");
// shows a modal with the rendered contents of a file
const filename = "Welcome.md";
const file = app.vault.getAbstractFileByPath(filename);

if (file) {
  await ntb.modal(file, {
    title: `**${file.basename}**`
  });
}
else {
  new Notice(`File not found: ${filename}`);
}

See

NtbModal.js in the examples/Scripts folder.


prompt()

prompt: (options?) => Promise<null | string>

Shows the prompt modal and waits for the user's input.

Parameters

Parameter Type Description
options? { class: string; default: string; label: string; large: boolean; placeholder: string; } Optional display options.
options.class? string Optional CSS class(es) to add to the component.
options.default? string Optional default value for text field. If not provided, no default value is set.
options.label? string Optional text shown above the text field, rendered as markdown. Default is no label.
options.large? boolean If set to true, the input field will be multi line. If not provided, defaults to false.
options.placeholder? string Optional text inside text field. Defaults to a preset message.

Returns

Promise<null | string>

The user's input.

Examples

// default (one-line) prompt with default placeholder message
const result = await ntb.prompt();

new Notice(result);
// large prompt with message, overridden placeholder, and default value 
const result = await ntb.prompt({
  label: "Enter some text",
  large: true,
  placeholder: "Placeholder",
  default: "Default"
});

new Notice(result);

See

NtbPrompt.js in the examples/Scripts folder.


setProperty()

setProperty: (property, value) => Promise<void>

Sets the given property's value in the active note.

Parameters

Parameter Type Description
property string Propety to set in the frontmatter.
value any Value to set for the property.

Returns

Promise<void>

Example

await ntb.setProperty('Created', moment().format('YYYY-MM-DD'));
await ntb.setProperty('cssclasses', 'myclass');
await ntb.setProperty('A Link', '[[Some Note]]');
await ntb.setProperty('A Number', 1234);
await ntb.setProperty('A List', ['asdf', 'asdf2']);

suggester()

suggester: (values, keys?, options?) => Promise<null | T>

Shows a suggester modal and waits for the user's selection.

Parameters

Parameter Type Description
values string[] | (value) => string Array of strings representing the text that will be displayed for each item in the suggester prompt. This can also be a function that maps an item to its text representation. Rendered as markdown: optionally mix in Obsidian and plugin markdown (e.g., Iconize) to have it rendered
keys? T[] Optional array containing the keys of each item in the correct order. If not provided, values are returned on selection.
options? { class: string; limit: number; placeholder: string; rendermd: boolean; } Optional display options.
options.class? string Optional CSS class(es) to add to the component.
options.limit? number Optional limit of the number of items rendered at once (useful to improve performance when displaying large lists).
options.placeholder? string Optional text inside text field; defaults to preset message.
options.rendermd? boolean Set to false to disable rendering of suggestions as markdown. Default is true.

Returns

Promise<null | T>

The selected value, or corresponding key if keys are provided.

Examples

// shows a suggester that returns the selected value 
const values = ["value `1`", "value `2`"];

const selectedValue = await ntb.suggester(values);

new Notice(selectedValue);
// shows a suggester that returns a key corresponding to the selected value, and overrides placeholder text
const values = ["value `1`", "value `2`"];
const keys = ["key1", "key2"];

const selectedKey = await ntb.suggester(values, keys, {
  placeholder: "Pick something"
});

new Notice(selectedKey);

See

NtbSuggester.js in the examples/Scripts folder.


t

t: string

This is the i18next translation function, scoped to Note Toolbar's localized strings.

Returns

The string translation corresponding with the provided key, if it exists, with a fallback to English. If the key does not exist, the key is returned.

Example

// shows "Copied to clipboard" if the language is English, or in another langauge if the translation exists
new Notice(ntb.t('api.msg.clipboard-copied'));

See

⚠️ **GitHub.com Fallback** ⚠️