How to Get URL and Link Text from Hyperlink?

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