New file org-mac-massage.el from John Wiegley.
[org-mode.git] / org-mac-message.el
bloba26f30d2a0942879b300f90e0c8c9634b3cdab16
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.1
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;;
9 ;; This file is not part of GNU Emacs.
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.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 (require 'org)
29 (org-add-link-type "message" 'org-mac-message-open)
31 (unless (fboundp 'do-applescript)
32 ;; Need to fake this using shell-command-to-string
33 (defun do-applescript (script)
34 (let (start return)
35 (while (string-match "\n" script)
36 (setq script (replace-match "\r" t t script)))
37 (while (string-match "'" script start)
38 (setq start (+ 2 match-beginning 0)
39 script (replace-match "\\'" t t script)))
40 (setq cmd (concat "osascript -e '" script "'"))
41 (setq return (shell-command-to-string cmd))
42 (concat "\"" (org-trim return) "\""))))
44 (defun org-mac-message-open (message-id)
45 "Visit the nnml message with the given Message-ID."
46 (start-process (concat "open message:" message-id) nil
47 "open" (concat "message:" message-id)))
49 (defun org-mac-insert-message-link ()
50 (interactive)
51 (let ((subject (do-applescript "tell application \"Mail\"
52 set theMessages to selection
53 subject of beginning of theMessages
54 end tell"))
55 (message-id (do-applescript "tell application \"Mail\"
56 set theMessages to selection
57 message id of beginning of theMessages
58 end tell")))
59 (insert (org-make-link-string
60 (concat "message://"
61 (substring message-id 1 (1- (length message-id))))
62 (substring subject 1 (1- (length subject)))))))
64 (provide 'org-mac-message)
66 ;;; org-mac-message.el ends here