Speedy creation of rich text links to Mail messages
If you use any applications with Cocoa-based text editors (TextEdit, Mail.app, and many more), you may have noticed that some of them, like Mail.app, recognize URLs and automatically turn them into links. The links are in Rich Text Format (RTF) and can be copied and pasted into other RTF-compatible text fields. To the best of my knowledge, though, there's no easy way to automate the creation of an RTF hyperlink, via AppleScript or other means. In programs that don't detect URLs, or if you want to link text to a URL, you generally have to select text, go to a menu item (Link..., Link Add..., etc.) that's a few submenus down, and then enter the url. Me? I'm always looking for the easy way out...
This little hack came about as part of an attempt to build a truly useful project management system using Mail.app and iCal (if that system comes to fruition, TUAW readers will be the first to know!). Needing a convenient way to paste links to email messages in Mail notes, I started with a shell script that inserts arguments into raw RTF code, and then picks it up on the clipboard as processed, hyperlinked rich text. This technique is useful in many circumstances, so I thought I'd share it.
The shell script itself is quite simple, but the code displays as a horrific mess on the web, so here's a link to a zip file. You can place it in any directory, make it executable with
chmod a+x rtflink.sh, and run it from Terminal with two arguments: the text of the link and the url. For example:
/Users/brett/Desktop/rtflink.sh "TUAW Rocks" "http://tuaw.com"
This script is a proof of concept and, if you know what you're doing, you can do a lot with it. To create it, I created a link in TextEdit and saved the file. I then opened the .rtf file in TextMate and copied out the raw RTF code. The shell script simply inserts the two arguments into a shell-escaped version of the code and then uses pbcopy to make the link in the clipboard. The secret ingredient is the "-Prefer rtf" argument in pbcopy, which takes the input as rich text rather than the raw source. Given that, you can easily make scripts to accomplish a few different tasks, such as turning any plain-text url into a link (using the selected url), linking selected text to a url in the clipboard or turning a url in the clipboard into rich text for pasting. Using something like ThisService, you could make a system-wide service (and hotkey) to handle a lot of these tasks.
The fully-functional example I'm providing is an AppleScript Script Bundle that takes the currently selected message in Mail and creates a rich text link from the subject of the email that is connected to the original message, and places it on the clipboard. I'm using it to rapidly add message links to my Mail notes. To try it out, unzip the archive and place the file "Copy email as RTF link.scptd" in ~/Library/Scripts/Applications/Mail, where the tilde (~) is your home directory. Now, in Mail, the script will show up under the script menu on the right side of the menubar. If you don't have a script menu, launch AppleScript Utility and select "Show Script menu in menu bar." With a single message selected, just run the script and you've got a link ready to paste into a Mail note (or any other Rich Text editor). After pasting, you can edit the text of the link by highlighting just the underlined text and starting to type.
If you know a little Applescript, you're more than welcome to add features, such as adding the sender's name to the subject text or possibly handling multiple emails. In the interest of simplicity, I didn't take this version any further than necessary. Download the Script Bundle and have fun, and be sure to share any modifications you come up with!
Note: given the simplistic approach, the RTF links created will always be in the same format (12 pt., blue, underlined, Regular Helvetica text). You can follow the procedure above and create different styles in TextEdit. If you happen to be well-versed in Rich Text code you can just edit the file directly, or (possibly?) fix it to match the format of the current paragraph.
Subscribe to Newsletter
Software Updatesmore updates
- Daily App: MyScript Calculator solves your hand-written math equations
- Findery app lets you discover the world around you using annotated notes and maps
- The Learnist app brings its crowd-sourced collection of information to your iPhone
- My cat Cinnamon reviews Friskies Cat Fishing 2
- Photo Grid Collage Maker is capable and free
- iExit gets new features and is now free