org-mac-message.el 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. ;;; org-mac-message.el - Support for links to Apple Mail messages by Message-ID
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2008 John Wiegley
  4. ;;
  5. ;; Author: John Wiegey <johnw@gnu.org>
  6. ;; Version: 1.2
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;;
  9. ;; This file is not part of GNU Emacs.
  10. ;;
  11. ;; This file is free software; you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 3, or (at your option)
  14. ;; any later version.
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  21. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. ;; Boston, MA 02110-1301, USA.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. (require 'org)
  25. (org-add-link-type "message" 'org-mac-message-open)
  26. (unless (fboundp 'do-applescript)
  27. ;; Need to fake this using shell-command-to-string
  28. (defun do-applescript (script)
  29. (let (start return)
  30. (while (string-match "\n" script)
  31. (setq script (replace-match "\r" t t script)))
  32. (while (string-match "'" script start)
  33. (setq start (+ 2 match-beginning 0)
  34. script (replace-match "\\'" t t script)))
  35. (setq cmd (concat "osascript -e '" script "'"))
  36. (setq return (shell-command-to-string cmd))
  37. (concat "\"" (org-trim return) "\""))))
  38. (defun org-mac-message-open (message-id)
  39. "Visit the message with the given Message-ID."
  40. (start-process (concat "open message:" message-id) nil
  41. "open" (concat "message://<" (substring message-id 2) ">")))
  42. (defun org-mac-message-insert-link ()
  43. "Insrt a link to the messages currently selected in Apple Mail."
  44. (interactive)
  45. (let ((subject (do-applescript "tell application \"Mail\"
  46. set theMessages to selection
  47. subject of beginning of theMessages
  48. end tell"))
  49. (message-id (do-applescript "tell application \"Mail\"
  50. set theMessages to selection
  51. message id of beginning of theMessages
  52. end tell")))
  53. (insert (org-make-link-string
  54. (concat "message://"
  55. (substring message-id 1 (1- (length message-id))))
  56. (substring subject 1 (1- (length subject)))))))
  57. (provide 'org-mac-message)
  58. ;;; org-mac-message.el ends here