Last active
October 12, 2022 19:58
-
-
Save voostindie/dedbeafb3287479d452c9fc21dbba5d8 to your computer and use it in GitHub Desktop.
Hook: alternative "Get Address" script for Obsidian that returns file links
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
#!/Users/vincent/.rbenv/shims/ruby | |
# | |
# Generate an Obisidian link to the currently opened document, for | |
# use in Hook. | |
# | |
# As of version 3.4.3, Hook has 3 built-in methods for integrating | |
# with Obsidian. None of them suit my personal needs. | |
# | |
# - default: uses the obsidian://hook-get-address URL, which makes | |
# Obsidian copy its URL to the clipboard. | |
# - Advanced URI: requires the use of the Advanced URI plugin in | |
# Obsidian, with a configuration that sets unique IDs in the | |
# front matter. | |
# - hook://file/ URLs scheme: also requires the Advanced URI plugin | |
# in Obsidian. | |
# | |
# This script is an alternative implementation of the third solution, | |
# without requiring an additional plugin for Obsidian. It does require | |
# a working version of Ruby though. | |
# | |
# In Hook, set the "Get Address" script to the following: | |
# | |
# tell application "System Events" to set names to (name of every window of process "Obsidian") | |
# do shell script "export LC_ALL=en_US.UTF-8; export LANG=en_US.UTF-8; /Users/vincent/.rbenv/shims/ruby /Users/vincent/Code/mac-automation/get-obsidian-address.rb " & quoted form of (item 1 of names)# | |
# | |
# What this does is, when you invoke Hook, get the list of Obsidian | |
# windows, select the first, and then invoke this Ruby script with that | |
# window's name as an argument. | |
# | |
# (In my testing, with multiple Obsidian windows open, the front | |
# window is always the first one in the list.) | |
# | |
# This Ruby script locates the selected vault on disk, reads its | |
# workspace configuration file, and selects the top item of the | |
# lastOpenFiles list. | |
# | |
# Finally it writes the path to this file. This is just a normal file | |
# reference, which means that hooks continue to work, even if you move | |
# files around. | |
# | |
# But now you have a problem: how to open these files in Obsidian? For | |
# that I'd like to refer to this nifty little tool called OpenIn that | |
# you can find at https://loshadki.app/openin/ and in the App Store. | |
# You can set this up with some rules to have it open `.md` files in | |
# Obsidian, but only some. To make Obsidian work in OpenIn, I set up | |
# a couple of rules: | |
# | |
# - Path matches: any of my vaults | |
# - Use applications: Obsidian | |
# - Custom script: the one-liner `ctx.url.protocol = 'obsidian'` | |
# | |
# And that does it! | |
Encoding.default_external = Encoding::UTF_8 | |
Encoding.default_internal = Encoding::UTF_8 | |
require 'json' | |
require 'erb' | |
# The window title is <vault> - Obsidian <version>", so | |
vault = ARGV[0].split(' - ')[0] | |
root = JSON.load_file(File.join(Dir.home, 'Library', 'Application Support', 'obsidian', 'obsidian.json'))['vaults'].values | |
.map { |v| v['path'] } | |
.select { |p| File.basename(p) == vault } | |
workspace = JSON.load_file(File.join(root, '.obsidian', 'workspace')) | |
document = workspace['lastOpenFiles'].first | |
print File.join(root, document) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm not using this script anymore. Instead I use the default Hook scripts for Obsidian, with the preference to return file links. That now works without using the clipboard (thanks to URL callback support in Obsidian),