{"id":36592,"date":"2022-07-26T15:17:06","date_gmt":"2022-07-26T19:17:06","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=36592"},"modified":"2022-08-04T16:05:10","modified_gmt":"2022-08-04T20:05:10","slug":"mail-links-and-percentages","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2022\/07\/26\/mail-links-and-percentages\/","title":{"rendered":"Mail Links and Percentages"},"content":{"rendered":"<p><a href=\"https:\/\/leancrew.com\/all-this\/2022\/07\/mail-links-and-percentages\/\">Dr. Drang<\/a>:<\/p>\n<blockquote cite=\"https:\/\/leancrew.com\/all-this\/2022\/07\/mail-links-and-percentages\/\">\n<p>Yesterday, John Voorhees wrote <a href=\"https:\/\/www.macstories.net\/stories\/generating-markdown-links-to-mail-messages-with-shortcuts-and-applescript\/\">a nice article at MacStories<\/a> about creating links to specific email messages. His system is in the form of a Shortcut, but the real work is done by an AppleScript. The AppleScript is an extension of <a href=\"https:\/\/daringfireball.net\/2007\/12\/message_urls_leopard_mail\">one John Gruber wrote<\/a> 15 years ago.<\/p>\n<p>[&#8230;]<\/p>\n<p>The questions I needed to answer were:<\/p>\n<ol>\n<li>Are reserved characters used in message IDs?<\/li>\n<li>If so, do they need to be percent-encoded in a <code>message<\/code> URL?<\/li>\n<\/ol>\n<p>[&#8230;]<\/p>\n<p>None of the reserved characters caused a problem except the percent sign itself.<\/p>\n<\/blockquote>\n<p>However, that does not mean that the strings are valid URLs, and in fact even putting the message ID in unencoded angle brackets will prevent it from working with <code>NSURL<\/code>. I think what&rsquo;s happening here is that Mail is receiving the URL as a string and removing the percent escapes directly, without actually parsing it as a URL.<\/p>\n<p>So, if you store unencoded links you are setting yourself up for trouble if Mail changes how it works, if you ever want to redirect the <tt>message:<\/tt> scheme to a different app, or if you want to detect <tt>message:<\/tt> URLs that are embedded in a document. Lots of e-mails unfortunately don&rsquo;t follow the <a href=\"http:\/\/www.faqs.org\/rfcs\/rfc2822.html\">specification<\/a>, and I&rsquo;ve seen Message-ID headers that include the <tt>\"<\/tt> and <tt>)<\/tt> characters, as well as spaces, which could mess up the way <a href=\"https:\/\/daringfireball.net\/projects\/markdown\/syntax#link\">Markdown<\/a> interprets the links.<\/p>\n<p>If you want to handle more than the percent symbol, here&rsquo;s an AppleScript that will escape the other characters, too:<\/p>\n<pre>\nuse AppleScript version \"2.4\"\nuse framework \"Foundation\"\n\non URLFromMessageID(_messageID)\n    set _components to my NSURLComponents's alloc's init\n    _components's setScheme:\"message\"\n    _components's setHost:(\"&lt;\" &amp; _messageID &amp; \"&gt;\")\n    return _components's |URL|'s absoluteString() as Unicode text\nend URLFromMessageID\n<\/pre>\n\n<p>Another issue to be aware of is that the Message-ID header sometimes includes text outside of the angle brackets. This should not be included in the URL. However, if you are using Mail&rsquo;s <code>message id<\/code> AppleScript property, as Gruber&rsquo;s script does, it will remove the extraneous text for you (just as it removes the angle brackets themselves).<\/p>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2022\/04\/14\/bug-tracking-and-customer-support-tools\/\">Bug Tracking and Customer Support Tools<\/a><\/li>\n<\/ul>\n\n<p id=\"mail-links-and-percentages-update-2022-08-04\">Update (2022-08-04): <a href=\"https:\/\/daringfireball.net\/linked\/2022\/08\/01\/voorhees-shortcut-apple-mail-links\">John Gruber<\/a>:<\/p>\n<blockquote cite=\"https:\/\/daringfireball.net\/linked\/2022\/08\/01\/voorhees-shortcut-apple-mail-links\">\n<p>But 15 years after adding support for these URLs, Apple still hasn&rsquo;t exposed a direct way to copy them from any given message other than drag-and-drop.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Dr. Drang: Yesterday, John Voorhees wrote a nice article at MacStories about creating links to specific email messages. His system is in the form of a Shortcut, but the real work is done by an AppleScript. The AppleScript is an extension of one John Gruber wrote 15 years ago. [&#8230;] The questions I needed to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2022-07-26T19:17:09Z","apple_news_api_id":"aacecfc8-7395-4538-8c77-f869159e4cac","apple_news_api_modified_at":"2022-08-04T20:05:15Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/Aqs7PyHOVRTiMd_hpFZ5MrA","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[126,159,650,150,30,2077,337,71,489],"class_list":["post-36592","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-applemail","tag-applescript","tag-applescriptobjc","tag-email","tag-mac","tag-macos-12","tag-markdown","tag-programming","tag-url"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/36592","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=36592"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/36592\/revisions"}],"predecessor-version":[{"id":36663,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/36592\/revisions\/36663"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=36592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=36592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=36592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}