Skip to main content

Add my obnote script

ID
772618f
date
2023-12-25 23:48:59+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
7d06ee0
message
Add my `obnote` script
changed files
2 files, 64 additions, 1 deletion

Changed files

macos/README.md (4757) → macos/README.md (5128)

diff --git a/macos/README.md b/macos/README.md
index d01cbf4..2f20392 100644
--- a/macos/README.md
+++ b/macos/README.md
@@ -45,6 +45,10 @@ scripts = [
         "description": "get OCR'd text for a single image using Live Text"
     },
     {
+        "name": "obnote",
+        "description": "print the path to the note I currently have open in [Obsidian](https://obsidian.md/), if any."
+    },
+    {
         "usage": "set_accent_colour (red|orange|yellow|green|blue|purple|pink|graphite)",
         "description": "set the accent colour, as configured in the Appearance settings",
     },
@@ -124,6 +128,14 @@ prints the title of the frontmost window in the named app. This is particularly 
 get OCR'd text for a single image using Live Text
 </dd>
 <dt>
+<a href="https://github.com/alexwlchan/scripts/blob/main/macos/obnote">
+<code>obnote</code>
+</a>
+</dt>
+<dd>
+print the path to the note I currently have open in [Obsidian](https://obsidian.md/), if any.
+</dd>
+<dt>
 <a href="https://github.com/alexwlchan/scripts/blob/main/macos/set_accent_colour">
 <code>set_accent_colour (red|orange|yellow|green|blue|purple|pink|graphite)</code>
 </a>
@@ -151,4 +163,4 @@ alias for <code>security unlock-keychain ~/Library/Keychains/login.keychain</cod
 </p>
 </dd>
 </dl>
-<!-- [[[end]]] (checksum: ee1950858a05fd7ea7bd4e60e619d61f) -->
+<!-- [[[end]]] (checksum: f7e1feec3b74bd4146f0910828465cf3) -->

macos/obnote (0) → macos/obnote (1539)

diff --git a/macos/obnote b/macos/obnote
new file mode 100755
index 0000000..8681361
--- /dev/null
+++ b/macos/obnote
@@ -0,0 +1,51 @@
+#!/usr/bin/env python3
+"""
+Prints the path to the note I currently have open in Obsidian (if any).
+
+This relies on knowing the on-disk locations of my Obsidian vaults,
+so you won't be able to use this without changing it for your own setup.
+"""
+
+import os
+import subprocess
+
+
+def get_file_paths_under(root=".", *, suffix=""):
+    """
+    Generates the absolute paths to every matching file under ``root``.
+    """
+    if not os.path.isdir(root):
+        raise ValueError(f"Cannot find files under non-existent directory: {root!r}")
+
+    for dirpath, _, filenames in os.walk(root):
+        for f in filenames:
+            p = os.path.join(dirpath, f)
+
+            if os.path.isfile(p) and f.lower().endswith(suffix):
+                yield p
+
+
+if __name__ == "__main__":
+    window_title = (
+        subprocess.check_output(["get_frontmost_window_title", "Obsidian"])
+        .strip()
+        .decode("utf8")
+    )
+
+    # The window title will be something of the form:
+    #
+    #     Short story ideas - textfiles - Obsidian v1.4.16
+    #
+    note_title, vault_name, _ = window_title.rsplit(" - ", 2)
+
+    if vault_name == "textfiles":
+        vault_root = os.path.join(os.environ["HOME"], "textfiles")
+    else:
+        raise ValueError(f"Unrecognised vault name: {vault_name}")
+
+    for path in get_file_paths_under(vault_root, suffix=".md"):
+        if path.endswith(f"/{note_title}.md"):
+            print(path, end="")
+            break
+    else:  # no break
+        raise RuntimeError(f"Could not find note with title {note_title}")