Is there a KM Token or Variable which contains the link to (the URL of) the currently selected email in Apple Mail?

Thanks in advance for your time and help.

No.

But I suspect your question is actually "I can drag an email from Mail to application X and get a clickable link. How do I do that with a KM macro?"

The last part will depend on what application X is, among other things. But for the clickable link:

Make MessageLink.kmmacros (3.7 KB)

Summary

If you select a message in Mail's message viewer and run the above you'll get a "Display Text" window. Copy the text and paste it into Notes and you'll see it's a clickable link to the original email. Exactly what else you need to do (and whether it'll work!) will depend on application X.

1 Like

What I would do is:

  • Select an item in Mail
  • hit ⌥⌘M

as defined in this macro:
Copy as Markdown Link - Macro Library - Keyboard Maestro Discourse

which will copy a Markdown style labelled link to the clipboard in the pattern:

[selected Mail item title]( clickable link to selected Mail item )

3 Likes

Thanks very much for the detailed response and macro.

I put the macro in my Mail palette → selected email → ran the macro → spinning wheel for a while → error messsage

KM Engine Log below:

2022-06-24 08:23:06 Execute macro “Make MessageLink” from trigger Macro Set Palette
2022-06-24 08:23:07 Action 9641374 failed: Execute an AppleScript failed with script error: text-script:93:99: execution error: Can’t get item 1 of missing value. (-1728)
2022-06-24 08:23:07 Execute an AppleScript failed with script error: text-script:93:99: execution error: Can’t get item 1 of missing value. (-1728). Macro “Make MessageLink” cancelled (while executing Execute AppleScript).

Which macOS version are we looking at ?

The latest version of the Copy as MD Link macro group is kept at:

RobTrew/copy-as-md-link: macOS Keyboard Maestro macro group – single keystroke to copy MD links from different applications.

but here is a zip of the current version (working here on macOS 11.6.7)

(Note, you need the whole macro group)

MD Link tools Macros.kmmacros.zip (51.8 KB)

2 Likes

Catalina, latest version. thanks very much for the referencees

That probably means you had no message selected in Mail's message viewer, or you had more than one message viewer open and we're looking in the wrong one for a selected message.

Either way -- ignore it. Mine was just a quickly knocked up suggestion of how you could do it. @ComplexPoint's macro is much, much better and will probably be ready to go with whatever your target app is. Delete mine, install his, and be instantly more productive!

WOW !! fantastic macro. Thank you so much. Compatible with so many apps. Works like a charm. Easy to install thanks to your post.

1 Like

Yes, after testing, I tend to prefer @ComplexPoint 's macro. I remain very grateful for all your work and insight.

Third party solution that's working for me. Durable linking to email and much more.

Free version links emails and webpages only. It's $30 or $70 for more features:

Hey Nige,

That's why it's generally more productive to use Mail's selection property – as it works with the frontmost message viewer and returns the selected messages.

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2014/07/30 17:32
# dMod: 2023/01/08 02:48
# Appl: Mail
# Task: Place URL of selected messages into the Clipboard.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Mail, @URL, @Selected, @Messages, @Clipboard
--------------------------------------------------------

set linkList to {}

tell application "Mail"
   set selectedMessageList to get selection
   
   repeat with _msg in selectedMessageList
      set end of linkList to "message://%3c" & _msg's message id & "%3e"
   end repeat
   
   set AppleScript's text item delimiters to return
   
   set the clipboard to (linkList as Unicode text)
   
end tell

--------------------------------------------------------

If you need to work with the front message viewer then this is the method we found to work most reliably:

tell application "Mail"
   tell (some message viewer whose index is 1)
      set msg to item 1 of (get selected messages)
   end tell
   tell msg
      return its subject
   end tell
end tell

** True up through macOS 10.14.6 Mojave – I haven't tested myself on later versions.

-Chris

Noted -- although I think the main reason the first is more reliable is that repeats through the list rather than trying to get the first item. Repeat handles an empty list gracefully, getting an item doesn't.

Kept in case I ever get to use Mail again -- requirement for Shared Exchange mailboxes at work means I'm stuck with <spit!> Outlook for the foreseeable...

There's a point – but it really depends upon how you do your error-handling. My example was simple and didn't include any.

The point of the exercise was this:

For my own scripts that are run from FastScripts I often run without formal error-handlers, because FS has built-in error-handling and displays a nice error-dialog if/when there is one.

Actually my normal boilerplate for working with a single selected message is this:

tell application "Mail"
   set selectedMessageList to selection
   if selectedMessageList ≠ {} then
      set selectedMessage to item 1 of selectedMessageList
      tell selectedMessage
         # Stub
      end tell
   else
      error "Zero messages were selected!"
   end if
end tell

However – if I'm feeling really lazy and am doing something one-off I might write it like this:

tell application "Mail"
   tell item 1 of (get selection)
      return its subject
   end tell
end tell

Or this:

tell application "Mail"
   (get selection)'s item 1's subject
end tell

Or even this:

tell application "Mail"
   set mRef to a reference to (get selection)'s item 1
   set msgSubject to mRef's subject
   set msgID to mRef's id
end tell

:sunglasses:

1 Like

My default setting, I'm afraid!

1 Like