MACRO: Yet Another HTML Link Creator

There are many of these around, but I didn't find one that worked like I needed to cover my three possible possible use cases, which are:

  1. I have some text selected in an editor, and want to wrap it in the href tag and paste the URL that I've copied from somewhere. Something like this: <a href="{paste}">{selected text}</a>

  2. I don't have a copied URL, but I don't have any selected text, so I just want to insert the href tag, and put the cursor in position so I can type the link text, like this: <a href="{paste}">{cursor}</a>

  3. I'm an idiot and tried to use my macro when I don't have a URL as the most recent item on the clipboard. This happens a fair bit, as I'll copy a URL, then forget I'd copied some text from elsewhere and get annoyed when it gets inserted as a link.

The following macro handles all three cases. It first makes sure there's a URL on the clipboard; if there's not, it plays an annoying noise and optionally displays a notification.

Next it checks to see if the Copy menu item is active—if it is, it means there's a selection. It cuts the selected text, does some clipboard manipulation, and winds up pasting the link in finished form.

Finally, if there's nothing selected but there is a URL on the clipboard, it just inserts the tag and puts the cursor where it needs to be for me to type the link text.

View Screenshot

The attached macro is disabled and does not have a trigger assigned; I have mine in a global group, assigned to Control-U because I do this a lot.

Create hyperlink.kmmacros (38 KB)

Feel free to ask if you have any questions;