Wrap Selection In Link in any program on Mac OS 10.6

The Mac OS X Services menu got a serious bit of love for 10.6, but until now I haven’t really played around with it. Today, however, I finally got around to trying it out, and for the first time since I started using OS X years ago, Services are actually becoming useful for me.

The specific need I wanted to address was my inability to easily insert some common HTML elements (particularly links) in WriteRoom. I’ve owned WriteRoom for years thanks to a software bundle, but I never had any use for it until I discovered QuickCursor. Now I can’t get enough of it; full screen editing for text fields in Safari is brilliant.

The only thing I haven’t been happy about was needing to type out every blessed character when I needed light HTML. Although WriteRoom’s Applescript support does not apparently provide access to selected text, the new Services menu does.

To add HTML link insertion to WriteRoom (or any other text editor that supports Services, for that matter) first boot up Automator and create a new Service workflow:

service_workflow.jpg

By default, Automator will have you processing selected text in any application. Check the “Replaces selected text” checkbox, and if you’re only going to be using the action in WriteRoom (or similar application) make sure to use the dropdown to target that app and avoid cluttering up your Services menu:

service_config.jpg

Over at the top right, type “Applescript” in the search box to filter the list for the “Run Applescript” action (make sure you have the Library highlighted, or it may not show up). Drag the action into your workflow:

services_action.jpg

Select everything in the text box, and replace it with this:

on run {input, parameters}
    set linkDefault to (the clipboard as string)
    set linkDefault to my switchText(linkDefault, "\\\"", "\"")
    set targetLink to do shell script "echo \"" & linkDefault & "\"|sed -E \"s/(mailto:)?(.+@.+\\..+)/mailto:\\2/\""
    if targetLink does not start with "mailto:" then
        set targetLink to do shell script "echo \"" & linkDefault & "\"|sed -E \"s/^(([a-zA-Z0-9-]+\\.)*[a-zA-Z0-9-]+\\.[a-zA-Z]{2,4}(\\/.*)?)/http:\\/\\/\\1/\""
        if targetLink does not start with "http" then
            set targetLink to "http://"
        end if
    end if
    
    return "<a href=\"" & targetLink & "\">" & input & "</a>"
end run

on switchText(fromText, targetText, replaceText)
    set d to text item delimiters
    set text item delimiters to replaceText
    set fromText to fromText's text items
    set text item delimiters to targetText
    tell fromText to set fromText to item 1 & ({""} & rest)
    set text item delimiters to d
    return fromText
end switchText

Save your workflow, and you’re basically done! When you select some text in WriteRoom or elsewhere and run the action from the Services menu, it will wrap the text with a link. It also will check the clipboard to see if there’s a recognizable email address or URL on it, and if so will use it for the link (otherwise defaults to http://).

For maximum productivity benefits, of course, you’ll want to visit the System Preferences Keyboard pane and give your new Service action a shortcut; I’m using control-shift-L since that’s what I’m used to from Textmate and Espresso.

Worth noting is the fact that the above Applescript doesn’t display a dialog box to query you for a URL if it can’t detect one on the clipboard, so for maximum effect you’ll want to copy your target URL prior to running the action if possible. The reason for this minor problem is that the dialogs spawned by my Service action never received focus, and for myself mousing over to give the dialog focus is more work than option arrowing a few times to edit the default URL.

You can, of course, add simple Service actions to surround text in other arbitrary HTML tags, as well. Simply use an Applescript similar to this instead:

on run {input, parameters}
	return "<strong>" & input & "</strong>"
end run

The only major downside that I’ve found of using Services this way is that it isn’t as quick as I’d like. I experimented with using shell scripts or Python instead of Applescript, but there were no significant speed boosts; as best I can tell, however Services is harvesting the selected text and then replacing it afterward is simply a slow process.

I’d love to hear how other people are leveraging the new 10.6 Services menu! I have the feeling that I’m just scratching the surface with this script.

6 responses to “Wrap Selection In Link in any program on Mac OS 10.6”

Leave a response

  1. Great script. I’ve been using the “Wrap text as link” script out of the Hyperlink Helper bundle in TextMate, with some minor modifications to run it as a shell script in Services. Ruby makes the string handling and link detection much easier :).

    I used to use HumaneText to write everything in Markdown and convert on the spot. I’m not sure if those services will work anymore in Snow Leopard, but the new Automator tools would make it easy to replicate. I should look into that…

  2. RawheaD says:

    Hi, just saw this; exactly what I was looking for! :-D Thanks!

    One thing that would make it even better for me; I always want to wrap a text in a URL link with the target=”_blank” modifier… how can I add this to the script? (I’m clueless with AppleScript…) Thanks!

  3. Ian Beck says:

    To include target=”_blank”, replace this line in the original script:

    return "<a href=\"" & targetLink & "\">" & input & "</a>"

    With this:

    return "<a href=\"" & targetLink & "\" target=\"_blank\">" & input & "</a>"
  4. RawheaD says:
  5. Matt says:

    Thanks. This was very helpful!

  6. I just wanted to say thank you, Ian. I link stuff up hundreds of times a week. I can’t imagine the time I would have saved if I had found your script earlier. I know I’ll save tons o’ time now.

Respond to Brett Terpstra

(cancel reply)