15 December 2010

Geek corner: easily hyperlink text in any Windows program

Disclaimer: this is geeky, but you might find it useful.

Further disclaimer: this is a Windows-only solution. If you use a Mac, Linux box, or CR-48, find your own geek to solve this problem for you.

I don't know about you, but I'm constantly writing documents (emails, blog posts, word docs, etc.) using various rich text editors, and wanting to hyperlink text in those documents. Generally speaking, I have found this frequent use case to be too much of a pain in the ass. The steps are:

- Copy the URL you're linking to onto the clipboard (perhaps using Ctrl-C).
- Highlight the text you want to hyperlink.
- Find the "make this a hyperlink" button or feature in the program you happen to be using. Often this is a button that looks like a little chain, hiding in some toolbar somewhere. Perhaps there's a hotkey for it. Perhaps not.
- Wait for the "make this a hyperlink" dialog box to pop up. Make sure your focus is in the "URL" textbox in that dialog.
- Paste the URL into the textbox (perhaps using Ctrl-V).
- Press the "OK" button or hit Enter or whatever.
- Huzzah!

These are the steps I want:

- Copy the URL you're linking to onto the clipboard (perhaps using Ctrl-C).
- Highlight the text you want to hyperlink.
- Hit some hotkey to "paste" the URL onto the text in such a way as to make it a hyperlink.
- Huzzah!

(In fact, I sheepishly admit that I am always accidentally blowing away the text I wanted to turn into a link, and replacing it with the URL I wanted to link to, by forgetting that it doesn't work this way and hitting Ctrl-V. This means that I'm a dunderhead. Probably you're not.)

So, I did what every good programmer does when they want to solve a problem that can obviously be solved with code: I Googled to find out if anyone has already made a solution I can steal.

That failed. I did find an AutoHotkey script which does this trick when you're working in a raw HTML code editor, but I wanted something that will work in rich text editors (such as Gmail and Microsoft Office programs) as well as apps that actually have a "raw HTML" mode (such as Blogger and WordPress).

So I did what any good programmer does when Googling fails: break down and write some damn code. But not, as it turned out, very much code at all.

As it happened, I only needed a very tiny bit of glue to solve this problem, because others had gotten it nearly there. My final solution features a modified version of the AutoHotKey mentioned above (by Lowell Heddings) as well as a C# class to simplify interactions with the Windows clipboard (by Mike Stall). Beyond modding Lowell's script, all I actually had to write were two lines of C# code. FTW.

So anyway, now I can copy a URL to my clipboard, highlight a chunk of text in any HTML-aware Windows app, hit Ctrl-Alt-V, and turn that text into a hyperlink. Want the same capability? Here's what you do:

0) You'll need the .NET Framework 2.0 (or better) - you've probably got it already.
1) Download and install AutoHotKey.
2) Download LinkPaster.zip and unzip it to somewhere convenient, such as c:\Program Files.
3) In your LinkPaster folder (such as c:\Program Files\LinkPaster), double-click the InsertHyperlink.ahk file to install and run that AutoHotkey script.
4) Huzzah! You should be off to the races. Let me know if it doesn't work.

(BTW, this hack made me very happy while I was writing this post.)


image by small world (rights)


vitor said...

Dear Mike,
Thank you very much.
I took me weeks googling and found nothing. This works. The best for me is the LinkPaster. As for the ahk script, I can change it for my own specific purposes.
Thanks, once again.
Best regards,

Mike Croghan said...

Glad to be helpful, Vitor! Pretty much everybody else I've talked to has said, "that's not a problem I'm looking to solve." I use it every day.