Convert obnote to be a pure shell function
- ID
7d88780- date
2023-12-28 09:39:29+00:00- author
Alex Chan <alex@alexwlchan.net>- parent
cf601de- message
Convert `obnote` to be a pure shell function- changed files
4 files, 49 additions, 97 deletions
Changed files
.gitattributes (220) → .gitattributes (164)
diff --git a/.gitattributes b/.gitattributes
index 1f07a11..80ccdb3 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -1,5 +1,4 @@
close_tabs linguist-language=JavaScript
get_focus_mode linguist-language=JavaScript
-get_frontmost_window_title linguist-language=JavaScript
get_live_text linguist-language=Swift
set_accent_colour linguist-language=Swift
macos/README.md (5428) → macos/README.md (4851)
diff --git a/macos/README.md b/macos/README.md
index d70ab52..dd39276 100644
--- a/macos/README.md
+++ b/macos/README.md
@@ -37,10 +37,6 @@ scripts = [
"description": "prints the current Focus mode"
},
{
- "usage": "get_frontmost_window_title [APP_NAME]",
- "description": "prints the title of the frontmost window in the named app. This is particularly useful when working with apps that don't support AppleScript."
- },
- {
"name": "get_live_text [image]",
"description": "get OCR'd text for a single image using Live Text"
},
@@ -117,15 +113,6 @@ cog_helpers.create_description_table(folder_name=folder_name, scripts=scripts)
</dd>
<dt>
- <a href="https://github.com/alexwlchan/scripts/blob/main/macos/get_frontmost_window_title">
- <code>get_frontmost_window_title [APP_NAME]</code>
- </a>
- </dt>
- <dd>
- prints the title of the frontmost window in the named app. This is particularly useful when working with apps that don't support AppleScript.
- </dd>
-
- <dt>
<a href="https://github.com/alexwlchan/scripts/blob/main/macos/get_live_text">
<code>get_live_text [image]</code>
</a>
@@ -173,4 +160,4 @@ cog_helpers.create_description_table(folder_name=folder_name, scripts=scripts)
</p>
</dd>
</dl>
-<!-- [[[end]]] (checksum: 92219c4a3477e066af5311a2e97e1db6) -->
+<!-- [[[end]]] (checksum: b76affe4f83859dc60b4ef333666ce0f) -->
macos/get_frontmost_window_title (859) → macos/get_frontmost_window_title (0)
diff --git a/macos/get_frontmost_window_title b/macos/get_frontmost_window_title
deleted file mode 100755
index 14b21e2..0000000
--- a/macos/get_frontmost_window_title
+++ /dev/null
@@ -1,31 +0,0 @@
-#!/usr/bin/env osascript -l JavaScript
-// Given the name of an app, print the title of the frontmost window
-// in that app.
-//
-// Examples:
-//
-// $ get_frontmost_window_title "Obsidian"
-// Short story ideas - textfiles - Obsidian v1.4.16
-//
-// $ get_frontmost_window_title "TextMate"
-// get_frontmost_window_title — macos (git: main)
-//
-// This is particularly useful for writing automations for apps that
-// don't support AppleScript.
-
-function run(argv) {
- if (argv.length !== 1) {
- throw new Error("Usage: get_frontmost_window_title <APP_NAME>")
- }
-
- const appName = argv[0];
-
- const systemEvents = Application("System Events");
- const process = systemEvents.processes[appName];
-
- if (process.windows.length > 0) {
- return process.windows[0].name();
- } else {
- throw new Error(`No windows open in ${appName}!`);
- }
-}
macos/obnote (1539) → macos/obnote (1097)
diff --git a/macos/obnote b/macos/obnote
index 8681361..35cad47 100755
--- a/macos/obnote
+++ b/macos/obnote
@@ -1,51 +1,48 @@
-#!/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}")
+#!/usr/bin/env bash
+# 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.
+# Add the path to your vaults in the `case` statement below.
+
+set -o errexit
+set -o nounset
+
+window_title=$(osascript -e '
+ tell application "System Events"
+ tell process "Obsidian" to get title of front window
+ end tell
+')
+
+# The window title will be a three part string of the form
+#
+# Note title - Vault name - Obsidian vX.Y.Z
+#
+# For example:
+#
+# Short story ideas - textfiles - Obsidian v1.4.16
+#
+note_title=$(echo "$window_title" \
+ | rev \
+ | cut -d '-' -f 3- \
+ | rev \
+ | sed 's/[[:space:]]*$//')
+
+vault_name=$(echo "$window_title" \
+ | rev \
+ | cut -d'-' -f 2 \
+ | rev \
+ | sed 's/^[[:space:]]*//' \
+ | sed 's/[[:space:]]*$//')
+
+case "$vault_name" in
+ "textfiles")
+ vault_root=~/textfiles
+ ;;
+ *)
+ echo "Unknown vault name" >&2
+ exit 1
+ ;;
+esac
+
+find "$vault_root" -name "$note_title*" -print | head -n 1