I ran into this problem today, so I created a simple Alfred workflow that allows me to paste URL encoded links, including BibDesk links, into Obsidian so that they don’t break.
It’s a very simple workflow, which uses a keyboard trigger (I’ve set mine up to be bound to shift-alt-command-U) to retrieve the contents of the clipboard, pass it through a small Python script and place the result back into the clipboard, while pasting it into the frontmost application, e.g., Obsidian.
It looks like this:
If you want to recreate something like this, here’s how I set it up.
The Hotkey component is set up with the following parameters:
- Action: “Pass through to workflow”
- Argument: “macOS Clipboard Contents”
The Copy to Clipboard component is set up with the following:
- Type: “Plain Text”
- [x]: Automatically paste to front most app
- [x]: Mark item as transient in clipboard
The bulk of the work is done by the Run Script component:
- Language: “/usr/bin/python”, “with input as argv”
- running instances: “Sequentially”
- Script:
# encoding: utf-8
import sys
from urllib import quote, unquote
uri = sys.argv[1]
delim = uri.find("://")
if delim == -1:
# if protocol delimiter not found simple urlencode string
sys.stdout.write(quote(unquote(uri)))
else:
# use position of delim to separate protocol and address
sys.stdout.write(uri[:delim+3] + quote(unquote(uri[delim+3:])))
This script first tries to determine whether the clipboard contents contains a protocol delimiter. If this can’t be found it URL encodes the whole string. (This is helpful if you just want to transform some text, say a BibDesk cite key, for use in a URL.)
If the script discovers a protocol delimiter, however, it slices the string into the protocol and address, URL encodes the address only, and puts them back together again.
Either way the result is written to the standard output, so that it is passed to the Copy to Clipboard component.
I hope this helps.
Edit: Looking through @luckman212’s code for addessbook://
URIs, I realised that I should wrap the url encoding in quote(unquote(..))
instead of quote(..)
to avoid mangling already encoded text. So I’ve updated the script.