Adds command to ribbon and command palette to open a given file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%* | |
// | |
// updated: 2022-09-26 | |
// | |
// Script adds a Command Palette command to open a file | |
// | |
// Install: | |
// 1. Add this text to a markdown file in your Templater "templates" folder | |
// 2. In templater settings, add the file you created in step 1 to the Startup section of Templater Settings | |
// 3. Customize the variables homepagePath and comanndTitle to suit your own needs | |
// 4. Restart Obisidan. Now the command should be available in the command palette | |
// | |
// Customizing: | |
// pagePath Full path to the Markdown file to open | |
// commandTitle Text that is displayed in the Command Palette | |
// commandID Unique ID for this command. All command palette commands in Obsidian need to have a unique ID | |
// iconID Icon id name from list of valid IDs. List available at: | |
// https://github.com/kometenstaub/customizable-page-header-buttons/blob/main/src/constants.ts | |
// https://github.com/phibr0/obsidian-commander/blob/cac8b52ffbc0ff3bb8a686d16767f7e98568f384/src/constants.ts | |
// | |
(async () => { | |
const pagePath = "Dashboard/00-Home Page.md"; | |
const commandTitle = "Open TFT Hacker Dashboard"; | |
const commandID = "open-home-page"; | |
const iconID = "home"; | |
openFile = (filePath)=> { | |
const file = app.vault.getAbstractFileByPath(filePath); | |
const activeLeaf = app.workspace.getActiveViewOfType(tp.obsidian.MarkdownView)?.leaf; | |
if(app.workspace.getActiveFile()==null || activeLeaf===undefined) | |
app.workspace.getLeaf().openFile(file); | |
else | |
app.workspace.splitActiveLeaf().openFile(file); | |
} | |
app.commands.addCommand({ | |
id: commandID, | |
icon: iconID, | |
name: commandTitle, | |
mobileOnly: false, | |
callback: async ()=> openFile(pagePath) | |
}) | |
app.workspace.leftRibbon.addRibbonActionButton(iconID, commandTitle, async ()=> openFile(pagePath)); | |
// app.workspace.leftRibbon.addRibbonActionButton(iconID, commandTitle, async ()=> console.log(app.workspace.getActiveViewOfType(tp.obsidian.MarkdownView).leaf)); | |
})(); | |
%> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment