Hi Tim,

Cool - thanks for the code! I may use it to improve my email capture workflow :)

--Diego


On Thu, Apr 29, 2021 at 6:23 PM Tim Visher <tim.visher@gmail.com> wrote:

Hi Diego and Alexander,

Thanks for the tips here. I finally got around to trying them out. Here's what I ended up with and it's working perfectly.

(defun timvisher-org-link-mac-mail-open-link
    (mid _)
  (start-process "open-link" nil "open" (format "message://%%3C%s%%3E"
                                                mid)))

(defun timvisher-org-link-mac-mail-add-message-links
    ()
  (org-link-set-parameters
   "message" :follow #'timvisher-org-link-mac-mail-open-link))

(eval-after-load 'org
  '(timvisher-org-link-mac-mail-add-message-links))

Pairing that with my org-capture TODO Current Mail Template is a dream come true. :)

(defun timvisher-org-current-mail-get-selected-message-subject
    ()
  (with-temp-buffer
    (call-process
     "osascript" nil t nil
     "-e" "tell application \"Mail\" to get subject of item 1 of (selection as list)")
    (buffer-substring-no-properties (point-min) (- (point-max) 1))))

(defun timvisher-org-current-mail-get-selected-message-id
    ()
  (with-temp-buffer
    (call-process
     "osascript" nil t nil
     "-e" "tell application \"Mail\" to get message id of item 1 of (selection as list)")
    (browse-url-url-encode-chars
     (buffer-substring-no-properties (point-min) (- (point-max) 1))
     "[/]")))

(defun timvisher-org-current-mail-get-link-string
    ()
  (let ((subject (timvisher-org-current-mail-get-selected-message-subject))
        (message-id (timvisher-org-current-mail-get-selected-message-id)))
    (org-link-make-string (format "message:%s" message-id)
                          subject)))

(defun timvisher-org-current-mail-get-body-quote-template-element
    ()
  (let ((body (setq body (with-temp-buffer
                 (call-process
                  "osascript" nil t nil
                  "-e" "tell application \"Mail\" to get content of item 1 of (selection as list)")
                 (buffer-substring-no-properties (point-min) (- (point-max) 1))))))
    (format "

  #+begin_quote
%s
  #+end_quote"
            (string-join
             (seq-reduce
              (lambda (acc next)
                (if (string= next (or (car (last acc)) ""))
                    acc
                  (append acc (list next))))
              (mapcar (lambda (string)
                        (let ((string (string-trim string)))
                          (if (string= "" string)
                              string
                            (format "  %s" string))))
                      (split-string body "\n"))
              '())
             "\n"))))

(setq org-capture-templates
      '(…
        ("twcm" "TODO Work Current Mail" entry
         (file+headline "~/Dropbox/todo/work.org" "Inbox")
         "* TODO %(timvisher-org-current-mail-get-link-string)

  %U%(timvisher-org-current-mail-get-body-quote-template-element)" :prepend t :immediate-finish t)
        …))

Thanks so much! :)