Skip to content

Instantly share code, notes, and snippets.

@schuhwerk
Last active December 28, 2024 17:53
Show Gist options
  • Save schuhwerk/36e3cb630d949aa275d0f240b9d82d95 to your computer and use it in GitHub Desktop.
Save schuhwerk/36e3cb630d949aa275d0f240b9d82d95 to your computer and use it in GitHub Desktop.
Open with Obsidian. Choose Proper Markdown-Editor based on file-location (Windows)
:: Universal markdown (.md) file opener with Obsidian vault support
::
:: This script handles two scenarios:
:: 1. Files in Obsidian vault: Opens using obsidian://open?path protocol
:: 2. Files outside vault: Opens using alternative editor (e.g. Typora)
::
:: Setup:
:: 1. Set VAULT_PATH to your Obsidian vault location
:: 2. Set ALT_EDITOR to your preferred markdown editor
:: 3. Register as default .md handler:
:: Right-click .md file -> Open with -> Choose another app -> (Scroll down) ->
:: -> Choose an App on your PC -> Select this script -> Confirm with "Always" (use this app)
::
:: URL: https://gist.github.com/schuhwerk/36e3cb630d949aa275d0f240b9d82d95
:: Version: 1.0
:: Author: https://github.com/schuhwerk
:: Set UTF-8 encoding (code page 65001). Properly handle umlauts, accents and other Unicode characters.
chcp 65001 >nul
:: Delayed Expansion will cause variables within a batch file to be expanded at execution time rather than at parse time
setlocal enableDelayedExpansion
:: the path with your obsidian notes. Change this to your own path.
set "VAULT_PATH=Nextcloud\Notes"
:: the path to your alternative editor. Change this to your own path (e.g. Typora, added to environment variables).
set "ALT_EDITOR=Typora.exe"
:: set to 1 to enable debug output.
set "DEBUG=0"
if "%DEBUG%"=="1" Echo.Input path: %*
:: Check if the input path contains the vault path using findstr:
:: - ECHO.%URLPATH% outputs the path
:: - | pipes the output to findstr
:: - /C: treats the search string as a literal string (not a regex pattern)
:: Example: /C:abc searches for exactly "abc", while without /C:
:: special characters like . [ ] * would be treated as regex
:: - >Nul suppresses the findstr output
:: - && executes the first block if found (Obsidian)
:: - || executes the second block if not found (ALT_EDITOR)
ECHO.%*| findstr /C:%VAULT_PATH%>Nul && (
:: Encode spaces for Obsidian vault paths.
set "OBSIDIAN_PATH=%*"
:: https://stackoverflow.com/questions/10218451/how-can-i-url-encode-spaces-in-an-nt-batch-file
set "OBSIDIAN_PATH=!OBSIDIAN_PATH: =%%20!"
if "%DEBUG%"=="1" Echo.Found %VAULT_PATH% - Opening in Obsidian
if "%DEBUG%"=="1" Echo.Command: start explorer "obsidian://open?path=!OBSIDIAN_PATH!"
start explorer "obsidian://open?path=!OBSIDIAN_PATH!"
) || (
if "%DEBUG%"=="1" Echo.Opening in Typora
if "%DEBUG%"=="1" Echo.Editor path: %ALT_EDITOR%
if "%DEBUG%"=="1" Echo.File path: %*
if "%DEBUG%"=="1" Echo.Command: start "" "%ALT_EDITOR%" %*
start "" "%ALT_EDITOR%" %*
)
if "%DEBUG%"=="1" pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment