5

I would like to be able to get a list of all the hyperlinked URLs in any formatted text that I select on the Mac (formatted text such as a web page or a word processor document).

Preferably I'd like use Applescript or Automator to extract this list of hyperlinks from the text (so that I can then use Applescript to perform further processing on these URLs).

Note that I am talking about hyperlinks being extracted from formatted text, not just extracting URLs from text containing plaintext URLs.

This hyperlink extraction from formatted text seems like it should be a simple programming task, but I have been struggling to find a way to do this in either Applescript or Automator.

Automator can be set to accept rich text input from a text selection, or can input rich text from the clipboard, but I cannot find any way to access this rich text as a string within Automator or Applescript, such that I can then extract the hyperlinked URLs from the string of rich text data.

Once I get access to the rich text data as a string, there will be no problem in extracting the URLs.

Any suggestions on how I might solve this issue are gratefully received.

2
  • Are you saying that the links are embedded links, as in the body of the text simply has "Cool website", and there's an embedded link? One problems is that will depend on what you're copying from. A word document or from a web page, or what? Can you post an example?
    – jweaks
    Sep 21, 2015 at 18:46
  • Yes, I mean embedded links, also called hyperlinks. The source I am copying from can vary: it can be a website, or it can be a word processor document. I don't think this should make much difference, as the Mac clipboard tends to store the text in the rich text format in either case.
    – Ash90
    Sep 22, 2015 at 4:42

1 Answer 1

5

Applescript itself does not unpack embedded text, so you'll have to use a helper app one way or another. You can use do shell script 'textutil' to do some unimbedding of links:

perl -ne 'print chr foreach unpack("C*",pack("H*",substr($_,11,-3)))' | 
textutil -stdin -stdout -convert html -format rtf

Then, you'll have to extract the URLs. I would suggest using the Automator action 'Extract Data' to do this. If you set the whole thing up as an Automator Workflow, you could invoke it from Applescript. Or, if you save it as a Service, you can just run the whole thing from the Service. Here's a screenshot of that method that should show what you need: Workflow Example

Let me know if you have questions. You can see variations on this technique here.

Update: If you want to create this into a Service, it is difficult to coerce the built-in input from Automator into RTF. An effective method is to ignore the input and do a

keystroke "c" using command down

to copy the selected text to the clipboard and then use the workflow from there. See example: Service Workflow Example

8
  • That's fabulous, jweaks, it works really well. Thanks very much for your help; it is much appreciated. It was very easy to follow as well with that screen shot you kindly provided.
    – Ash90
    Sep 23, 2015 at 4:49
  • Just one question on this approach, if I may: I have set your code up in Automator; how would I convert this code to taking the Automator text selection as the text input, rather than using the clipboard as text input? There is a setting in Automator that allows it to input rich text.
    – Ash90
    Sep 23, 2015 at 4:54
  • To make it work on selected text, you will create not a "Workflow" but a "Service" in Automator. Then, at the top of the workflow, set it to work on selected text. You'll then remove the first piped command, the osascript command, and start the shell script with the perl command.
    – jweaks
    Sep 23, 2015 at 6:40
  • That's what I tried, but for some reason it did not work. What I did was make it an Automator Service, then I removed the first line of the shell script, and I changed the applescript to simply: on run {input} display dialog input as string end run, which then shows the extracted URLs in a pop-up dialog box. But no extracted URLs from my selected text appeared in the dialog box; it was just a blank dialog box (whereas they did appear in the dialog box in the clipboard version).
    – Ash90
    Sep 23, 2015 at 17:23
  • 1
    Brilliant script, saved me at least an hour. Thank you! May 22, 2018 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.