Linking to an e-mail

I’ve been using the following script for a couple of months now, both with Merlin and DEVONthink, in order to create quick “links” to particular e-mail messages in Apple Mail. It’s not a very pretty script, but it does get the job done.

Here’s what you do: Select an e-mail in Apple Mail, and then run this script (I use FastScripts Lite to bind the script to a key, like Control-Apple-R). The script takes a few seconds, then reveals a folder to you in the Finder with your e-mail file selected.

This file will not resemble your e-mail in any way. It will have a strange name, like “38578.emlx”. Anyway, just drag this file over to DEVONthink, and hold down Command-Option as you release it. This causes a link to the e-mail file to appear in DEVONthink, with the same crazy filename as the title.

Another thing the script does is copy the e-mail’s subject line to your clipboard. So just click on the title to rename it, and past the subject as its title.

The reason I manually paste in the title is that Merlin has no Applescript support at all. I’m sure this script could be improved for DEVONthink to do the importing/renaming automatically, behind the scenes. I’ll probably get on that soon.

Now that your e-mail is linked in DEVONthink, just double-click on it to reveal the message in Apple Mail. It even has a cute Apple Mail icon next to it. Handy for keeping a quick pointer to a significant project e-mail within your DEVONthink group for that project.

John

p.s. This script only works if the volume where you store your e-mail is being indexed by Spotlight, since it relies on Spotlight metadata to associate a particular e-mail with its filename (something Mail scripting will not tell you).


tell application "Mail" to set mailList to selection

try
	tell application "Mail" to set {myTitle, myID} to {subject, id} of item 1 of mailList
on error
	display dialog "Check that you have an email selected in Mail.app" buttons {"OK"}
	return
end try

set the clipboard to myTitle

set {myDels, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {"\""}}
set myTitle to text items of myTitle
set AppleScript's text item delimiters to {"\\\""}
set myTitle to myTitle as text
set AppleScript's text item delimiters to myDels

set {myDels, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {"'"}}
set myTitle to text items of myTitle
set AppleScript's text item delimiters to {"\\'"}
set myTitle to myTitle as text
set AppleScript's text item delimiters to myDels

set {myDels, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {"`"}}
set myTitle to text items of myTitle
set AppleScript's text item delimiters to {"\\`"}
set myTitle to myTitle as text
set AppleScript's text item delimiters to myDels

set someEmails to paragraphs of (do shell script "mdfind \"kMDItemTitle == '" & myTitle & "' && kMDItemKind == 'emlx'\"")

repeat with anEmail in someEmails
	if anEmail contains myID then
		set myFile to do shell script "python -c 'import sys, urllib; print urllib.quote(sys.argv[1])' " & quoted form of anEmail
		
		try
			set myFile to POSIX file myFile
			
			tell application "Finder"
				activate
				reveal file myFile
				select myFile
			end tell
		on error
			display dialog ("Could not find file '" & myFile as string) & "'"
			return
		end try
	end if
end repeat