How to Get URL and Link Text from Hyperlink?

If I copy (or just select) a HTML hyperlink from a Web page or a RTF document, how can I get the parts of the underlying HTML code? IOW, I want to get the link text and URL into separate variables.

Example:
Hyperlink: Keyboard Maestro Web Site

would yield:
Link Text: Keyboard Maestro Web Site
URL: http://www.keyboardmaestro.com/main/

I have searched extensively in the KM docs, KM forum, and the Internet in general, and have found nothing.

I would prefer a solution that works with rich text documents (like TextEdit, Evernote, Word, etc) as well as web pages.

Thanks for any help.

Maybe I’ve found a clue. But I don’t know how to interpret and use data on the clipboard.

After I copy a hyperlink to the clipboard and run this AppleScript:

set recClip to the clipboard as record
log recClip
set dataHTML to «class HTML» of recClip
log dataHTML

return

I get this data from the log recClip:

(*«class utf8»:How to Get URL and Link Text from Hyperlink?, «class HTML»:«data HTML3C6D65746120636861727365743D277574662D38273E3C612068726566 . . .

I’m wondering if there is a way to decode the «class HTML»:«data to reveal the URL?

Any ideas?

More trial and error.

I found this AppleScript that seems to work, but it requires writing the clipboard to a file first. I’d rather not create a file unless there is no other way.

REF: http://macscripter.net/viewtopic.php?id=31297


set thex to «class HTML» of (the clipboard as record)
-- This will trigger an error if you've copied something other than HTML data
set f to (path to "temp" from user domain as text) & "temp.txt"
-- Writes to a file named "temp.txt" in "Macintosh HD:Users:[userid]:Library:Caches:TemporaryItems:temp.txt"
set newFile to open for access file f with write permission
set eof of newFile to 0
write thex to newFile
close access newFile
set newFile to open for access file f
set theHTML to read newFile

OK, I’m getting really close now.

I find the following command works OK in Terminal, but not in AppleScript;

osascript -e 'the clipboard as «class HTML»' |   perl -ne 'print chr foreach npack("C*",pack("H*",substr($_,11,-3)))'

This results in the HTML code I’m looking for:

<meta charset='utf-8'><a href="http://forum.keyboardmaestro.com/t/how-to-get-url-and-link-text-from-hyperlink/1579/1" class="title" style="color:
. . . etc

AppleScript:

set strCMD to "'the clipboard as «class HTML»' |   perl -ne 'print chr foreach unpack(\"C*\",pack(\"H*\",substr($_,11,-3)))'"

set strHTML to (do shell script strCMD)
log strHTML

This results in nothing being returned.

What am I doing wrong?

The latter does not have the whole shell command. It is missing the "osascript -e" part.

Dud! :frowning: of course.
I thought I had tried that, but obviously not.

OK, thanks Peter. It's working now.

I think you should find the snippets you need in the code used in the macro linked below.

You will see that you can bypass file-writing by using the clipboard itself – further piping through pbcopy and pbpaste