Last active
May 16, 2023 14:16
-
-
Save luckman212/1d9275cf77c3b3fa935acbda7e95a548 to your computer and use it in GitHub Desktop.
URL-encode Obsidian links - meant to be used with Service Station, Alfred etc.
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
#!/usr/bin/env bash | |
_realpath() { | |
if [[ -x $(command -v realpath) ]]; then | |
realpath "$1" | |
else | |
$(brew --prefix)/bin/python3 -c 'import sys,os; print(os.path.realpath(sys.argv[1]));' "$1" | |
fi | |
} | |
_urlenc() { | |
local STRING=$* | |
[ -n "$STRING" ] || return 1 | |
$(brew --prefix)/bin/python3 -c 'import sys; from urllib.parse import quote,unquote; print(quote(unquote(sys.argv[1])));' "$STRING" | |
} | |
_canonicalize() { | |
unset canonical | |
if [[ $1 =~ ^(/System/Volumes/Data)(/.*)$ ]]; then | |
canonical="${BASH_REMATCH[2]}" | |
fi | |
if [[ -n $canonical ]]; then | |
inode1=$(stat -f'%i' "$1") | |
inode2=$(stat -f'%i' "$canonical") | |
if [[ $inode1 -eq $inode2 ]]; then | |
fullpath="$canonical" | |
fi | |
fi | |
} | |
_adbk() { | |
abcdp=${filename%.*} | |
safe=$(_urlenc "$abcdp") | |
name=$(mdls -raw -name kMDItemTitle "$fullpath") | |
echo "[$name](addressbook://$safe)" | |
} | |
_file() { | |
safe=$(_urlenc "${fullpath}") | |
case $ext in | |
gif|jpg|jpeg|png) | |
name="${filename%.*}" | |
echo "![$name](app://local/$safe)" | |
;; | |
esac | |
echo "[$filename](file://$safe)" | |
} | |
case $1 in | |
-h|--help) echo "usage: ${0##*/} <file>"; exit;; | |
esac | |
[[ -e $1 ]] || exit | |
fullpath=$(_realpath "$1") | |
_canonicalize "$fullpath" | |
filename="${fullpath##*/}" | |
ext=$(tr '[:upper:]' '[:lower:]' <<<"${filename##*.}") | |
case $ext in | |
abcdp) _adbk; exit;; | |
*) _file; exit;; | |
esac |
The above script is not a fully baked solution @floppybox
I just posted a version that should fully work for you, as long as you have python3 installed via Homebrew.
see https://gist.github.com/luckman212/d2570b072fd1698492b0e17183990004
Let me know!
@floppybox I needed to "chmod +x" the script before it worked.
@faxd That's a good point. Yes the script needs to be executable.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello Luckman212,
I would love to use your script with Obsidian and Service Station, which I bought for this purpose because it would make things so much easier with external files.
I have copied the script to the Application Scripts Directory of Service Station and it shows up in the context menu of my files.
But when click on the item in the context menu of any file, nothing happens.
Anything I could have missed?
Regards
Florian