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: 1.2
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; This file is part of GNU Emacs.
  7. ;; 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, or (at your option)
  10. ;; 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; see the file COPYING. If not, write to the
  17. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18. ;; Boston, MA 02110-1301, USA.
  19. ;;; Commentary:
  20. ;; This file implements links to Apple Mail messages from within Org-mode.
  21. ;; Org-mode does not load this module by default - if you would actually like
  22. ;; this to happen then configure the variable `org-modules'.
  23. ;;; Code:
  24. (require 'org)
  25. (org-add-link-type "message" 'org-mac-message-open)
  26. (declare-function do-applescript "mac.c" (string))
  27. (unless (fboundp 'do-applescript)
  28. ;; Need to fake this using shell-command-to-string
  29. (defun do-applescript (script)
  30. (let (start cmd return)
  31. (while (string-match "\n" script)
  32. (setq script (replace-match "\r" t t script)))
  33. (while (string-match "'" script start)
  34. (setq start (+ 2 (match-beginning 0))
  35. script (replace-match "\\'" t t script)))
  36. (setq cmd (concat "osascript -e '" script "'"))
  37. (setq return (shell-command-to-string cmd))
  38. (concat "\"" (org-trim return) "\""))))
  39. (defun org-mac-message-open (message-id)
  40. "Visit the message with the given MESSAGE-ID.
  41. This will use the command `open' with the message URL."
  42. (start-process (concat "open message:" message-id) nil
  43. "open" (concat "message://<" (substring message-id 2) ">")))
  44. (defun org-mac-message-insert-link ()
  45. "Insert a link to the messages currently selected in Apple Mail.
  46. This will use applescript to get the message-id and the subject of the
  47. active mail in AppleMail and make a link out of it."
  48. (interactive)
  49. (insert (org-mac-message-get-link)))
  50. (defun org-mac-message-get-link ()
  51. "Insert a link to the messages currently selected in Apple Mail.
  52. This will use applescript to get the message-id and the subject of the
  53. active mail in AppleMail and make a link out of it."
  54. (let ((subject (do-applescript "tell application \"Mail\"
  55. set theMessages to selection
  56. subject of beginning of theMessages
  57. end tell"))
  58. (message-id (do-applescript "tell application \"Mail\"
  59. set theMessages to selection
  60. message id of beginning of theMessages
  61. end tell")))
  62. (org-make-link-string
  63. (concat "message://"
  64. (substring message-id 1 (1- (length message-id))))
  65. (substring subject 1 (1- (length subject))))))
  66. (provide 'org-mac-message)
  67. ;; arch-tag: 3806d0c1-abe1-4db6-9c31-f3ed7d4a9b32
  68. ;;; org-mac-message.el ends here