Skip to content

Additional Gallery items

Chris Gurney edited this page Jun 4, 2026 · 58 revisions

Clipboard

Copy to clipboard

/**
 * Copies the selected text to the clipboard.
 */

const e = ntb.app.workspace.activeLeaf.view?.editor;
try { await activeWindow.navigator.clipboard.writeText(e.getSelection()); e?.focus(); } catch (e) { console.error(e); new Notice(ntb.t('api.msg.error-console')); }

Cut to clipboard

/**
 * Cuts the selected text to the clipboard.
 */

const e = ntb.app.workspace.activeLeaf.view?.editor;
try { await activeWindow.navigator.clipboard.writeText(e.getSelection()); e.replaceSelection(''); e?.focus(); } catch (e) { console.error(e); new Notice(ntb.t('api.msg.error-console')); }

Paste from clipboard

/**
 * Pastes the text from the clipboard.
 */

const e = ntb.app.workspace.activeLeaf.view?.editor;
try { let rs = e.replaceSelection; let text = await activeWindow.navigator.clipboard.readText(); if (text) rs.apply(e, [text]); e?.focus(); } catch (e) { console.error(e); new Notice(ntb.t('api.msg.error-console')); }

Copy block link

/**
 * Copies a link to the current block to the clipboard.
 */

const { metadataCache: m } = app, e = ntb.app.workspace.activeEditor?.editor, f = ntb.app.workspace.getActiveFile(), l = e?.getCursor().line, b = e?.getLine(l).match(/\\^([\\w-]+)/)?.[1] || m.getFileCache(f)?.blocks?.[l]?.id;
b ? (window.navigator.clipboard.writeText(`[[${f.path}#^${b}]]`), new Notice(ntb.t('api.msg.clipboard-copied'))) : new Notice(ntb.t('api.msg.block-no-id'));

Copy note content

/**
 * Copy body of the note to the clipboard, ignoring frontmatter.
 */

const e = ntb.app.workspace.activeLeaf.view?.editor;
if (!e) return;
const c = e.getValue();
const b = c.startsWith('---\\n') ? c.replace(/^---\\n[\\s\\S]*?\\n---\\n*/, '') : c;
await activeWindow.navigator.clipboard.writeText(b);
new Notice(ntb.t('api.msg.clipboard-copied'));

Append to a file

Append text to a file

/**
 * Appends the prompted text to a file you specify.
 */

const fn = 'File.md'; // CHANGE THIS
const f = ntb.app.vault.getAbstractFileByPath(fn);
if (!f) { new Notice(ntb.t('api.msg.file-not-exist')); return; };
const txt = await ntb.prompt({ large: true });
if (txt) { const c = await ntb.app.vault.read(f); await ntb.app.vault.modify(f, c + '\\n\\n' + txt); }

Backup

Backup plugin data

/**
 * Copies Note Toolbar's settings (data.json) file into the vault.
 */

const src = `${ntb.app.vault.configDir}/plugins/note-toolbar/data.json`;
const date = new Date().toISOString().slice(0, 10).replace(/-/g, '');
const dest = `note-toolbar-${date}-data.json`;
ntb.app.vault.adapter.read(src).then(data => { 
  ntb.app.vault.adapter.write(dest, data); 
  new Notice(ntb.t('api.msg.file-created', {filename: dest})); 
});

Export all toolbars to callouts

/**
 * Writes an export file containing all toolbars as callouts.
 */

const toolbars = ntb.getToolbars();
let callouts = '';
for (let toolbar of toolbars) {
    callouts += `## ${toolbar.getName()}\n\n`;
    callouts += await ntb.export(toolbar);
    callouts += '\n';
}
const datestamp = new Date().toISOString().replace(/[:.]/g, '').replace('T', '-').split('Z')[0];
const filename = `TOOLBARS ${datestamp}.md`;
await ntb.app.vault.create(filename, callouts);
new Notice(`Toolbars exported to:\n${filename}`);

Other

File list

Shows a list of files in the specified folder, in the current note.

🧩 Requires the Dataview ↗ plugin.

/**
 * Shows a list of files in the specified folder (and subfolders).
 * Excludes the current file. Defaults to current folder if folder not provided. Sorts alphabetically.
 * 
 * Arguments = fileFolder: "Scripts"
 * Requires a Note Toolbar Output callout ID to render.
 */

function FileList(input) {
    let fileFolder;

    if (input) {
        ({fileFolder} = input);
    }
    /* if fileFolder is not provided... */
    else {
        /* ...default to the folder of the calling note */
        fileFolder = dv.current().file.folder;
    }
    fileFolder = fileFolder ?? "";

    const files = app.vault.getFiles()
        .filter(file => (fileFolder === ""
            ? !file.path.includes("/")
            : file.path.startsWith(
                fileFolder.endsWith("/") ? fileFolder : fileFolder + "/"))
            && file.path !== dv.current().file.path)
        .sort((a, b) => a.name.localeCompare(b.name));

    dv.paragraph("📂 `" + fileFolder + "`:");
    dv.list(files.map(file => dv.fileLink(file.path)));
}

FileList(input);

Clone this wiki locally