Create Markdown Link from Hyperlink Under Mouse [Example] Macro (v9.0.2)

MACRO:   Create Markdown Link from Hyperlink Under Mouse [Example]

--- VER: 1.0    2019-09-16 ---
Requires: KM 9.0.2+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

Create Markdown Link from Hyperlink Under Mouse [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • Create Markdown Link from Hyperlink Under Mouse

HOW TO USE

  1. First, make sure you have followed instructions in the Macro Setup below.
  2. Hover the Mouse Over a valid Hyperlink
  3. Trigger this macro.

MACRO SETUP

  • Carefully review the Release Notes and the Macro Actions
    • Make sure you understand what the Macro will do.
    • You are responsible for running the Macro, not me. ??
      .
      .
      Make These Changes to this Macro
  1. Assign a Trigger to this macro.
  2. Move this macro to a Macro Group that is only Active when you need this Macro.
  3. ENABLE this Macro, and the Macro Group it is in.
    .

REQUIRES:

  1. KM 9.0+ (may work in KM 8.2+ in some cases)
  2. macOS 10.11.6 (El Capitan)+

TAGS: @URL @Hyperlink @Browser @JavaScript

USER SETTINGS:

  • Any Action in magenta color is designed to be changed by end-user

ACTION COLOR CODES

  • To facilitate the reading, customizing, and maintenance of this macro,
    key Actions are colored as follows:
  • GREEN -- Key Comments designed to highlight main sections of macro
  • MAGENTA -- Actions designed to be customized by user
  • YELLOW -- Primary Actions (usually the main purpose of the macro)
  • ORANGE -- Actions that permanently destroy Variables or Clipboards,
    OR IF/THEN and PAUSE Actions

USE AT YOUR OWN RISK

  • While I have given this a modest amount of testing, and to the best of my knowledge will do no harm, I cannot guarantee it.
  • If you have any doubts or questions:
    • Ask first
    • Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.

What happens with an anchor tag that has no href?

While it is technically valid HTML, it has no meaning or purpose.
I can only say, why don't you just try it and find out what happens.

IAC, you will not get a valid Markdown link.

Actually, it provides a Markdown link that exactly models the hyperlink:
<a>An Anchor without a href</a>

yields:
[An Anchor without a href]()

There's an old computer saying for this: GIGO

Return ONLY URL

Several people have asked how to get only the URL.

You could easily modify this macro/script to return just the URL. This should do the trick:

JavaScript to Return URL

var linkElem = document.querySelector('a:hover');

var urlStr = '';

if (linkElem) {
   urlStr =  linkElem.href; }

urlStr;

Replace the script in the first Execute JavaScript Action with the above script.

2 Likes