contrib/lisp/org-mac-link.el: Fix typo in the comment section
[org-mode/org-tableheadings.git] / contrib / lisp / org-mac-link.el
blobb0c2aedd5363da31484e08753e62db971352eab0
1 ;;; org-mac-link.el --- Grab links and url from various mac
2 ;; Application and insert them as links into org-mode documents
3 ;;
4 ;; Copyright (c) 2010-2013 Free Software Foundation, Inc.
5 ;;
6 ;; Authors:
7 ;; Anthony Lander <anthony.lander@gmail.com>
8 ;; John Wiegley <johnw@gnu.org>
9 ;; Christopher Suckling <suckling at gmail dot com>
10 ;; Daniil Frumin <difrumin@gmail.com>
12 ;;
13 ;; Version: 1.1
14 ;; Keywords: org, mac, hyperlink
16 ;; This file is not part of GNU Emacs.
18 ;; This program is free software; you can redistribute it and/or modify
19 ;; it under the terms of the GNU General Public License as published by
20 ;; the Free Software Foundation; either version 3, or (at your option)
21 ;; any later version.
23 ;; This program is distributed in the hope that it will be useful,
24 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
25 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 ;; GNU General Public License for more details.
28 ;; You should have received a copy of the GNU General Public License
29 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
31 ;;; Commentary:
33 ;; This code allows you to grab either the current selected items, or
34 ;; the frontmost url in various mac appliations, and insert them as
35 ;; hyperlinks into the current org-mode document at point.
37 ;; This code is heavily based on, and indeed incorporates,
38 ;; org-mac-message.el written by John Wiegley and Christopher
39 ;; Suckling.
41 ;; Detailed comments for each application interface are inlined with
42 ;; the code. Here is a brief overview of how the code interacts with
43 ;; each application:
45 ;; Finder.app - grab links to the selected files in the frontmost window
46 ;; Mail.app - grab links to the selected messages in the message list
47 ;; AddressBook.app - Grab links to the selected addressbook Cards
48 ;; Firefox.app - Grab the url of the frontmost tab in the frontmost window
49 ;; Vimperator/Firefox.app - Grab the url of the frontmost tab in the frontmost window
50 ;; Safari.app - Grab the url of the frontmost tab in the frontmost window
51 ;; Google Chrome.app - Grab the url of the frontmost tab in the frontmost window
52 ;; Together.app - Grab links to the selected items in the library list
53 ;; Skim.app - Grab a link to the selected page in the topmost pdf document
56 ;; Installation:
58 ;; add (require 'org-mac-link) to your .emacs, and optionally bind a
59 ;; key to activate the link grabber menu, like this:
61 ;; (add-hook 'org-mode-hook (lambda ()
62 ;; (define-key org-mode-map (kbd "C-c g") 'org-mac-grab-link)))
64 ;; Usage:
66 ;; Type C-c g (or whatever key you defined, as above), or type M-x
67 ;; org-mac-grab-link RET to activate the link grabber. This will present
68 ;; you with a menu to choose an application from which to grab a link
69 ;; to insert at point. You may also type C-g to abort.
71 ;; Customizing:
73 ;; You may customize which applications appear in the grab menu by
74 ;; customizing the group `org-mac-link'. Changes take effect
75 ;; immediately.
78 ;;; Code:
80 (require 'org)
81 (require 'org-mac-message)
83 (defgroup org-mac-link nil
84 "Options concerning grabbing links from external Mac
85 applications and inserting them in org documents"
86 :tag "Org Mac link"
87 :group 'org-link)
89 (defcustom org-mac-grab-Finder-app-p t
90 "Enable menu option [F]inder to grab links from the Finder"
91 :tag "Grab Finder.app links"
92 :group 'org-mac-link
93 :type 'boolean)
95 (defcustom org-mac-grab-Mail-app-p t
96 "Enable menu option [m]ail to grab links from Mail.app"
97 :tag "Grab Mail.app links"
98 :group 'org-mac-link
99 :type 'boolean)
101 (defcustom org-mac-grab-Addressbook-app-p t
102 "Enable menu option [a]ddressbook to grab links from AddressBook.app"
103 :tag "Grab AddressBook.app links"
104 :group 'org-mac-link
105 :type 'boolean)
107 (defcustom org-mac-grab-Safari-app-p t
108 "Enable menu option [s]afari to grab links from Safari.app"
109 :tag "Grab Safari.app links"
110 :group 'org-mac-link
111 :type 'boolean)
113 (defcustom org-mac-grab-Firefox-app-p t
114 "Enable menu option [f]irefox to grab links from Firefox.app"
115 :tag "Grab Firefox.app links"
116 :group 'org-mac-link
117 :type 'boolean)
119 (defcustom org-mac-grab-Firefox+Vimperator-p nil
120 "Enable menu option [v]imperator to grab links from Firefox.app running the Vimperator plugin"
121 :tag "Grab Vimperator/Firefox.app links"
122 :group 'org-mac-link
123 :type 'boolean)
125 (defcustom org-mac-grab-Chrome-app-p t
126 "Enable menu option [f]irefox to grab links from Google Chrome.app"
127 :tag "Grab Google Chrome.app links"
128 :group 'org-mac-link
129 :type 'boolean)
131 (defcustom org-mac-grab-Together-app-p nil
132 "Enable menu option [t]ogether to grab links from Together.app"
133 :tag "Grab Together.app links"
134 :group 'org-mac-link
135 :type 'boolean)
137 (defcustom org-mac-grab-Skim-app-p
138 (< 0 (length (shell-command-to-string
139 "mdfind kMDItemCFBundleIdentifier == 'net.sourceforge.skim-app.skim'")))
140 "Enable menu option [S]kim to grab page links from Skim.app"
141 :tag "Grab Skim.app page links"
142 :group 'org-mac-link
143 :type 'boolean)
145 (defcustom org-mac-Skim-highlight-selection-p nil
146 "Highlight (using notes) the selection (if present) when grabbing the a link from Skim.app"
147 :tag "Highlight selection in Skim.app"
148 :group 'org-mac-link
149 :type 'boolean)
151 (defgroup org-mac-flagged-mail nil
152 "Options concerning linking to flagged Mail.app messages."
153 :tag "Org Mail.app"
154 :group 'org-link)
156 (defcustom org-mac-mail-account "customize"
157 "The Mail.app account in which to search for flagged messages."
158 :group 'org-mac-flagged-mail
159 :type 'string)
162 ;; In mac.c, removed in Emacs 23.
163 (declare-function do-applescript "org-mac-message" (script))
164 (unless (fboundp 'do-applescript)
165 ;; Need to fake this using shell-command-to-string
166 (defun do-applescript (script)
167 (let (start cmd return)
168 (while (string-match "\n" script)
169 (setq script (replace-match "\r" t t script)))
170 (while (string-match "'" script start)
171 (setq start (+ 2 (match-beginning 0))
172 script (replace-match "\\'" t t script)))
173 (setq cmd (concat "osascript -e '" script "'"))
174 (setq return (shell-command-to-string cmd))
175 (concat "\"" (org-trim return) "\""))))
178 (defun org-mac-grab-link ()
179 "Prompt the user for an application to grab a link from, then go grab the link, and insert it at point"
180 (interactive)
181 (let* ((descriptors `(("F" "inder" org-mac-finder-insert-selected ,org-mac-grab-Finder-app-p)
182 ("m" "ail" org-mac-message-insert-selected ,org-mac-grab-Mail-app-p)
183 ("a" "ddressbook" org-mac-addressbook-insert-selected ,org-mac-grab-Addressbook-app-p)
184 ("s" "afari" org-mac-safari-insert-frontmost-url ,org-mac-grab-Safari-app-p)
185 ("f" "irefox" org-mac-firefox-insert-frontmost-url ,org-mac-grab-Firefox-app-p)
186 ("v" "imperator" org-mac-vimperator-insert-frontmost-url ,org-mac-grab-Firefox+Vimperator-p)
187 ("c" "hrome" org-mac-chrome-insert-frontmost-url ,org-mac-grab-Chrome-app-p)
188 ("t" "ogether" org-mac-together-insert-selected ,org-mac-grab-Together-app-p)
189 ("S" "kim" org-mac-skim-insert-page ,org-mac-grab-Skim-app-p)))
190 (menu-string (make-string 0 ?x))
191 input)
193 ;; Create the menu string for the keymap
194 (mapc '(lambda (descriptor)
195 (when (elt descriptor 3)
196 (setf menu-string (concat menu-string "[" (elt descriptor 0) "]" (elt descriptor 1) " "))))
197 descriptors)
198 (setf (elt menu-string (- (length menu-string) 1)) ?:)
200 ;; Prompt the user, and grab the link
201 (message menu-string)
202 (setq input (read-char-exclusive))
203 (mapc '(lambda (descriptor)
204 (let ((key (elt (elt descriptor 0) 0))
205 (active (elt descriptor 3))
206 (grab-function (elt descriptor 2)))
207 (when (and active (eq input key))
208 (call-interactively grab-function))))
209 descriptors)))
211 (defun org-mac-paste-applescript-links (as-link-list)
212 "Paste in a list of links from an applescript handler. The
213 links are of the form <link>::split::<name>"
214 (let* ((link-list
215 (mapcar
216 (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x) (setq x (match-string 1 x))) x)
217 (split-string as-link-list "[\r\n]+")))
218 split-link URL description orglink orglink-insert rtn orglink-list)
219 (while link-list
220 (setq split-link (split-string (pop link-list) "::split::"))
221 (setq URL (car split-link))
222 (setq description (cadr split-link))
223 (when (not (string= URL ""))
224 (setq orglink (org-make-link-string URL description))
225 (push orglink orglink-list)))
226 (setq rtn (mapconcat 'identity orglink-list "\n"))
227 (kill-new rtn)
228 rtn))
232 ;; Handle links from Firefox.app
234 ;; This code allows you to grab the current active url from the main
235 ;; Firefox.app window, and insert it as a link into an org-mode
236 ;; document. Unfortunately, firefox does not expose an applescript
237 ;; dictionary, so this is necessarily introduces some limitations.
239 ;; The applescript to grab the url from Firefox.app uses the System
240 ;; Events application to give focus to the firefox application, select
241 ;; the contents of the url bar, and copy it. It then uses the title of
242 ;; the window as the text of the link. There is no way to grab links
243 ;; from other open tabs, and further, if there is more than one window
244 ;; open, it is not clear which one will be used (though emperically it
245 ;; seems that it is always the last active window).
247 (defun org-as-mac-firefox-get-frontmost-url ()
248 (let ((result (do-applescript
249 (concat
250 "set oldClipboard to the clipboard\n"
251 "set frontmostApplication to path to frontmost application\n"
252 "tell application \"Firefox\"\n"
253 " activate\n"
254 " delay 0.15\n"
255 " tell application \"System Events\"\n"
256 " keystroke \"l\" using {command down}\n"
257 " keystroke \"a\" using {command down}\n"
258 " keystroke \"c\" using {command down}\n"
259 " end tell\n"
260 " delay 0.15\n"
261 " set theUrl to the clipboard\n"
262 " set the clipboard to oldClipboard\n"
263 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
264 "end tell\n"
265 "activate application (frontmostApplication as text)\n"
266 "set links to {}\n"
267 "copy theResult to the end of links\n"
268 "return links as string\n"))))
269 (car (split-string result "[\r\n]+" t))))
271 (defun org-mac-firefox-get-frontmost-url ()
272 (interactive)
273 (message "Applescript: Getting Firefox url...")
274 (let* ((url-and-title (org-as-mac-firefox-get-frontmost-url))
275 (split-link (split-string url-and-title "::split::"))
276 (URL (car split-link))
277 (description (cadr split-link))
278 (org-link))
279 (when (not (string= URL ""))
280 (setq org-link (org-make-link-string URL description)))
281 (kill-new org-link)
282 org-link))
284 (defun org-mac-firefox-insert-frontmost-url ()
285 (interactive)
286 (insert (org-mac-firefox-get-frontmost-url)))
289 ;; Handle links from Google Firefox.app running the Vimperator extension
290 ;; Grab the frontmost url from Firefox+Vimperator. Same limitations are
291 ;; Firefox
293 (defun org-as-mac-vimperator-get-frontmost-url ()
294 (let ((result (do-applescript
295 (concat
296 "set oldClipboard to the clipboard\n"
297 "set frontmostApplication to path to frontmost application\n"
298 "tell application \"Firefox\"\n"
299 " activate\n"
300 " delay 0.15\n"
301 " tell application \"System Events\"\n"
302 " keystroke \"y\"\n"
303 " end tell\n"
304 " delay 0.15\n"
305 " set theUrl to the clipboard\n"
306 " set the clipboard to oldClipboard\n"
307 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
308 "end tell\n"
309 "activate application (frontmostApplication as text)\n"
310 "set links to {}\n"
311 "copy theResult to the end of links\n"
312 "return links as string\n"))))
313 (replace-regexp-in-string "\s+-\s+Vimperator" "" (car (split-string result "[\r\n]+" t)))))
316 (defun org-mac-vimperator-get-frontmost-url ()
317 (interactive)
318 (message "Applescript: Getting Vimperator url...")
319 (let* ((url-and-title (org-as-mac-vimperator-get-frontmost-url))
320 (split-link (split-string url-and-title "::split::"))
321 (URL (car split-link))
322 (description (cadr split-link))
323 (org-link))
324 (when (not (string= URL ""))
325 (setq org-link (org-make-link-string URL description)))
326 (kill-new org-link)
327 org-link))
329 (defun org-mac-vimperator-insert-frontmost-url ()
330 (interactive)
331 (insert (org-mac-vimperator-get-frontmost-url)))
334 ;; Handle links from Google Chrome.app
335 ;; Grab the frontmost url from Google Chrome. Same limitations as
336 ;; Firefox because Chrome doesn't publish an Applescript dictionary
338 (defun org-as-mac-chrome-get-frontmost-url ()
339 (let ((result (do-applescript
340 (concat
341 "set oldClipboard to the clipboard\n"
342 "set frontmostApplication to path to frontmost application\n"
343 "tell application \"Google Chrome\"\n"
344 " activate\n"
345 " delay 0.15\n"
346 " tell application \"System Events\"\n"
347 " keystroke \"l\" using command down\n"
348 " keystroke \"c\" using command down\n"
349 " end tell\n"
350 " delay 0.15\n"
351 " set theUrl to the clipboard\n"
352 " set the clipboard to oldClipboard\n"
353 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
354 "end tell\n"
355 "activate application (frontmostApplication as text)\n"
356 "set links to {}\n"
357 "copy theResult to the end of links\n"
358 "return links as string\n"))))
359 (car (split-string result "[\r\n]+" t))))
361 (defun org-mac-chrome-get-frontmost-url ()
362 (interactive)
363 (message "Applescript: Getting Chrome url...")
364 (let* ((url-and-title (org-as-mac-chrome-get-frontmost-url))
365 (split-link (split-string url-and-title "::split::"))
366 (URL (car split-link))
367 (description (cadr split-link))
368 (org-link))
369 (when (not (string= URL ""))
370 (setq org-link (org-make-link-string URL description)))
371 (kill-new org-link)
372 org-link))
374 (defun org-mac-chrome-insert-frontmost-url ()
375 (interactive)
376 (insert (org-mac-chrome-get-frontmost-url)))
379 ;; Handle links from Safari.app
380 ;; Grab the frontmost url from Safari.
382 (defun org-as-mac-safari-get-frontmost-url ()
383 (let ((result (do-applescript
384 (concat
385 "tell application \"Safari\"\n"
386 " set theUrl to URL of document 1\n"
387 " set theName to the name of the document 1\n"
388 " return theUrl & \"::split::\" & theName & \"\n\"\n"
389 "end tell\n"))))
390 (car (split-string result "[\r\n]+" t))))
392 (defun org-mac-safari-get-frontmost-url ()
393 (interactive)
394 (message "Applescript: Getting Safari url...")
395 (let* ((url-and-title (org-as-mac-safari-get-frontmost-url))
396 (split-link (split-string url-and-title "::split::"))
397 (URL (car split-link))
398 (description (cadr split-link))
399 (org-link))
400 (when (not (string= URL ""))
401 (setq org-link (org-make-link-string URL description)))
402 (kill-new org-link)
403 org-link))
405 (defun org-mac-safari-insert-frontmost-url ()
406 (interactive)
407 (insert (org-mac-safari-get-frontmost-url)))
412 ;; Handle links from together.app
416 (org-add-link-type "x-together-item" 'org-mac-together-item-open)
418 (defun org-mac-together-item-open (uid)
419 "Open the given uid, which is a reference to an item in Together"
420 (shell-command (concat "open -a Together \"x-together-item:" uid "\"")))
422 (defun as-get-selected-together-items ()
423 (do-applescript
424 (concat
425 "tell application \"Together\"\n"
426 " set theLinkList to {}\n"
427 " set theSelection to selected items\n"
428 " repeat with theItem in theSelection\n"
429 " set theLink to (get item link of theItem) & \"::split::\" & (get name of theItem) & \"\n\"\n"
430 " copy theLink to end of theLinkList\n"
431 " end repeat\n"
432 " return theLinkList as string\n"
433 "end tell")))
435 (defun org-mac-together-get-selected ()
436 (interactive)
437 (message "Applescript: Getting Togther items...")
438 (org-mac-paste-applescript-links (as-get-selected-together-items)))
440 (defun org-mac-together-insert-selected ()
441 (interactive)
442 (insert (org-mac-together-get-selected)))
447 ;; Handle links from Finder.app
451 (defun as-get-selected-finder-items ()
452 (do-applescript
453 (concat
454 "tell application \"Finder\"\n"
455 " set theSelection to the selection\n"
456 " set links to {}\n"
457 " repeat with theItem in theSelection\n"
458 " set theLink to \"file://\" & (POSIX path of (theItem as string)) & \"::split::\" & (get the name of theItem) & \"\n\"\n"
459 " copy theLink to the end of links\n"
460 " end repeat\n"
461 " return links as string\n"
462 "end tell\n")))
464 (defun org-mac-finder-item-get-selected ()
465 (interactive)
466 (message "Applescript: Getting Finder items...")
467 (org-mac-paste-applescript-links (as-get-selected-finder-items)))
469 (defun org-mac-finder-insert-selected ()
470 (interactive)
471 (insert (org-mac-finder-item-get-selected)))
476 ;; Handle links from AddressBook.app
480 (org-add-link-type "addressbook" 'org-mac-addressbook-item-open)
482 (defun org-mac-addressbook-item-open (uid)
483 "Open the given uid, which is a reference to an item in Together"
484 (shell-command (concat "open \"addressbook:" uid "\"")))
486 (defun as-get-selected-addressbook-items ()
487 (do-applescript
488 (concat
489 "tell application \"Address Book\"\n"
490 " set theSelection to the selection\n"
491 " set links to {}\n"
492 " repeat with theItem in theSelection\n"
493 " set theLink to \"addressbook://\" & (the id of theItem) & \"::split::\" & (the name of theItem) & \"\n\"\n"
494 " copy theLink to the end of links\n"
495 " end repeat\n"
496 " return links as string\n"
497 "end tell\n")))
499 (defun org-mac-addressbook-item-get-selected ()
500 (interactive)
501 (message "Applescript: Getting Address Book items...")
502 (org-mac-paste-applescript-links (as-get-selected-addressbook-items)))
504 (defun org-mac-addressbook-insert-selected ()
505 (interactive)
506 (insert (org-mac-addressbook-item-get-selected)))
510 ;; Handle links from Skim.app
512 ;; Original code & idea by Christopher Suckling (org-mac-protocol)
514 (org-add-link-type "skim" 'org-mac-skim-open)
516 (defun org-mac-skim-open (uri)
517 "Visit page of pdf in Skim"
518 (let* ((page (when (string-match "::\\(.+\\)\\'" uri)
519 (match-string 1 uri)))
520 (document (substring uri 0 (match-beginning 0))))
521 (do-applescript
522 (concat
523 "tell application \"Skim\"\n"
524 "activate\n"
525 "set theDoc to \"" document "\"\n"
526 "set thePage to " page "\n"
527 "open theDoc\n"
528 "go document 1 to page thePage of document 1\n"
529 "end tell"))))
532 (defun as-get-skim-page-link ()
533 (do-applescript
534 (concat
535 "tell application \"Skim\"\n"
536 "set theDoc to front document\n"
537 "set theTitle to (name of theDoc)\n"
538 "set thePath to (path of theDoc)\n"
539 "set thePage to (get index for current page of theDoc)\n"
540 "set theSelection to selection of theDoc\n"
541 "set theContent to contents of (get text for theSelection)\n"
542 "if theContent is missing value then\n"
543 " set theContent to theTitle & \", p. \" & thePage\n"
544 (when org-mac-Skim-highlight-selection-p
545 (concat
546 "else\n"
547 " tell theDoc\n"
548 " set theNote to make note with properties {type:highlight note, selection:theSelection}\n"
549 " set text of theNote to (get text for theSelection)\n"
550 " end tell\n"))
551 "end if\n"
552 "set theLink to \"skim://\" & thePath & \"::\" & thePage & "
553 "\"::split::\" & theContent\n"
554 "end tell\n"
555 "return theLink as string\n")))
557 (defun org-mac-skim-get-page ()
558 (interactive)
559 (message "Applescript: Getting Skim page link...")
560 (let* ((link-and-descr (as-get-skim-page-link))
561 (split-link (split-string link-and-descr "::split::"))
562 (link (car split-link))
563 (description (cadr split-link))
564 (org-link))
565 (when (not (string= link ""))
566 (setq org-link (org-make-link-string link description)))
567 (kill-new org-link)
568 org-link))
570 (defun org-mac-skim-insert-page ()
571 (interactive)
572 (insert (org-mac-skim-get-page)))
578 ;; Handle links from Mail.app
581 (org-add-link-type "message" 'org-mac-message-open)
583 (defun org-mac-message-open (message-id)
584 "Visit the message with the given MESSAGE-ID.
585 This will use the command `open' with the message URL."
586 (start-process (concat "open message:" message-id) nil
587 "open" (concat "message://<" (substring message-id 2) ">")))
589 (defun org-as-get-selected-mail ()
590 "AppleScript to create links to selected messages in Mail.app."
591 (do-applescript
592 (concat
593 "tell application \"Mail\"\n"
594 "set theLinkList to {}\n"
595 "set theSelection to selection\n"
596 "repeat with theMessage in theSelection\n"
597 "set theID to message id of theMessage\n"
598 "set theSubject to subject of theMessage\n"
599 "set theLink to \"message://\" & theID & \"::split::\" & theSubject & \"\n\"\n"
600 "copy theLink to end of theLinkList\n"
601 "end repeat\n"
602 "return theLinkList as string\n"
603 "end tell")))
605 (defun org-as-get-flagged-mail ()
606 "AppleScript to create links to flagged messages in Mail.app."
607 (do-applescript
608 (concat
609 ;; Is Growl installed?
610 "tell application \"System Events\"\n"
611 "set growlHelpers to the name of every process whose creator type contains \"GRRR\"\n"
612 "if (count of growlHelpers) > 0 then\n"
613 "set growlHelperApp to item 1 of growlHelpers\n"
614 "else\n"
615 "set growlHelperApp to \"\"\n"
616 "end if\n"
617 "end tell\n"
619 ;; Get links
620 "tell application \"Mail\"\n"
621 "set theMailboxes to every mailbox of account \"" org-mac-mail-account "\"\n"
622 "set theLinkList to {}\n"
623 "repeat with aMailbox in theMailboxes\n"
624 "set theSelection to (every message in aMailbox whose flagged status = true)\n"
625 "repeat with theMessage in theSelection\n"
626 "set theID to message id of theMessage\n"
627 "set theSubject to subject of theMessage\n"
628 "set theLink to \"message://\" & theID & \"::split::\" & theSubject & \"\n\"\n"
629 "copy theLink to end of theLinkList\n"
631 ;; Report progress through Growl
632 ;; This "double tell" idiom is described in detail at
633 ;; http://macscripter.net/viewtopic.php?id=24570 The
634 ;; script compiler needs static knowledge of the
635 ;; growlHelperApp. Hmm, since we're compiling
636 ;; on-the-fly here, this is likely to be way less
637 ;; portable than I'd hoped. It'll work when the name
638 ;; is still "GrowlHelperApp", though.
639 "if growlHelperApp is not \"\" then\n"
640 "tell application \"GrowlHelperApp\"\n"
641 "tell application growlHelperApp\n"
642 "set the allNotificationsList to {\"FlaggedMail\"}\n"
643 "set the enabledNotificationsList to allNotificationsList\n"
644 "register as application \"FlaggedMail\" all notifications allNotificationsList default notifications enabledNotificationsList icon of application \"Mail\"\n"
645 "notify with name \"FlaggedMail\" title \"Importing flagged message\" description theSubject application name \"FlaggedMail\"\n"
646 "end tell\n"
647 "end tell\n"
648 "end if\n"
649 "end repeat\n"
650 "end repeat\n"
651 "return theLinkList as string\n"
652 "end tell")))
654 (defun org-mac-message-get-links (&optional select-or-flag)
655 "Create links to the messages currently selected or flagged in Mail.app.
656 This will use AppleScript to get the message-id and the subject of the
657 messages in Mail.app and make a link out of it.
658 When SELECT-OR-FLAG is \"s\", get the selected messages (this is also
659 the default). When SELECT-OR-FLAG is \"f\", get the flagged messages.
660 The Org-syntax text will be pushed to the kill ring, and also returned."
661 (interactive "sLink to (s)elected or (f)lagged messages: ")
662 (setq select-or-flag (or select-or-flag "s"))
663 (message "AppleScript: searching mailboxes...")
664 (let* ((as-link-list
665 (if (string= select-or-flag "s")
666 (org-as-get-selected-mail)
667 (if (string= select-or-flag "f")
668 (org-as-get-flagged-mail)
669 (error "Please select \"s\" or \"f\""))))
670 (link-list
671 (mapcar
672 (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x) (setq x (match-string 1 x))) x)
673 (split-string as-link-list "[\r\n]+")))
674 split-link URL description orglink orglink-insert rtn orglink-list)
675 (while link-list
676 (setq split-link (split-string (pop link-list) "::split::"))
677 (setq URL (car split-link))
678 (setq description (cadr split-link))
679 (when (not (string= URL ""))
680 (setq orglink (org-make-link-string URL description))
681 (push orglink orglink-list)))
682 (setq rtn (mapconcat 'identity orglink-list "\n"))
683 (kill-new rtn)
684 rtn))
686 (defun org-mac-message-insert-selected ()
687 "Insert a link to the messages currently selected in Mail.app.
688 This will use AppleScript to get the message-id and the subject of the
689 active mail in Mail.app and make a link out of it."
690 (interactive)
691 (insert (org-mac-message-get-links "s")))
693 ;; The following line is for backward compatibility
694 (defalias 'org-mac-message-insert-link 'org-mac-message-insert-selected)
696 (defun org-mac-message-insert-flagged (org-buffer org-heading)
697 "Asks for an org buffer and a heading within it, and replace message links.
698 If heading exists, delete all message:// links within heading's first
699 level. If heading doesn't exist, create it at point-max. Insert
700 list of message:// links to flagged mail after heading."
701 (interactive "bBuffer in which to insert links: \nsHeading after which to insert links: ")
702 (with-current-buffer org-buffer
703 (goto-char (point-min))
704 (let ((isearch-forward t)
705 (message-re "\\[\\[\\(message:\\)\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"))
706 (if (org-goto-local-search-headings org-heading nil t)
707 (if (not (eobp))
708 (progn
709 (save-excursion
710 (while (re-search-forward
711 message-re (save-excursion (outline-next-heading)) t)
712 (delete-region (match-beginning 0) (match-end 0)))
713 (insert "\n" (org-mac-message-get-links "f")))
714 (flush-lines "^$" (point) (outline-next-heading)))
715 (insert "\n" (org-mac-message-get-links "f")))
716 (goto-char (point-max))
717 (insert "\n")
718 (org-insert-heading nil t)
719 (insert org-heading "\n" (org-mac-message-get-links "f"))))))
722 (provide 'org-mac-link)
724 ;;; org-mac-link.el ends here