This Week's Sponsor:

Winterfest 2024

The Festival of Artisanal Software


Send Selected Mail Message To Evernote With Source URL

In trying out the new Evernote beta for Mac, I ended up looking for a way to quickly send a selected Mail message to Evernote. I knew it was possible with AppleScript, but after searching on Google, I couldn’t find the perfect solution that fit my needs. Fortunately, by combining this script with an old tip by John Gruber, I managed to assemble a simple AppleScript that does exactly what I want.

Mine is a super-quick modification of this AppleScript by Efficient Computing. The original AppleScript uses a selected Mail message as the base for a new rich text note in Evernote that contains:

  • The message’s Subject as title
  • A short header of “From”, “Subject”, “Date”, and “To” fields
  • The message’s content as the actual note (in rich text)

To ensure a lightweight communication between Evernote and Mail.app, I wanted to add message:// URLs to the note. Not widely documented, message:// is a URL scheme that Mail uses in combination with so-called “message IDs” to reference individual messages. It means that, on OS X, you can click such URL or manually launch one (from, say, Safari) to go directly to a message (it will open in a new Mail.app window).

In researching options, I found this 2007 post by John Gruber explaining possible schemes for message:// URLs:

The first URL format listed above — message:%3cMESSAGE-ID%3e (no slashes) — is the one that Mail generates when you drag a message out of Mail. However, I have found that the second format — message://%3cMESSAGE-ID%3e (with slashes) — is better. Here’s why: if you paste the URL itself into a text field in any Cocoa app that uses NSTextView, you can then Control-click anywhere in the URL itself and use the Open URL and Make Link commands at the top of the contextual menu, because Cocoa will recognize the text as a URL.

With Cocoa apps, that is still the case in Mountain Lion. By percent-encoding the < and > characters, apps like TextEdit and Safari will offer an “Open URL” option upon control-clicking any portion of the URL.

But I’ve found Gruber’s suggested scheme has an advantage for Evernote as well. Evernote has a good AppleScript dictionary, and among the definitions of the suite there’s a “source URL” property that defines the source link for a specific note. Usually, this is used for notes that are clipped from webpages.

In the new Evernote 5, source URLs are available from a popover in a “URL” field with a “View Site” button; they are also displayed as root domains above the note’s editor.

In my tests, I’ve discovered that percent-encoded message:// URLs work better for Evernote’s source URL parameter. That URL format, in fact, becomes clickable in Evernote’s source field above the note editor. This is very handy if you want to open the original message in Mail without having to open the note’s detail popover first. I have, therefore, modified the original AppleScript to generate a message:// with John Gruber’s suggested method, using it as a source URL for a new note.

-- Slightly modified version of Efficient Computing's AppleScript: http://efficientcomputing.commons.gc.cuny.edu/2012/03/17/copy-email-message-in-mail-app-to-evernote-applescript/

tell application "Mail"

	--get selected messages

	set theSelection to selection

	
	--loop through all selected messages

	repeat with theMessage in theSelection

		
		--get information from message

		set theMessageDate to the date received of theMessage

		set theMessageSender to sender of theMessage

		set theMessageSubject to the subject of the theMessage

		set theMessageContent to the content of theMessage

		set theMessageURL to "message://%3c" & theMessage's message id & "%3e"

		
		--make a short header

		set theHeader to the all headers of theMessage

		set theShortHeader to (paragraph 1 of theHeader & return & paragraph 2 of theHeader & return & paragraph 3 of theHeader & return & paragraph 4 of theHeader & return & return)

		
		--import message to Evernote

		tell application "Evernote"

			synchronize

			set theNewNote to (create note with text (theMessageURL & return & return & theShortHeader & theMessageContent))

			set the title of theNewNote to theMessageSubject

			set the source URL of theNewNote to theMessageURL

			set the creation date of theNewNote to theMessageDate

			synchronize

		end tell

		
	end repeat

end tell

As you can see, I also repeat the message:// URL as the first line of the note. I’m doing this for two reasons. First, if Evernote ever decides to abandon the source URL parameter, I can make sure the link to a message won’t get lost, as I’ll have a backup string in the main note. The second reason is related to the Evernote app for iOS: while message:// URLs don’t work on iOS, one can always hope. I feel more comfortable knowing that, sometime in the future, I’ll probably be able to tap on an inline link in Evernote for iOS instead of bringing up an additional menu.

The AppleScript has its limitations, obiously. With rich HTML images containing backgrounds and other graphical layouts, it’ll only grab the text content of a message. Furthermore, because of the way it is designed, the script will capture the content of a message alongside every included quote level. Personally, I find this to be a nice plus (I always see an entire conversation in the note), but it’d be nice to have some kind of control on the contents of a message.

To use the AppleScript, you can use apps like Alfred, Keyboard Maestro, or FastScripts to assign a keyboard shortcut to it. Otherwise, use the built-in OS X Script menu to invoke it when Mail is frontmost.

If you have suggestions, feel free to ping me on Twitter or App.net.

Access Extra Content and Perks

Founded in 2015, Club MacStories has delivered exclusive content every week for nearly a decade.

What started with weekly and monthly email newsletters has blossomed into a family of memberships designed every MacStories fan.

Learn more here and from our Club FAQs.

Club MacStories: Weekly and monthly newsletters via email and the web that are brimming with apps, tips, automation workflows, longform writing, early access to the MacStories Unwind podcast, periodic giveaways, and more;

Club MacStories+: Everything that Club MacStories offers, plus an active Discord community, advanced search and custom RSS features for exploring the Club’s entire back catalog, bonus columns, and dozens of app discounts;

Club Premier: All of the above and AppStories+, an extended version of our flagship podcast that’s delivered early, ad-free, and in high-bitrate audio.