Make Mail email ID for Spotlight

There seems to be a lot of people who want to target individual Mail.app emails from iCal events or other applications. Having looked into doing this with Applescript I had come to the conclusion that this very slow, or in some cases impossible.

That was until I came across the Unix commands ‘mdfind’ and ‘mdls’ while looking into a way around the complete uselessness of the Finder’s Find command. These commands are Unix hooks into Spotlight metadata searches. See: http://developer.apple.com/macosx/spotlight.html . Due to the use of Spotlight, this solution only works in Tiger.

This script takes the sent date and title of the first selected message in Mail. It then queries the Spotlight metadata to find the email messages that have the same title. It then loops through these to find the email file that has the same sent date. Then a 20 digit number is added to the Finder comments of this message’s file. The same number is added to the clipboard for you to paste where you like.

When you want to find the email, just copy the number into the Spotlight window.

I’m sure there are many possible improvements to this approach, but it’s not one I’ve come across being used before with Mail messages.

Best wishes

John M

-- Find Email ID --
-- John Maisey --
-- Will only work in OS 10.4 or later --
(*
0.1 -- 22 Aug 2005
What it does:
This script takes the first selected message in Mail.app, and adds a very individual 20 digit number to the Finder Comments of the email's file.  The number is also added to the clipboard to paste into the notes field of an iCal event (or anywhere else you want to target the message from). When you need to find the email, just copy the 'Find Email ID' number into the Spotlight search field and select the email message.
*)

-- Make list of references to currently selected emails.
tell application "Mail" to set mailList to selection

-- In try block to catch empty list errors when no message selected.
try
	-- Get subject and date sent from the first selected message.
	tell application "Mail" to set myProperties to {subject, date sent} of item 1 of mailList
on error
	-- If no email selected 
	display dialog "Check that you have an email selected in Mail.app" buttons {"OK"}
	return
end try

-- Make date in ISO 8601 format. --
-- Get year, month, day of month and time from the date sent.
set myYear to year of (item 2 of myProperties)
set myMonth to (characters -2 thru -1 of ("0" & (month of (item 2 of myProperties) as number))) as text
set myDay to (characters -2 thru -1 of ("0" & (day of (item 2 of myProperties) as number))) as text
set myTime to time string of (item 2 of myProperties)
set myTimeString to (myYear & "-" & myMonth & "-" & myDay & " " & myTime) as text

-- Find every email file whose title is the same as the subject of the email.
-- mdind is a handler for Tiger's Spotlight
set someEmails to paragraphs of (do shell script "mdfind \"kMDItemTitle == '" & (item 1 of myProperties) & "' && kMDItemKind == 'emlx'\"")

-- Double check for the right one by comparing sending dates. --
-- Loop through the emails found by their title.
repeat with anEmail in someEmails
	-- Get Creation Date and Item ID.
	set mykMDitems to do shell script "mdls -name kMDItemContentCreationDate -name kMDItemID " & (quoted form of anEmail)
	-- If Creation Date is the same as the original email's
	if mykMDitems contains myTimeString then
		-- Set the file's Finder comment to sent time and current time. 
		set myComment to "Find Email ID: " & words of (myTimeString & (time string of (current date))) as text
		-- Make Mac file path of the POSIX file path (anEmail).
		set myFile to (POSIX file anEmail) as string
		-- Add 'Email ID' to the file's Finder Comments.
		tell application "Finder" to set comment of file myFile to (comment of file myFile & return & myComment)
		-- Having found the message exit the anEmail repeat loop
		exit repeat
	end if
end repeat

-- Add text to clipboard for pasting into iCal and let us know.
set the clipboard to (myComment & return & "Copy this ID into Spotlight.") as text
display dialog "The clipboard contains the " & myComment & return & "Paste this into the iCal event's notes." buttons {"OK"}

-- All done. --