org-mac-message.el 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. ;;; org-mac-message.el --- Support for links to Apple Mail messages from within Org-mode
  2. ;; Copyright (C) 2008 Free Software Foundation, Inc.
  3. ;; Author: John Wiegley <johnw@gnu.org>
  4. ;; Version: 6.13pre02
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This file implements links to Apple Mail messages from within Org-mode.
  19. ;; Org-mode does not load this module by default - if you would actually like
  20. ;; this to happen then configure the variable `org-modules'.
  21. ;;; Code:
  22. (require 'org)
  23. (org-add-link-type "message" 'org-mac-message-open)
  24. ;; In mac.c, removed in Emacs 23.
  25. (declare-function do-applescript "org-mac-message" (script))
  26. (unless (fboundp 'do-applescript)
  27. ;; Need to fake this using shell-command-to-string
  28. (defun do-applescript (script)
  29. (let (start cmd 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. This will use the command `open' with the message URL."
  41. (start-process (concat "open message:" message-id) nil
  42. "open" (concat "message://<" (substring message-id 2) ">")))
  43. (defun org-mac-message-insert-link ()
  44. "Insert a link to the messages currently selected in Apple Mail.
  45. This will use applescript to get the message-id and the subject of the
  46. active mail in AppleMail and make a link out of it."
  47. (interactive)
  48. (insert (org-mac-message-get-link)))
  49. (defun org-mac-message-get-link ()
  50. "Insert a link to the messages currently selected in Apple Mail.
  51. This will use applescript to get the message-id and the subject of the
  52. active mail in AppleMail and make a link out of it."
  53. (let ((subject (do-applescript "tell application \"Mail\"
  54. set theMessages to selection
  55. subject of beginning of theMessages
  56. end tell"))
  57. (message-id (do-applescript "tell application \"Mail\"
  58. set theMessages to selection
  59. message id of beginning of theMessages
  60. end tell")))
  61. (org-make-link-string
  62. (concat "message://"
  63. (substring message-id 1 (1- (length message-id))))
  64. (substring subject 1 (1- (length subject))))))
  65. (provide 'org-mac-message)
  66. ;; arch-tag: 3806d0c1-abe1-4db6-9c31-f3ed7d4a9b32
  67. ;;; org-mac-message.el ends here