Skip to main content

Overview

The App class is the central interface for interacting with Obsidian. It provides access to all major subsystems including the workspace, vault, metadata cache, and more. Every plugin receives an instance of App which serves as the entry point for most plugin functionality. Available since: v0.9.7

Properties

keymap
Keymap
The keymap manager for handling keyboard shortcuts.Since: v0.9.7
scope
Scope
The current scope for managing keybindings and event handlers.Since: v0.9.7
workspace
Workspace
The workspace manager providing access to leaves, panes, and the active view.Since: v0.9.7
vault
Vault
The vault interface for reading, writing, and managing files.Since: v0.9.7
metadataCache
MetadataCache
The metadata cache containing parsed file information, links, and frontmatter.Since: v0.9.7
fileManager
FileManager
The file manager for higher-level file operations like renaming and moving files.Since: v0.11.0
lastEvent
UserEvent | null
The last known user interaction event, useful for determining which modifier keys are currently pressed.Since: v0.12.17
renderContext
RenderContext
The render context for managing rendering operations.Since: v1.10.0
secretStorage
SecretStorage
Secure storage for sensitive data like API keys and tokens.Since: v1.11.4

Methods

isDarkMode()

Determines whether the app is currently in dark mode. Returns: boolean - true if dark mode is enabled, false otherwise. Since: v1.10.0
if (this.app.isDarkMode()) {
  // Apply dark mode specific styling
  console.log('Dark mode is active');
}

loadLocalStorage()

Retrieves a value from localStorage specific to the current vault.
key
string
required
The key to retrieve from localStorage.
Returns: any | null - The stored value, or null if not found. Since: v1.8.7
// Load plugin-specific settings from localStorage
const cachedData = this.app.loadLocalStorage('my-plugin-cache');
if (cachedData) {
  console.log('Loaded cached data:', cachedData);
}

saveLocalStorage()

Saves a vault-specific value to localStorage. If data is null, the entry will be cleared.
key
string
required
The key under which to store the data.
data
unknown | null
required
The value to store. Must be serializable. Pass null to clear the entry.
Returns: void Since: v1.8.7
// Save temporary cache data
this.app.saveLocalStorage('my-plugin-cache', {
  lastSync: Date.now(),
  items: ['item1', 'item2']
});

// Clear the cache
this.app.saveLocalStorage('my-plugin-cache', null);

Example

import { Plugin } from 'obsidian';

export default class MyPlugin extends Plugin {
  async onload() {
    // Access the active file through the workspace
    const activeFile = this.app.workspace.getActiveFile();
    
    if (activeFile) {
      // Read file contents using the vault
      const content = await this.app.vault.read(activeFile);
      
      // Get metadata from the cache
      const metadata = this.app.metadataCache.getFileCache(activeFile);
      
      console.log(`File: ${activeFile.path}`);
      console.log(`Content length: ${content.length}`);
      console.log(`Has frontmatter: ${!!metadata?.frontmatter}`);
    }
    
    // Check theme mode
    if (this.app.isDarkMode()) {
      console.log('Running in dark mode');
    }
    
    // Use localStorage for caching
    const lastRun = this.app.loadLocalStorage('last-run-time');
    this.app.saveLocalStorage('last-run-time', Date.now());
  }
}
  • UserEvent - Type alias for MouseEvent | KeyboardEvent | TouchEvent | PointerEvent
  • Workspace - Interface for managing workspace layout and views
  • Vault - Interface for file system operations
  • MetadataCache - Interface for accessing parsed file metadata
  • FileManager - Interface for high-level file operations

Build docs developers (and LLMs) love