org-mac-message.el 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.02b
  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. (declare-function do-applescript "mac.c" (string))
  25. (unless (fboundp 'do-applescript)
  26. ;; Need to fake this using shell-command-to-string
  27. (defun do-applescript (script)
  28. (let (start cmd return)
  29. (while (string-match "\n" script)
  30. (setq script (replace-match "\r" t t script)))
  31. (while (string-match "'" script start)
  32. (setq start (+ 2 (match-beginning 0))
  33. script (replace-match "\\'" t t script)))
  34. (setq cmd (concat "osascript -e '" script "'"))
  35. (setq return (shell-command-to-string cmd))
  36. (concat "\"" (org-trim return) "\""))))
  37. (defun org-mac-message-open (message-id)
  38. "Visit the message with the given MESSAGE-ID.
  39. This will use the command `open' with the message URL."
  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. "Insert a link to the messages currently selected in Apple Mail.
  44. This will use applescript to get the message-id and the subject of the
  45. active mail in AppleMail and make a link out of it."
  46. (interactive)
  47. (insert (org-mac-message-get-link)))
  48. (defun org-mac-message-get-link ()
  49. "Insert a link to the messages currently selected in Apple Mail.
  50. This will use applescript to get the message-id and the subject of the
  51. active mail in AppleMail and make a link out of it."
  52. (let ((subject (do-applescript "tell application \"Mail\"
  53. set theMessages to selection
  54. subject of beginning of theMessages
  55. end tell"))
  56. (message-id (do-applescript "tell application \"Mail\"
  57. set theMessages to selection
  58. message id of beginning of theMessages
  59. end tell")))
  60. (org-make-link-string
  61. (concat "message://"
  62. (substring message-id 1 (1- (length message-id))))
  63. (substring subject 1 (1- (length subject))))))
  64. (provide 'org-mac-message)
  65. ;; arch-tag: 3806d0c1-abe1-4db6-9c31-f3ed7d4a9b32
  66. ;;; org-mac-message.el ends here