Skip to content

Instantly share code, notes, and snippets.

@luckman212
Last active March 7, 2024 16:37
Show Gist options
  • Select an option

  • Save luckman212/d2570b072fd1698492b0e17183990004 to your computer and use it in GitHub Desktop.

Select an option

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

@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