org-mac-message.el 8.9 KB

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