10.6: Create an RTF-formatted link to current Safari page

Oct 21, '09 07:30:04AM

Contributed by: alucard

Snow Leopard only hintTo copy a clickable hyperlink of the currently shown website from Safari (with the website title and its URL embedded in it) to the clipboard, in order to paste it somewhere else, you would have to: Open a rich text editor, drag the link from the Safari URL bar to the text editor, Control-click on the created hyperlink, and finally, click on Copy Link. This is tedious work. Safari should have a "copy hyperlink to clipboard" button.

Because it didn't, I wrote my own Service in 10.6 to get the job done. Here's how:

  1. Open Automator and create a new Service. In the Service Receives section, set the two drop-down menus to No Input and Any Application.
  2. Find and drag the Run AppleScript action into the work area.
  3. Paste the following AppleScript into the code box in the action item:
  4. tell application "Safari"
      set theURL to URL of document 1
      set theTitle to name of document 1
      
      set startEcho to "echo "
      set echoDelimiter to "'"
      
      set html_1 to "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">
      <html>
      <head>
        <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
        <meta http-equiv=\"Content-Style-Type\" content=\"text/css\">
        <title>"
      set html_2 to "</title>
        <meta name=\"Generator\" content=\"Cocoa HTML Writer\">
        <meta name=\"CocoaVersion\" content=\"1038.11\">
          <style type=\"text/css\">
            p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
          </style>
        </head>
      <body>
      <p class=\"p1\"><a href=\""
      set html_3 to "\">"
      set html_4 to "</a></p>
      </body>
      </html>"
      
      set echoCommand to startEcho & echoDelimiter & html_1 & theTitle & html_2 & theURL & html_3 & theTitle & html_4 & echoDelimiter
      set textutilCommand to " | textutil -convert rtf -inputencoding UTF-8 -format html -stdin -stdout"
      set pbcopyCommand to " | pbcopy -Prefer rtf"
      
      set entireCommand to echoCommand & textutilCommand & pbcopyCommand
      
      do shell script entireCommand
    end tell
    This AppleScript sends a string that contains the website title and URL in html format to the terminal program textutil. textutil converts that HTML string into an RTF-formatted string and sends it to pbcopy, which copies the string to the Mac OS clipboard.
  5. Save this Service with any name you like.
  6. Open System Preferences » Keyboard, and select Services in the left-hand column.
  7. Select the Service you just created, and define a keyboard shortcut for it.
Now, every time you press that shortcut, from any Services-aware application, a clickable hyperlink with the title and the embedded URL of the currently-active Safari tab will be copied to the Mac OS clipboard, ready to be pasted in any other application. This works great with an app that keeps track of your recent clipboard items.

[robg adds: This works fine in 10.6, and can be used in 10.4 and 10.5 as well, though not as a Service. Because textutil was added in 10.4, this hint won't work in older system releases.]

Comments (22)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20091002090934432