The dish on parenting ... check out the new ParentDish!

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.

Related Headlines

Reader Comments (Page 1 of 1)

Add your comments

Please keep your comments relevant to this blog entry. Email addresses are never displayed, but they are required to confirm your comments.

When you enter your name and email address, you'll be sent a link to confirm your comment, and a password. To leave another comment, just use that password.

To create a live link, simply type the URL (including http://) or email address and we will make it a live link for you. You can put up to 3 URLs in your comments. Line breaks and paragraphs are automatically converted — no need to use <p> or <br> tags.

New Users

Current Users

TUAW Features

Mac 101 iPhone Around the Worldask-tuaw
Mac News
Macworld (497)
.Mac (38)
Accessories (624)
Airport (75)
Analysis / Opinion (1291)
Apple (1609)
Apple Corporate (547)
Apple Financial (186)
Apple History (38)
Apple Professional (49)
Apple TV (160)
Audio (441)
Bad Apple (119)
Beta Beat (148)
Blogging (87)
Bluetooth (15)
Bugs/Recalls (56)
Cult of Mac (869)
Deals (200)
Desktops (114)
Developer (205)
Education (93)
eMac (10)
Enterprise (126)
Features (373)
Freeware (363)
Gaming (344)
Graphic Design (18)
Hardware (1269)
Holidays (41)
Humor (587)
iBook (65)
iLife (237)
iMac (183)
Internet (306)
Internet Tools (1289)
iPhone (1350)
iPod Family (2020)
iTS (962)
iTunes (794)
iWork (18)
Leopard (355)
Mac mini (109)
Mac Pro (50)
MacBook (196)
MacBook Air (77)
Macbook Pro (214)
Multimedia (433)
Odds and ends (1417)
Open Source (271)
OS (890)
Peripherals (190)
Podcasting (181)
Podcasts (83)
Portables (196)
PowerBook (137)
PowerMac G5 (50)
Retail (572)
Retro Mac (47)
Rig of the Week (42)
Rumors (612)
Software (4220)
Software Update (395)
Steve Jobs (252)
Stocking Stuffers (55)
Surveys and Polls (96)
Switchers (111)
The Woz (34)
TUAW Business (227)
Universal Binary (281)
UNIX / BSD (62)
Video (909)
Weekend Review (74)
WIN Business (49)
Wireless (80)
XServe (35)
Mac Events
One More Thing (27)
Liveblog (0)
Other Events (231)
WWDC (181)
Mac Learning
Ask TUAW (96)
Blogs (85)
Books (23)
Books and Blogs (63)
Cool tools (444)
Hacks (462)
How-tos (480)
Interviews (33)
Mods (184)
Productivity (583)
Reviews (99)
Security (145)
Terminal Tips (56)
Tips and tricks (559)
Troubleshooting (161)
TUAW Features
iPhone 101 (23)
TUAW Labs (3)
Blast From the Past (17)
TUAW Tips (141)
Flickr Find (32)
Found Footage (70)
Mac 101 (81)
TUAW Interview (30)
Widget Watch (196)
The Daily Best (2)
TUAW Faceoff (5)

RESOURCES

RSS NEWSFEEDS

Powered by Blogsmith

Sponsored Links

The Unofficial Apple Weblog (TUAW) bloggers (30 days)

#BloggerPostsCmts
1Dave Caolo490
2Cory Bohon455
3Mat Lu374
4Erica Sadun351
5Scott McNulty341
6Michael Rose2919
7Brett Terpstra240
8Mike Schramm194
9Robert Palmer1322
10Steven Sande139
11Joshua Ellis64
12Christina Warren618
13Nik Fletcher41
14Chris Ullrich21
15Victor Agreda, Jr.14
16Jason Clarke11
17Lisa Hoover11

Featured Galleries

Macworld 2008 Keynote
Macworld 2008 Build-up
Macworld Expo 2007 show floor
The Macworld Faithful in Line
iPhone First Look
iPhone 2.0 - .Mac push e-mail
iMac 1998
TUAW Faceoff: Screenshot apps on the firing line
Boston Apple Store (Boylston Street)

 

    Most Commented On (7 days)

    Recent Comments

    More Apple Analysis

    More from AOL Money and Finance

    Weblogs, Inc. Network

    Other Weblogs Inc. Network blogs you might be interested in: