Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Get Mail message IDs via AppleScript and MailTags Apps
I'll not tell you how much time I've wasted trying to find a way of using AppleScript to generate a link (filepath or URL) to an individual email message in Mail.app. There are a number of solutions out there, but most involve invoking a shell script to run a Spotlight query from the command line. Clever but cumbersome. However there is an easier way.

The message ID of an email message can be combined with the message:// protocol, which is provided by MailTags, to specify a URL for individual email messages. (MalTags is required in order to use this hint; without it, the message:// protocol isn't recognized.)

The simple AppleScript snippet below shows you how to easily get the message ID for a selected email message in Mail.app. You can then use this URL in your own applications. For example, embed a link to an email message in an iCal event or todo, or even use the URL in web-based applications. I've used message URLs to link email messages to actions in my own PHP-based GTD system. Nice.

Copy/paste the following AppleScript into Script Editor. Then run Mail.app, select an email message, and then run the script. You'll get the idea. Adapt the script for your own purposes.
tell application "Mail"
  set theSelectedMessages to selection
  set the selected_message to item 1 ¬
   of the theSelectedMessages
   set message_id to the message id of the selected_message
 end tell

 set message_url to "message://" & message_id
 open location message_url
As far as I know, this trick only works if you use Mail.app, but I've not tried it with other clients.
    •    
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[21,073 views]  

Get Mail message IDs via AppleScript and MailTags | 5 comments | Create New Account
Click here to return to the 'Get Mail message IDs via AppleScript and MailTags' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Get Mail message IDs via AppleScript and MailTags
Authored by: edified on Feb 20, '07 04:13:09PM
Finally! I use message ID's all the time to delete passwords from emails (why would you ever email someone their password anyway- bleh) This script helps me immensely- I'll just adapt it for my use.

Thanks very much, Ed
~Love Language? ~

[ Reply to This | # ]

Get Mail message IDs via AppleScript and MailTags
Authored by: FlyBoy on Feb 20, '07 04:58:48PM
I had previously discussed this in terms of iCal URL handling in this thread: http://www.macosxhints.com/comment.php?mode=view&cid=81797 MailTags is, for me, one of the must have utilities for OS X. I was glad to pay the shareware fee! Norm

[ Reply to This | # ]
Get Mail message IDs via AppleScript and MailTags
Authored by: amc on Feb 21, '07 11:37:06PM

I dont know if this is useful to anyone, but if you display 'Long Headers' in Mail, you can get the messageID from there. It appears that MailTags adds "%3C" to the beginning, and "%3E" to the end of the messageID, and of course the "message://" prefix.



[ Reply to This | # ]
Open a Mail Message via ID with just AppleScript
Authored by: unclecal on Mar 18, '07 02:55:20PM

You can open a message directly via applescript without the need for MailTags via the function below. But, make sure the TargetID isn't encased in <>.

on run {TargetAccount, TargetMailbox, TargetID}
    tell application "Mail"

        set myMailbox to mailbox TargetMailbox of account TargetAccount
        set myMessages to (messages of myMailbox)

        repeat with eachMessage in myMessages
           if message id of eachMessage is TargetID then
               tell eachMessage
                  open
                  exit repeat
               end tell
           end if
       end repeat

    end tell
end run


I'm guessing the script could be refined further to directly reference the desired message by ID without a loop. But the loop works well.



[ Reply to This | # ]
use file:// and AppleScript
Authored by: tbdavis on Apr 12, '07 11:49:11PM

This is a longer script, but it's pretty fast, and the URL opens very quickly in Mail.app and without the need for any third party software, which is not to say it's better, just different.

Save this file to ~/Library/Scripts/Applications/Mail/Get_Message_URL.scpt to have the AppleScript menu item appear only when Mail is the current application.


-- gets file: URLs for selected messages
using terms from application "Mail"
	
	-- If run from the Scripts menu
	on perform mail action with messages selectedMsgs
		set msgURLs to ""
		set doClipBoard to "Copy to clipboard"
		tell application "Mail"
			if (count of selectedMsgs) is equal to 0 then
				set msgURLs to "There are no selected messages."
				display dialog msgURLs with title "Message URLs" buttons {"Ok"} default button 1 with icon note giving up after 360
				return
			end if
			repeat with msg in selectedMsgs
				set mb to mailbox of msg
				set acctType to account type of account of mb
				set acctSuffix to ""
				if acctType is equal to imap then
					set acctSuffix to ".imapmbox"
				else if acctType is equal to Mac then
					set acctSuffix to ".imapmbox"
				else if acctType is equal to pop then
					set acctSuffix to ".mbox"
				else
					display dialog "Don't know account type '" & acctType & "'" buttons {"Ok"}
					return
				end if
				set acctFolder to account directory of account of mb
				set msgFolder to "Messages/"
				try
					repeat while class of mb is mailbox
						set mbName to name of mb
						set msgFolder to mbName & acctSuffix & "/" & msgFolder
						set mb to container of mb
					end repeat
				on error errMessage number errNumber
					display dialog "Error: " & errNumber & return & tab & errMessage with title "ERROR" buttons {"Ok"}
				end try
				set msgFile to (id of msg) & ".emlx"
				if msgURLs is not equal to "" then
					set msgURLs to msgURLs & return
				end if
				set msgURLs to msgURLs & "file://" & acctFolder & "/" & msgFolder & msgFile
			end repeat
		end tell
		display dialog msgURLs with title "Message URLs" buttons {doClipBoard, "Cancel"} default button 1 cancel button 2 giving up after 360
		if button returned of result is doClipBoard then
			tell me to set the clipboard to msgURLs
		end if
		
	end perform mail action with messages
	
	-- If run as an ordinary script, instead of directly from the Scripts
	-- menu, it will call the default handler instead.
	on run
		tell application "Mail" to set selectedMessages to selection
		tell me to perform mail action with messages (selectedMessages)
	end run
end using terms from


[ Reply to This | # ]