I’m wondering if there’s a way to obtain a reference link/URL to an email in Apple’s Mail app, and paste it into a DT3 document? Clicking the link would then open the particular email in Mail.
I do this all of the time with a todo app called Things, and would love to be able to do the same in DT3.
You didn’t mention into what type of document. However you’re lucky, I wrote almost all parts of the script on other occasions - and by putting them together I now noticed that I forgot to escape special characters, so thanks for that.
This script copies both, a markdown and a RTF link.
Depending on the destination document one link is inserted when you paste, in RTF documents you’ll get a RTF link, in markdown a markdown link.
-- Copy mail link (as Markdown and RTF link)
-- The handler sets the clipboard to both, a Markdown and a RTF version of the link.
-- Depending on the destination document one link is inserted when you paste, in RTF documents you'll get a RTF link, in markdown a markdown link.
-- It can be used to either copy only links or links plus text.
tell application "Mail"
try
set theSelection to selection
if theSelection = {} or (count theSelection) > 1 then error "Please select one mail"
set theMail to item 1 of theSelection
set theSender to sender of theMail
set theSubject to subject of theMail
set theURL to "message://%3c" & message id of theMail & "%3e"
set theName to (theSender & " - " & theSubject) as string
my copyMarkdownRTFLink(theName, theURL, "", "", "")
display notification "Copied Link"
on error error_message number error_number
if the error_number is not -128 then display alert "Mail" message error_message as warning
return
end try
end tell
on copyMarkdownRTFLink(theName, theURL, theText, theDelimiter_Markdown, theDelimiter_HTML)
try
set theName to my replace_String(theName, "&", "&")
set theName to my replace_String(theName, "<", "<")
set theName to my replace_String(theName, ">", ">")
set theHTMLLink to ("<a href=\"" & theURL & "\">" & theName & "</a>") as string
do shell script "export LANG=\"en_US.UTF-8\" && echo '<font face=\"helvetica\">' " & quoted form of theText & quoted form of theDelimiter_HTML ¬
& quoted form of theHTMLLink & "'</font>' | textutil -format html -convert rtf -inputencoding UTF-8 -stdin -stdout | pbcopy -Prefer rtf"
if theText ≠ "" then set theText to theText & space & space
set theMarkdownText to (theText & theDelimiter_Markdown & "[" & theName & "](" & theURL & ")") as string
set theClipboard_record to the clipboard as record -- https://forum.latenightsw.com/t/how-do-i-set-clipboard-pasteboard-to-both-rich-text-rtf-and-plain-text/1189/3
set theClipboard_RTFdata to «class RTF » of theClipboard_record -- binary RTF data
set the clipboard to {Unicode text:theMarkdownText, «class RTF »:theClipboard_RTFdata}
on error error_message number error_number
activate application "Mail"
display alert "Error: Handler \"copyMarkdownRTFLink\"" message error_message as warning
error number -128
end try
end copyMarkdownRTFLink
on replace_String(theText, oldString, newString)
local ASTID, theText, oldString, newString, lst
set ASTID to AppleScript's text item delimiters
try
considering case
set AppleScript's text item delimiters to oldString
set lst to every text item of theText
set AppleScript's text item delimiters to newString
set theText to lst as string
end considering
set AppleScript's text item delimiters to ASTID
return theText
on error eMsg number eNum
set AppleScript's text item delimiters to ASTID
error "Can't replaceString: " & eMsg number eNum
end try
end replace_String
Save it to Mail’s script folder ~/Library/Scripts/Applications/Mail/
In case of this script the latter is the better option as you can then access it from within Mail.app without the need to switch to DEVONthink to run the script.
To use the script
select a mail
run the script from the menu
paste in a markdown or RTF document.
Note:
Clicking the RTF link does open the mail, however Mail.app does not become the active app, so it might be that the opened mail is hidden behind another app’s window. No idea why. Maybe something @cgrunenberg wants to check. Mail.app becomes active when a link is clicked in TextEdit.app.
Not to derail the thread, but wow, thanks for this app reference. This app solves a wish I’ve had for quite sometime, I index a lot of files into DT just to get this linking function with DT being the link source…but my DT database is getting too resource intensive and I needed to remove some of the indexed locations