Release 6.05b.
[org-mode.git] / lisp / org-mac-message.el
blob871547b1eba59b174cdf4cbfd1995d44bd54cec3
1 ;;; org-mac-message.el --- Support for links to Apple Mail messages from within Org-mode
3 ;; Copyright (C) 2008 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw@gnu.org>
6 ;; Version: 6.05b
7 ;; Keywords: outlines, hypermedia, calendar, wp
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs 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 of the License, or
14 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
25 ;; This file implements links to Apple Mail messages from within Org-mode.
26 ;; Org-mode does not load this module by default - if you would actually like
27 ;; this to happen then configure the variable `org-modules'.
29 ;;; Code:
31 (require 'org)
33 (org-add-link-type "message" 'org-mac-message-open)
35 (declare-function do-applescript "mac.c" (string))
36 (unless (fboundp 'do-applescript)
37 ;; Need to fake this using shell-command-to-string
38 (defun do-applescript (script)
39 (let (start cmd return)
40 (while (string-match "\n" script)
41 (setq script (replace-match "\r" t t script)))
42 (while (string-match "'" script start)
43 (setq start (+ 2 (match-beginning 0))
44 script (replace-match "\\'" t t script)))
45 (setq cmd (concat "osascript -e '" script "'"))
46 (setq return (shell-command-to-string cmd))
47 (concat "\"" (org-trim return) "\""))))
49 (defun org-mac-message-open (message-id)
50 "Visit the message with the given MESSAGE-ID.
51 This will use the command `open' with the message URL."
52 (start-process (concat "open message:" message-id) nil
53 "open" (concat "message://<" (substring message-id 2) ">")))
55 (defun org-mac-message-insert-link ()
56 "Insert a link to the messages currently selected in Apple Mail.
57 This will use applescript to get the message-id and the subject of the
58 active mail in AppleMail and make a link out of it."
59 (interactive)
60 (insert (org-mac-message-get-link)))
62 (defun org-mac-message-get-link ()
63 "Insert a link to the messages currently selected in Apple Mail.
64 This will use applescript to get the message-id and the subject of the
65 active mail in AppleMail and make a link out of it."
66 (let ((subject (do-applescript "tell application \"Mail\"
67 set theMessages to selection
68 subject of beginning of theMessages
69 end tell"))
70 (message-id (do-applescript "tell application \"Mail\"
71 set theMessages to selection
72 message id of beginning of theMessages
73 end tell")))
74 (org-make-link-string
75 (concat "message://"
76 (substring message-id 1 (1- (length message-id))))
77 (substring subject 1 (1- (length subject))))))
79 (provide 'org-mac-message)
81 ;; arch-tag: 3806d0c1-abe1-4db6-9c31-f3ed7d4a9b32
83 ;;; org-mac-message.el ends here