New to the forum, but have been amazed by KM since I discovered it a year ago, and this is the reason I decided to come here today and create an account.
I wanted to take an URL and insert it in a document, e.g. LibreOffice, as a link with the title of the website as the text. I searched and found that similar questions have been asked a few times, to name the main ones I used for inspiration:
- How do I construct a link (title with embedded link) in KM, then paste that into evernote or an email?
- Set Clipboard to RTF Hyperlink & Plain Text MD Link
- Copy Markdown as rich text and plain unmarked text
I thought I should share my solution; the solution depends on Python and to command line programs that comes with macOS. I presume you use Python 3 installed via Fink, adopt accordingly.
- The URL is assumed to be in the System Clipboard, but you can change that according to your needs, this being Keyboard Maestro.
- Action: Execute Shell Script – with input from System Clipboard, execute text script, ignore results (see script below)
- Action: Paste
The script in step 2 is rather straightforward:
/Users/CompletePathToTheFileToAvoidNastySurprises/get-title-of-webpage.py | /usr/bin/textutil -format html -convert rtf -stdin -stdout | /usr/bin/pbcopy -Prefer rtf
(It is always prudent to have complete paths IMHO, don't forget to chmod u+x
on the the Python script).
The Python file is the reason for this post, I donate it to public domain, and it depends on the requests library.
#!/opt/sw/bin/python3.8
# -*- mode: python; coding: utf-8 -*-
import sys
import re
import requests # python -m pip install requests
url=sys.stdin.read()
title="No title"
lang = ""
try:
if m := re.search("https?://.*$", url):
url = m.group(0).strip()
r = requests.get(url)
if m := re.search("""<html[^>]+lang=["']?([a-z][a-z])""", r.text, flags=re.IGNORECASE):
lang = m.group(1)
if m := re.search("<title.*?>(.*?)</title.*?>", r.text, flags=re.DOTALL|re.IGNORECASE):
title = m.group(1).strip()
else:
url = "No link"
except:
url = "Error link" # Not good to ignore errors but so very many thing can go wrong
if lang:
lang=f''' hreflang="{lang}"'''
print(f"""<a href="{url}"{lang}>{title}</a>""")
One can argue if hreflang makes any difference.
I have a similar function in Emacs (I assume you can tell I use Emacs, can't you...?) and I wanted something similar for LibreOffice and friends.