1 ;;; org-mac-message.el --- Links to Apple Mail.app messages from within Org-mode
3 ;; Copyright (C) 2008, 2009 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw@gnu.org>
6 ;; Christopher Suckling <suckling at gmail dot com>
9 ;; Keywords: outlines, hypermedia, calendar, wp
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This file implements links to Apple Mail.app messages from within Org-mode.
28 ;; Org-mode does not load this module by default - if you would actually like
29 ;; this to happen then configure the variable `org-modules'.
31 ;; If you would like to create links to all flagged messages in an
32 ;; Apple Mail.app account, please customize the variable
33 ;; `org-mac-mail-account' and then call one of the following functions:
35 ;; (org-mac-message-insert-selected) copies a formatted list of links to
38 ;; (org-mac-message-insert-selected) inserts at point links to any
39 ;; messages selected in Mail.app.
41 ;; (org-mac-message-insert-flagged) searches within an org-mode buffer
42 ;; for a specific heading, creating it if it doesn't exist. Any
43 ;; message:// links within the first level of the heading are deleted
44 ;; and replaced with links to flagged messages.
50 (defgroup org-mac-flagged-mail nil
51 "Options concerning linking to flagged Mail.app messages"
55 (defcustom org-mac-mail-account
"customize"
56 "The Mail.app account in which to search for flagged messages"
57 :group
'org-mac-flagged-mail
60 (org-add-link-type "message" 'org-mac-message-open
)
62 ;; In mac.c, removed in Emacs 23.
63 (declare-function do-applescript
"org-mac-message" (script))
64 (unless (fboundp 'do-applescript
)
65 ;; Need to fake this using shell-command-to-string
66 (defun do-applescript (script)
67 (let (start cmd return
)
68 (while (string-match "\n" script
)
69 (setq script
(replace-match "\r" t t script
)))
70 (while (string-match "'" script start
)
71 (setq start
(+ 2 (match-beginning 0))
72 script
(replace-match "\\'" t t script
)))
73 (setq cmd
(concat "osascript -e '" script
"'"))
74 (setq return
(shell-command-to-string cmd
))
75 (concat "\"" (org-trim return
) "\""))))
77 (defun org-mac-message-open (message-id)
78 "Visit the message with the given MESSAGE-ID.
79 This will use the command `open' with the message URL."
80 (start-process (concat "open message:" message-id
) nil
81 "open" (concat "message://<" (substring message-id
2) ">")))
83 (defun as-get-selected-mail ()
84 "AppleScript to create links to selected messages in Mail.app"
87 "tell application \"Mail\"\n"
88 "set theLinkList to {}\n"
89 "set theSelection to selection\n"
90 "repeat with theMessage in theSelection\n"
91 "set theID to message id of theMessage\n"
92 "set theSubject to subject of theMessage\n"
93 "set theLink to \"message://\" & theID & \"::split::\" & theSubject & \"\n\"\n"
94 "copy theLink to end of theLinkList\n"
96 "return theLinkList as string\n"
99 (defun as-get-flagged-mail ()
100 "AppleScript to create links to flagged messages in Mail.app"
103 ;; Is Growl installed?
104 "tell application \"System Events\"\n"
105 "set growlHelpers to the name of every process whose creator type contains \"GRRR\"\n"
106 "if (count of growlHelpers) > 0 then\n"
107 "set growlHelperApp to item 1 of growlHelpers\n"
109 "set growlHelperApp to \"\"\n"
114 "tell application \"Mail\"\n"
115 "set theMailboxes to every mailbox of account \"" org-mac-mail-account
"\"\n"
116 "set theLinkList to {}\n"
117 "repeat with aMailbox in theMailboxes\n"
118 "set theSelection to (every message in aMailbox whose flagged status = true)\n"
119 "repeat with theMessage in theSelection\n"
120 "set theID to message id of theMessage\n"
121 "set theSubject to subject of theMessage\n"
122 "set theLink to \"message://\" & theID & \"::split::\" & theSubject & \"\n\"\n"
123 "copy theLink to end of theLinkList\n"
125 ;; Report progress through Growl
126 ;; This "double tell" idiom is described in detail at
127 ;; http://macscripter.net/viewtopic.php?id=24570 The
128 ;; script compiler needs static knowledge of the
129 ;; growlHelperApp. Hmm, since we're compiling
130 ;; on-the-fly here, this is likely to be way less
131 ;; portable than I'd hoped. It'll work when the name
132 ;; is still "GrowlHelperApp", though.
133 "if growlHelperApp is not \"\" then\n"
134 "tell application \"GrowlHelperApp\"\n"
135 "tell application growlHelperApp\n"
136 "set the allNotificationsList to {\"FlaggedMail\"}\n"
137 "set the enabledNotificationsList to allNotificationsList\n"
138 "register as application \"FlaggedMail\" all notifications allNotificationsList default notifications enabledNotificationsList icon of application \"Mail\"\n"
139 "notify with name \"FlaggedMail\" title \"Importing flagged message\" description theSubject application name \"FlaggedMail\"\n"
145 "return theLinkList as string\n"
148 (defun org-mac-message-get-links (select-or-flag)
149 "Create links to the messages currently selected or flagged in
150 Mail.app. This will use AppleScript to get the message-id and
151 the subject of the message in Mail.app and make a link out
153 (interactive "sLink to (s)elected or (f)lagged messages: ")
154 (message "AppleScript: searching mailboxes...")
156 (if (string= select-or-flag
"s")
157 (as-get-selected-mail)
158 (if (string= select-or-flag
"f")
159 (as-get-flagged-mail)
160 (error "Please select \"s\" or \"f\""))))
163 (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x
) (setq x
(match-string 1 x
))) x
)
164 (split-string as-link-list
"[\r\n]+")))
172 (setq split-link
(split-string (pop link-list
) "::split::"))
173 (setq URL
(car split-link
))
174 (setq description
(cadr split-link
))
175 (when (not (string= URL
""))
176 (setq orglink
(org-make-link-string URL description
))
177 (push orglink orglink-list
)))
180 (insert (concat (pop orglink-list
)) "\n"))
181 (kill-region (point-min) (point-max))
183 (message "Messages copied to kill-ring"))
185 (defun org-mac-message-insert-selected ()
186 "Insert a link to the messages currently selected in Mail.app.
187 This will use applescript to get the message-id and the subject of the
188 active mail in Mail.app and make a link out of it."
190 (org-mac-message-get-links "s")
193 ;; The following line is for backward compatibility
194 (defalias 'org-mac-message-insert-link
'org-mac-message-insert-selected
)
196 (defun org-mac-message-insert-flagged (org-buffer org-heading
)
197 "Asks for an org buffer and a heading within it. If heading
198 exists, delete all message:// links within heading's first
199 level. If heading doesn't exist, create it at point-max. Insert
200 list of message:// links to flagged mail after heading."
201 (interactive "bBuffer in which to insert links: \nsHeading after which to insert links: ")
203 (set-buffer org-buffer
)
204 (goto-char (point-min))
205 (let ((isearch-forward t
)
206 (message-re "\\[\\[\\(message:\\)\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"))
207 (if (org-goto-local-search-headings org-heading nil t
)
211 (while (re-search-forward message-re
(save-excursion (outline-next-heading)) t
)
213 (delete-region (match-beginning 0) (match-end 0)))
215 (org-mac-message-get-links "f")
217 (flush-lines "^$" (point) (outline-next-heading)))
219 (org-mac-message-get-links "f")
221 (goto-char (point-max))
224 (insert (concat org-heading
"\n"))
225 (org-mac-message-get-links "f")
228 (provide 'org-mac-message
)
230 ;; arch-tag: 3806d0c1-abe1-4db6-9c31-f3ed7d4a9b32
232 ;;; org-mac-message.el ends here