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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@faxd That's a good point. Yes the script needs to be executable.