Skip to content

Instantly share code, notes, and snippets.

@luckman212
Last active March 7, 2024 16:37
Show Gist options
  • Save luckman212/d2570b072fd1698492b0e17183990004 to your computer and use it in GitHub Desktop.
Save luckman212/d2570b072fd1698492b0e17183990004 to your computer and use it in GitHub Desktop.
Self contained version that should fully work for generating Markdown links via Service Station [REQUIRES Python3 from Homebrew!]
#!/usr/bin/env bash
###############################################
### requires python3 installed via Homebrew ###
###############################################
unset TARGETED_URL
MENU_KIND=-1
SELECTED_ITEM_URLS=()
_pfx() {
case $(arch) in
arm64) echo '/opt/homebrew';;
*) echo '/usr/local';;
esac
}
py3=$(_pfx)/bin/python3
_realpath() {
$py3 -c 'import sys,os; print(os.path.realpath(sys.argv[1]));' "$1"
}
_urlenc() {
$py3 -c 'import sys; from urllib.parse import quote,unquote; print(quote(unquote(sys.argv[1])));' "$1"
}
_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
}
_markdown() {
fullpath=$(_realpath "$1")
_canonicalize "$fullpath"
filename="${fullpath##*/}"
safe=$(_urlenc "${fullpath}")
ext=$(tr '[:upper:]' '[:lower:]' <<<"${filename##*.}")
case $ext in
gif|jpg|jpeg|png)
name="${filename%.*}"
echo "![$name](file://$safe)"
;;
*)
echo "[$filename](file://$safe)"
;;
esac
}
IFS=''
while [[ $# -gt 0 ]]; do
case $1 in
-targetedURL)
TARGETED_URL=$2;
shift 2
;;
-menuKind)
MENU_KIND=$2;
shift 2
;;
-selectedItemURLs)
shift
;;
*)
SELECTED_ITEM_URLS+=("$1")
shift
;;
esac
done
if [[ "${#SELECTED_ITEM_URLS}" -gt 0 ]]; then
for f in "${SELECTED_ITEM_URLS[@]}"; do
_markdown "$f"
done | pbcopy
fi
@Edomenegaz
Copy link

Hey Luke 👋 Just found this snippet here, but it does not seem to be working. Whenever I try to use it, the only thing that goes in my clipboard is the following string: [](file://) – are you still using the script as of today? Is it working or it required some modifications? I wonder if some MacOS updates broke it somehow (permissions, maybe?). I'm running Sonoma.

@luckman212
Copy link
Author

@Edomenegaz Yes I'm still using this (often!). Just want to make sure: you're using it as part of Service Station right? It's specifically for that. Just double checking.

@Edomenegaz
Copy link

Yep. I'm using it with Service Station, and I think I've correctly setup the app's permissions. I've also run chmod +x on the script file and I have python3 installed with Homebrew.

@luckman212
Copy link
Author

@Edomenegaz ok, can you open a Terminal and run each of the commands below, and post the output?

python3 -V
brew --config
/bin/bash -c 'echo ${BASH_VERSINFO[@]} ; echo $PATH'

@Edomenegaz
Copy link

3.8.8
HOMEBREW_VERSION: 4.2.7
ORIGIN: https://github.com/Homebrew/brew
HEAD: 6170b6297c61ef0b68814c708eeff2199d8b0d11
Last commit: 4 weeks ago
Core tap JSON: 11 Feb 13:36 UTC
Core cask tap JSON: 11 Feb 13:36 UTC
HOMEBREW_PREFIX: /usr/local
HOMEBREW_REPOSITORY: /usr/local/Homebrew
HOMEBREW_CELLAR: /usr/local/Cellar
HOMEBREW_CASK_OPTS: []
HOMEBREW_DISPLAY: /private/tmp/com.apple.launchd.KIyRpfAIFQ/org.macosforge.xquartz:0
HOMEBREW_MAKE_JOBS: 10
Homebrew Ruby: 3.1.4 => /usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/3.1.4/bin/ruby
CPU: 10-core 64-bit westmere
Clang: 15.0.0 build 1500
Git: 2.12.2 => /usr/local/bin/git
Curl: 8.1.2 => /usr/bin/curl
macOS: 14.1-x86_64
CLT: 15.1.0.0.1.1700200546
Xcode: N/A
Rosetta 2: true
3 2 57 1 release arm64-apple-darwin23
/Users/myUser/.rvm/gems/ruby-2.4.0/bin:/Users/myUser/.rvm/gems/ruby-2.4.0@global/bin:/Users/myUser/.rvm/rubies/ruby-2.4.0/bin:/Users/myUser/opt/anaconda3/bin:/Users/myUser/opt/anaconda3/condabin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/opt/X11/bin:/Library/Apple/usr/bin:/opt/ImageMagick/bin:/Library/TeX/texbin:/Applications/Wireshark.app/Contents/MacOS:/usr/local/share/dotnet:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Users/myUser/.rvm/bin

@luckman212
Copy link
Author

Ok, that looks pretty normal (except your very old 3.8 version for Python...)

What about these

/usr/local/bin/python3 -V

And

/usr/local/bin/python3 -c 'import sys; from urllib.parse import quote,unquote; print(quote(unquote(sys.argv[1])));' "hello world"

@Edomenegaz
Copy link

This outputs respectively:
Python 3.9.5
hello%20world

@luckman212
Copy link
Author

@Edomenegaz There was a missing ;; closure in one of the case statements which I just corrected above, but I don't think that was causing your issue.

If you run the script manually via the Terminal (not from within Service Station) e.g.

/path/to/obsidian_link2.sh 'abc/def/this is a test'

And then paste the contents of your clipboard, what do you get then?

@Edomenegaz
Copy link

@luckman212 First of all, I really appreciate your help. I don't want to waste your time, so don't bother too much if you have more important things to do 😄 If we can't make it work, I'll think about a different solution.

However, here's the content of the clipboard after running the script via Terminal.
[this is a test](file:///Users/myUser/abc/def/this%20is%20a%20test)

It seems to be working correctly here, right?

@luckman212
Copy link
Author

No problem. I don't mind trying to help. But, it's very strange. Yes the script seems to be working. So I would really focus on your Privacy & Security settings.

Maybe Service Station lost it's grant for Full Disk Access. I have definitely experienced a bug on macOS where even though the app is listed in FDA, something is broken underneath the surface. Usually removing the app and then re-adding it corrects this (if that is indeed your issue)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment