Getting Title of a Webpage and Paste It as a Link

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:

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.

  1. The URL is assumed to be in the System Clipboard, but you can change that according to your needs, this being Keyboard Maestro.
  2. Action: Execute Shell Script – with input from System Clipboard, execute text script, ignore results (see script below)
  3. 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.

1 Like

@Muotki, welcome to the forum! It looks like you have jumped in with both feet. I'm sure you will like it here. I look forward to your contributions.

Here are a couple posts worth checking out:

With regards to Markdown Links, you might want to check out Copy as Markdown Link by @ComplexPoint. It's very powerful.

No point in doing with just one foot, right? :wink: I now realise I maybe should've attached the script instead of copy the text of it; different forums, different cultures (I obviously missed this when I browsed the FAQ and newbie guides). I thought it would be a bit overkill with adding the macro itself, hence I described it in text instead. :smirk:

I need to study the "Markdown Link" macro you mention, it seemed to be very complex when I gave it a glance just now. I think though it tries to solve a different problem from the one I wanted to solve, but might be handy once I understand what it does.

I wanted to keep the solution simple, the macros I found that did something similar also queried the browser for link and title. Even if I would prefer that option to access the web again, they usually needed to do special handling of Firefox, which is the main browser for me. Since I am ... not found of kludges I stayed away from those solutions. The benefit of this solution is also that all it needs is the URL – regardless of where it comes from. The reason I wanted to share this was to show to others that it can be made pretty simple.

2 Likes

IMO (disclaimer: I'm not a forum moderator), the method you shared is just fine. I provided the forum links for future reference.

I have seen a number of cases where someone is seeking help and only describes the problem. The first response is often: please share/upload your macro.

Fair enough.

Sounds like a great strategy. Thanks for sharing.

Again, welcome!

1 Like