Generating Markdown Links to Mail Messages with Shortcuts and AppleScript

One of the system app updates we covered on AppStories this week that I’m most excited about is Mail. The app will finally introduce several advanced features this fall, including:

Undo send, allowing you to recall a message for 10 seconds after sending a message

Message scheduling with suggested and fully-customizable future delivery times and dates

Follow Up, which surfaces requests you’ve made in messages for which you haven’t received a response

Remind Me, a snooze-like feature for scheduling messages to reappear in your inbox later

Missing recipient and attachment alerts

Improved search

For the first time in quite a while, that list makes Mail a much more attractive alternative to third-party apps. Mail won’t match every feature offered by third parties, but my needs for advanced email client features are fairly modest, which I expect puts me squarely in the demographic that Apple is targeting.

Mimestream offers Gmail’s excellent search and other features in a native Mac package.

Until recently, my email use was split between Mimestream, which is only available on the Mac, and Spark on iOS and iPadOS. The split wasn’t ideal, but because I handle most of my email on my Mac, I tolerated it.

For the past several weeks, I’ve been using Mail exclusively on all of my devices, which has been a refreshing change of pace. Still, it’s not perfect. Of the features I use most in third-party mail clients, the single biggest shortcoming of Mail is its clunky implementation of deep linking.

I drop links to email messages in my notes and tasks all the time as a way to quickly access important contextual information. Mimestream offers Gmail URLs, and Spark can create its own app-specific and web URLs right within those apps’ UIs.

I like the way drag and drop on the iPhone and iPad links a message to its subject, but having to use drag and drop is clunky.

In contrast, on iOS and iPadOS, you can only link to a Mail message by dragging it out of Mail into another app’s text field. I’ll take it, but I’d prefer if I could quickly generate a link from the share sheet or with Shortcuts instead. The situation on the Mac isn’t much better, requiring users to resort to AppleScript to construct a URL that links back to a Mail message.

With weeks of Ventura testing ahead of me, I decided to see what I could do to improve the situation. The result isn’t perfect: I still have no choice on iOS and iPadOS but to drag and drop messages. However, I’ve improved the experience on the Mac using a combination of AppleScript and a shortcut that I trigger using Raycast to link the subject of a Mail message to its URL. For added context, my shortcut adds the sender’s name too.

Going into this project, I was aware of an AppleScript that’s been passed around automation forums for years and written about by David Sparks that relies on Python. The trouble is that Python is no longer pre-installed on the Mac. It’s not hard to install, but I wondered if there was a simpler way to create message links that didn’t include a Python dependency.

As it turns out, what I set out to do is pretty simple. The seed for the script I wrote started with a story Federico wrote about how he used Mail’s URL scheme in the iOS 7 days, which drew upon a script John Gruber posted on Daring Fireball in 2007. With that script as my guide, it didn’t take long to create one of my own using Mail’s built-in AppleScript support and a single-action shortcut that doesn’t require Python.

Generating a Markdown-formatted link to a Mail message with sender information using AppleScript

Mail has extensive AppleScript support, which makes it fairly simple to access the components of a message. The first step is to set the variable _msg to the selected message in Mail. Next, _messageURL takes the message’s message ID and drops it into the URL scheme that will open it in the Mail app regardless of where it is filed. Next, I access the subject of the message and its sender. Then, I pull the name from the sender variable using extract name from because the variable includes the person’s name and their email address, but all I want is the person’s name.

The final step is to assemble it all as a single string of Markdown-formatted text and copy that to the clipboard using AppleScript’s syntax for building strings. The script is then embedded in a single-action shortcut that uses the Run AppleScript action to trigger it. Now, I can have Mail open next to a note-taking app or task manager and drop in a link to the message and its sender by invoking Raycast and typing the alias I assigned to the shortcut (mlink) or the keyboard shortcut I gave it (⌃M). Plus, the links I create work across iOS, iPadOS, and macOS.

A Markdown-formatted source link with sender information in Obsidian.

My Mail Link shortcut, which you can download below, has improved how I link to messages on my Mac. However, because I have to rely on drag and drop on the iPhone and iPad, it’s not perfect. What I really want is a better solution from Apple. Ventura improves the interoperability of system apps across platforms and through collaboration with other users. The next step should be deeper integration at the content level, something which I’d like to think is foreshadowed Focus Filters, Apple’s new way to contextually display calendars, mailboxes, tab groups, and other content based on your iOS, iPadOS, and macOS Focus settings. Hopefully, a user-friendly deep linking solution from Apple will emerge in the future. Until then, though, I’ll be using Mail Link on the Mac and drag and drop on the iPhone and iPad.

You can download Mail Link here:

Mail Link

Mail Link is a Mac-only shortcut that uses AppleScript to link a Mail message’s URL to its subject using Markdown formatting. The referenced message includes the sender’s name too.

Get the shortcut here.