59b88d2a5cc0e49813398c25ce14e6059d1a4b74
[org-mode.git] / contrib / lisp / org-mac-link.el
blob59b88d2a5cc0e49813398c25ce14e6059d1a4b74
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)
82 (defgroup org-mac-link nil
83 "Options concerning grabbing links from external Mac
84 applications and inserting them in org documents"
85 :tag "Org Mac link"
86 :group 'org-link)
88 (defcustom org-mac-grab-Finder-app-p t
89 "Enable menu option [F]inder to grab links from the Finder"
90 :tag "Grab Finder.app links"
91 :group 'org-mac-link
92 :type 'boolean)
94 (defcustom org-mac-grab-Mail-app-p t
95 "Enable menu option [m]ail to grab links from Mail.app"
96 :tag "Grab Mail.app links"
97 :group 'org-mac-link
98 :type 'boolean)
100 (defcustom org-mac-grab-Addressbook-app-p t
101 "Enable menu option [a]ddressbook to grab links from AddressBook.app"
102 :tag "Grab AddressBook.app links"
103 :group 'org-mac-link
104 :type 'boolean)
106 (defcustom org-mac-grab-Safari-app-p t
107 "Enable menu option [s]afari to grab links from Safari.app"
108 :tag "Grab Safari.app links"
109 :group 'org-mac-link
110 :type 'boolean)
112 (defcustom org-mac-grab-Firefox-app-p t
113 "Enable menu option [f]irefox to grab links from Firefox.app"
114 :tag "Grab Firefox.app links"
115 :group 'org-mac-link
116 :type 'boolean)
118 (defcustom org-mac-grab-Firefox+Vimperator-p nil
119 "Enable menu option [v]imperator to grab links from Firefox.app running the Vimperator plugin"
120 :tag "Grab Vimperator/Firefox.app links"
121 :group 'org-mac-link
122 :type 'boolean)
124 (defcustom org-mac-grab-Chrome-app-p t
125 "Enable menu option [f]irefox to grab links from Google Chrome.app"
126 :tag "Grab Google Chrome.app links"
127 :group 'org-mac-link
128 :type 'boolean)
130 (defcustom org-mac-grab-Together-app-p nil
131 "Enable menu option [t]ogether to grab links from Together.app"
132 :tag "Grab Together.app links"
133 :group 'org-mac-link
134 :type 'boolean)
136 (defcustom org-mac-grab-Skim-app-p
137 (< 0 (length (shell-command-to-string
138 "mdfind kMDItemCFBundleIdentifier == 'net.sourceforge.skim-app.skim'")))
139 "Enable menu option [S]kim to grab page links from Skim.app"
140 :tag "Grab Skim.app page links"
141 :group 'org-mac-link
142 :type 'boolean)
144 (defcustom org-mac-Skim-highlight-selection-p nil
145 "Highlight (using notes) the selection (if present) when grabbing the a link from Skim.app"
146 :tag "Highlight selection in Skim.app"
147 :group 'org-mac-link
148 :type 'boolean)
150 (defgroup org-mac-flagged-mail nil
151 "Options concerning linking to flagged Mail.app messages."
152 :tag "Org Mail.app"
153 :group 'org-link)
155 (defcustom org-mac-mail-account "customize"
156 "The Mail.app account in which to search for flagged messages."
157 :group 'org-mac-flagged-mail
158 :type 'string)
161 ;; In mac.c, removed in Emacs 23.
162 (declare-function do-applescript "org-mac-message" (script))
163 (unless (fboundp 'do-applescript)
164 ;; Need to fake this using shell-command-to-string
165 (defun do-applescript (script)
166 (let (start cmd return)
167 (while (string-match "\n" script)
168 (setq script (replace-match "\r" t t script)))
169 (while (string-match "'" script start)
170 (setq start (+ 2 (match-beginning 0))
171 script (replace-match "\\'" t t script)))
172 (setq cmd (concat "osascript -e '" script "'"))
173 (setq return (shell-command-to-string cmd))
174 (concat "\"" (org-trim return) "\""))))
177 (defun org-mac-grab-link ()
178 "Prompt the user for an application to grab a link from, then go grab the link, and insert it at point"
179 (interactive)
180 (let* ((descriptors `(("F" "inder" org-mac-finder-insert-selected ,org-mac-grab-Finder-app-p)
181 ("m" "ail" org-mac-message-insert-selected ,org-mac-grab-Mail-app-p)
182 ("a" "ddressbook" org-mac-addressbook-insert-selected ,org-mac-grab-Addressbook-app-p)
183 ("s" "afari" org-mac-safari-insert-frontmost-url ,org-mac-grab-Safari-app-p)
184 ("f" "irefox" org-mac-firefox-insert-frontmost-url ,org-mac-grab-Firefox-app-p)
185 ("v" "imperator" org-mac-vimperator-insert-frontmost-url ,org-mac-grab-Firefox+Vimperator-p)
186 ("c" "hrome" org-mac-chrome-insert-frontmost-url ,org-mac-grab-Chrome-app-p)
187 ("t" "ogether" org-mac-together-insert-selected ,org-mac-grab-Together-app-p)
188 ("S" "kim" org-mac-skim-insert-page ,org-mac-grab-Skim-app-p)))
189 (menu-string (make-string 0 ?x))
190 input)
192 ;; Create the menu string for the keymap
193 (mapc '(lambda (descriptor)
194 (when (elt descriptor 3)
195 (setf menu-string (concat menu-string "[" (elt descriptor 0) "]" (elt descriptor 1) " "))))
196 descriptors)
197 (setf (elt menu-string (- (length menu-string) 1)) ?:)
199 ;; Prompt the user, and grab the link
200 (message menu-string)
201 (setq input (read-char-exclusive))
202 (mapc '(lambda (descriptor)
203 (let ((key (elt (elt descriptor 0) 0))
204 (active (elt descriptor 3))
205 (grab-function (elt descriptor 2)))
206 (when (and active (eq input key))
207 (call-interactively grab-function))))
208 descriptors)))
210 (defun org-mac-paste-applescript-links (as-link-list)
211 "Paste in a list of links from an applescript handler. The
212 links are of the form <link>::split::<name>"
213 (let* ((link-list
214 (mapcar
215 (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x) (setq x (match-string 1 x))) x)
216 (split-string as-link-list "[\r\n]+")))
217 split-link URL description orglink orglink-insert rtn orglink-list)
218 (while link-list
219 (setq split-link (split-string (pop link-list) "::split::"))
220 (setq URL (car split-link))
221 (setq description (cadr split-link))
222 (when (not (string= URL ""))
223 (setq orglink (org-make-link-string URL description))
224 (push orglink orglink-list)))
225 (setq rtn (mapconcat 'identity orglink-list "\n"))
226 (kill-new rtn)
227 rtn))
231 ;; Handle links from Firefox.app
233 ;; This code allows you to grab the current active url from the main
234 ;; Firefox.app window, and insert it as a link into an org-mode
235 ;; document. Unfortunately, firefox does not expose an applescript
236 ;; dictionary, so this is necessarily introduces some limitations.
238 ;; The applescript to grab the url from Firefox.app uses the System
239 ;; Events application to give focus to the firefox application, select
240 ;; the contents of the url bar, and copy it. It then uses the title of
241 ;; the window as the text of the link. There is no way to grab links
242 ;; from other open tabs, and further, if there is more than one window
243 ;; open, it is not clear which one will be used (though emperically it
244 ;; seems that it is always the last active window).
246 (defun org-as-mac-firefox-get-frontmost-url ()
247 (let ((result (do-applescript
248 (concat
249 "set oldClipboard to the clipboard\n"
250 "set frontmostApplication to path to frontmost application\n"
251 "tell application \"Firefox\"\n"
252 " activate\n"
253 " delay 0.15\n"
254 " tell application \"System Events\"\n"
255 " keystroke \"l\" using {command down}\n"
256 " keystroke \"a\" using {command down}\n"
257 " keystroke \"c\" using {command down}\n"
258 " end tell\n"
259 " delay 0.15\n"
260 " set theUrl to the clipboard\n"
261 " set the clipboard to oldClipboard\n"
262 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
263 "end tell\n"
264 "activate application (frontmostApplication as text)\n"
265 "set links to {}\n"
266 "copy theResult to the end of links\n"
267 "return links as string\n"))))
268 (car (split-string result "[\r\n]+" t))))
270 (defun org-mac-firefox-get-frontmost-url ()
271 (interactive)
272 (message "Applescript: Getting Firefox url...")
273 (let* ((url-and-title (org-as-mac-firefox-get-frontmost-url))
274 (split-link (split-string url-and-title "::split::"))
275 (URL (car split-link))
276 (description (cadr split-link))
277 (org-link))
278 (when (not (string= URL ""))
279 (setq org-link (org-make-link-string URL description)))
280 (kill-new org-link)
281 org-link))
283 (defun org-mac-firefox-insert-frontmost-url ()
284 (interactive)
285 (insert (org-mac-firefox-get-frontmost-url)))
288 ;; Handle links from Google Firefox.app running the Vimperator extension
289 ;; Grab the frontmost url from Firefox+Vimperator. Same limitations are
290 ;; Firefox
292 (defun org-as-mac-vimperator-get-frontmost-url ()
293 (let ((result (do-applescript
294 (concat
295 "set oldClipboard to the clipboard\n"
296 "set frontmostApplication to path to frontmost application\n"
297 "tell application \"Firefox\"\n"
298 " activate\n"
299 " delay 0.15\n"
300 " tell application \"System Events\"\n"
301 " keystroke \"y\"\n"
302 " end tell\n"
303 " delay 0.15\n"
304 " set theUrl to the clipboard\n"
305 " set the clipboard to oldClipboard\n"
306 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
307 "end tell\n"
308 "activate application (frontmostApplication as text)\n"
309 "set links to {}\n"
310 "copy theResult to the end of links\n"
311 "return links as string\n"))))
312 (replace-regexp-in-string "\s+-\s+Vimperator" "" (car (split-string result "[\r\n]+" t)))))
315 (defun org-mac-vimperator-get-frontmost-url ()
316 (interactive)
317 (message "Applescript: Getting Vimperator url...")
318 (let* ((url-and-title (org-as-mac-vimperator-get-frontmost-url))
319 (split-link (split-string url-and-title "::split::"))
320 (URL (car split-link))
321 (description (cadr split-link))
322 (org-link))
323 (when (not (string= URL ""))
324 (setq org-link (org-make-link-string URL description)))
325 (kill-new org-link)
326 org-link))
328 (defun org-mac-vimperator-insert-frontmost-url ()
329 (interactive)
330 (insert (org-mac-vimperator-get-frontmost-url)))
333 ;; Handle links from Google Chrome.app
334 ;; Grab the frontmost url from Google Chrome. Same limitations as
335 ;; Firefox because Chrome doesn't publish an Applescript dictionary
337 (defun org-as-mac-chrome-get-frontmost-url ()
338 (let ((result (do-applescript
339 (concat
340 "set oldClipboard to the clipboard\n"
341 "set frontmostApplication to path to frontmost application\n"
342 "tell application \"Google Chrome\"\n"
343 " activate\n"
344 " delay 0.15\n"
345 " tell application \"System Events\"\n"
346 " keystroke \"l\" using command down\n"
347 " keystroke \"c\" using command down\n"
348 " end tell\n"
349 " delay 0.15\n"
350 " set theUrl to the clipboard\n"
351 " set the clipboard to oldClipboard\n"
352 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
353 "end tell\n"
354 "activate application (frontmostApplication as text)\n"
355 "set links to {}\n"
356 "copy theResult to the end of links\n"
357 "return links as string\n"))))
358 (car (split-string result "[\r\n]+" t))))
360 (defun org-mac-chrome-get-frontmost-url ()
361 (interactive)
362 (message "Applescript: Getting Chrome url...")
363 (let* ((url-and-title (org-as-mac-chrome-get-frontmost-url))
364 (split-link (split-string url-and-title "::split::"))
365 (URL (car split-link))
366 (description (cadr split-link))
367 (org-link))
368 (when (not (string= URL ""))
369 (setq org-link (org-make-link-string URL description)))
370 (kill-new org-link)
371 org-link))
373 (defun org-mac-chrome-insert-frontmost-url ()
374 (interactive)
375 (insert (org-mac-chrome-get-frontmost-url)))
378 ;; Handle links from Safari.app
379 ;; Grab the frontmost url from Safari.
381 (defun org-as-mac-safari-get-frontmost-url ()
382 (let ((result (do-applescript
383 (concat
384 "tell application \"Safari\"\n"
385 " set theUrl to URL of document 1\n"
386 " set theName to the name of the document 1\n"
387 " return theUrl & \"::split::\" & theName & \"\n\"\n"
388 "end tell\n"))))
389 (car (split-string result "[\r\n]+" t))))
391 (defun org-mac-safari-get-frontmost-url ()
392 (interactive)
393 (message "Applescript: Getting Safari url...")
394 (let* ((url-and-title (org-as-mac-safari-get-frontmost-url))
395 (split-link (split-string url-and-title "::split::"))
396 (URL (car split-link))
397 (description (cadr split-link))
398 (org-link))
399 (when (not (string= URL ""))
400 (setq org-link (org-make-link-string URL description)))
401 (kill-new org-link)
402 org-link))
404 (defun org-mac-safari-insert-frontmost-url ()
405 (interactive)
406 (insert (org-mac-safari-get-frontmost-url)))
411 ;; Handle links from together.app
415 (org-add-link-type "x-together-item" 'org-mac-together-item-open)
417 (defun org-mac-together-item-open (uid)
418 "Open the given uid, which is a reference to an item in Together"
419 (shell-command (concat "open -a Together \"x-together-item:" uid "\"")))
421 (defun as-get-selected-together-items ()
422 (do-applescript
423 (concat
424 "tell application \"Together\"\n"
425 " set theLinkList to {}\n"
426 " set theSelection to selected items\n"
427 " repeat with theItem in theSelection\n"
428 " set theLink to (get item link of theItem) & \"::split::\" & (get name of theItem) & \"\n\"\n"
429 " copy theLink to end of theLinkList\n"
430 " end repeat\n"
431 " return theLinkList as string\n"
432 "end tell")))
434 (defun org-mac-together-get-selected ()
435 (interactive)
436 (message "Applescript: Getting Togther items...")
437 (org-mac-paste-applescript-links (as-get-selected-together-items)))
439 (defun org-mac-together-insert-selected ()
440 (interactive)
441 (insert (org-mac-together-get-selected)))
446 ;; Handle links from Finder.app
450 (defun as-get-selected-finder-items ()
451 (do-applescript
452 (concat
453 "tell application \"Finder\"\n"
454 " set theSelection to the selection\n"
455 " set links to {}\n"
456 " repeat with theItem in theSelection\n"
457 " set theLink to \"file://\" & (POSIX path of (theItem as string)) & \"::split::\" & (get the name of theItem) & \"\n\"\n"
458 " copy theLink to the end of links\n"
459 " end repeat\n"
460 " return links as string\n"
461 "end tell\n")))
463 (defun org-mac-finder-item-get-selected ()
464 (interactive)
465 (message "Applescript: Getting Finder items...")
466 (org-mac-paste-applescript-links (as-get-selected-finder-items)))
468 (defun org-mac-finder-insert-selected ()
469 (interactive)
470 (insert (org-mac-finder-item-get-selected)))
475 ;; Handle links from AddressBook.app
479 (org-add-link-type "addressbook" 'org-mac-addressbook-item-open)
481 (defun org-mac-addressbook-item-open (uid)
482 "Open the given uid, which is a reference to an item in Together"
483 (shell-command (concat "open \"addressbook:" uid "\"")))
485 (defun as-get-selected-addressbook-items ()
486 (do-applescript
487 (concat
488 "tell application \"Address Book\"\n"
489 " set theSelection to the selection\n"
490 " set links to {}\n"
491 " repeat with theItem in theSelection\n"
492 " set theLink to \"addressbook://\" & (the id of theItem) & \"::split::\" & (the name of theItem) & \"\n\"\n"
493 " copy theLink to the end of links\n"
494 " end repeat\n"
495 " return links as string\n"
496 "end tell\n")))
498 (defun org-mac-addressbook-item-get-selected ()
499 (interactive)
500 (message "Applescript: Getting Address Book items...")
501 (org-mac-paste-applescript-links (as-get-selected-addressbook-items)))
503 (defun org-mac-addressbook-insert-selected ()
504 (interactive)
505 (insert (org-mac-addressbook-item-get-selected)))
509 ;; Handle links from Skim.app
511 ;; Original code & idea by Christopher Suckling (org-mac-protocol)
513 (org-add-link-type "skim" 'org-mac-skim-open)
515 (defun org-mac-skim-open (uri)
516 "Visit page of pdf in Skim"
517 (let* ((page (when (string-match "::\\(.+\\)\\'" uri)
518 (match-string 1 uri)))
519 (document (substring uri 0 (match-beginning 0))))
520 (do-applescript
521 (concat
522 "tell application \"Skim\"\n"
523 "activate\n"
524 "set theDoc to \"" document "\"\n"
525 "set thePage to " page "\n"
526 "open theDoc\n"
527 "go document 1 to page thePage of document 1\n"
528 "end tell"))))
531 (defun as-get-skim-page-link ()
532 (do-applescript
533 (concat
534 "tell application \"Skim\"\n"
535 "set theDoc to front document\n"
536 "set theTitle to (name of theDoc)\n"
537 "set thePath to (path of theDoc)\n"
538 "set thePage to (get index for current page of theDoc)\n"
539 "set theSelection to selection of theDoc\n"
540 "set theContent to contents of (get text for theSelection)\n"
541 "if theContent is missing value then\n"
542 " set theContent to theTitle & \", p. \" & thePage\n"
543 (when org-mac-Skim-highlight-selection-p
544 (concat
545 "else\n"
546 " tell theDoc\n"
547 " set theNote to make note with properties {type:highlight note, selection:theSelection}\n"
548 " set text of theNote to (get text for theSelection)\n"
549 " end tell\n"))
550 "end if\n"
551 "set theLink to \"skim://\" & thePath & \"::\" & thePage & "
552 "\"::split::\" & theContent\n"
553 "end tell\n"
554 "return theLink as string\n")))
556 (defun org-mac-skim-get-page ()
557 (interactive)
558 (message "Applescript: Getting Skim page link...")
559 (let* ((link-and-descr (as-get-skim-page-link))
560 (split-link (split-string link-and-descr "::split::"))
561 (link (car split-link))
562 (description (cadr split-link))
563 (org-link))
564 (when (not (string= link ""))
565 (setq org-link (org-make-link-string link description)))
566 (kill-new org-link)
567 org-link))
569 (defun org-mac-skim-insert-page ()
570 (interactive)
571 (insert (org-mac-skim-get-page)))
577 ;; Handle links from Mail.app
580 (org-add-link-type "message" 'org-mac-message-open)
582 (defun org-mac-message-open (message-id)
583 "Visit the message with the given MESSAGE-ID.
584 This will use the command `open' with the message URL."
585 (start-process (concat "open message:" message-id) nil
586 "open" (concat "message://<" (substring message-id 2) ">")))
588 (defun org-as-get-selected-mail ()
589 "AppleScript to create links to selected messages in Mail.app."
590 (do-applescript
591 (concat
592 "tell application \"Mail\"\n"
593 "set theLinkList to {}\n"
594 "set theSelection to selection\n"
595 "repeat with theMessage in theSelection\n"
596 "set theID to message id of theMessage\n"
597 "set theSubject to subject of theMessage\n"
598 "set theLink to \"message://\" & theID & \"::split::\" & theSubject & \"\n\"\n"
599 "copy theLink to end of theLinkList\n"
600 "end repeat\n"
601 "return theLinkList as string\n"
602 "end tell")))
604 (defun org-as-get-flagged-mail ()
605 "AppleScript to create links to flagged messages in Mail.app."
606 (do-applescript
607 (concat
608 ;; Is Growl installed?
609 "tell application \"System Events\"\n"
610 "set growlHelpers to the name of every process whose creator type contains \"GRRR\"\n"
611 "if (count of growlHelpers) > 0 then\n"
612 "set growlHelperApp to item 1 of growlHelpers\n"
613 "else\n"
614 "set growlHelperApp to \"\"\n"
615 "end if\n"
616 "end tell\n"
618 ;; Get links
619 "tell application \"Mail\"\n"
620 "set theMailboxes to every mailbox of account \"" org-mac-mail-account "\"\n"
621 "set theLinkList to {}\n"
622 "repeat with aMailbox in theMailboxes\n"
623 "set theSelection to (every message in aMailbox whose flagged status = true)\n"
624 "repeat with theMessage in theSelection\n"
625 "set theID to message id of theMessage\n"
626 "set theSubject to subject of theMessage\n"
627 "set theLink to \"message://\" & theID & \"::split::\" & theSubject & \"\n\"\n"
628 "copy theLink to end of theLinkList\n"
630 ;; Report progress through Growl
631 ;; This "double tell" idiom is described in detail at
632 ;; http://macscripter.net/viewtopic.php?id=24570 The
633 ;; script compiler needs static knowledge of the
634 ;; growlHelperApp. Hmm, since we're compiling
635 ;; on-the-fly here, this is likely to be way less
636 ;; portable than I'd hoped. It'll work when the name
637 ;; is still "GrowlHelperApp", though.
638 "if growlHelperApp is not \"\" then\n"
639 "tell application \"GrowlHelperApp\"\n"
640 "tell application growlHelperApp\n"
641 "set the allNotificationsList to {\"FlaggedMail\"}\n"
642 "set the enabledNotificationsList to allNotificationsList\n"
643 "register as application \"FlaggedMail\" all notifications allNotificationsList default notifications enabledNotificationsList icon of application \"Mail\"\n"
644 "notify with name \"FlaggedMail\" title \"Importing flagged message\" description theSubject application name \"FlaggedMail\"\n"
645 "end tell\n"
646 "end tell\n"
647 "end if\n"
648 "end repeat\n"
649 "end repeat\n"
650 "return theLinkList as string\n"
651 "end tell")))
653 (defun org-mac-message-get-links (&optional select-or-flag)
654 "Create links to the messages currently selected or flagged in Mail.app.
655 This will use AppleScript to get the message-id and the subject of the
656 messages in Mail.app and make a link out of it.
657 When SELECT-OR-FLAG is \"s\", get the selected messages (this is also
658 the default). When SELECT-OR-FLAG is \"f\", get the flagged messages.
659 The Org-syntax text will be pushed to the kill ring, and also returned."
660 (interactive "sLink to (s)elected or (f)lagged messages: ")
661 (setq select-or-flag (or select-or-flag "s"))
662 (message "AppleScript: searching mailboxes...")
663 (let* ((as-link-list
664 (if (string= select-or-flag "s")
665 (org-as-get-selected-mail)
666 (if (string= select-or-flag "f")
667 (org-as-get-flagged-mail)
668 (error "Please select \"s\" or \"f\""))))
669 (link-list
670 (mapcar
671 (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x) (setq x (match-string 1 x))) x)
672 (split-string as-link-list "[\r\n]+")))
673 split-link URL description orglink orglink-insert rtn orglink-list)
674 (while link-list
675 (setq split-link (split-string (pop link-list) "::split::"))
676 (setq URL (car split-link))
677 (setq description (cadr split-link))
678 (when (not (string= URL ""))
679 (setq orglink (org-make-link-string URL description))
680 (push orglink orglink-list)))
681 (setq rtn (mapconcat 'identity orglink-list "\n"))
682 (kill-new rtn)
683 rtn))
685 (defun org-mac-message-insert-selected ()
686 "Insert a link to the messages currently selected in Mail.app.
687 This will use AppleScript to get the message-id and the subject of the
688 active mail in Mail.app and make a link out of it."
689 (interactive)
690 (insert (org-mac-message-get-links "s")))
692 ;; The following line is for backward compatibility
693 (defalias 'org-mac-message-insert-link 'org-mac-message-insert-selected)
695 (defun org-mac-message-insert-flagged (org-buffer org-heading)
696 "Asks for an org buffer and a heading within it, and replace message links.
697 If heading exists, delete all message:// links within heading's first
698 level. If heading doesn't exist, create it at point-max. Insert
699 list of message:// links to flagged mail after heading."
700 (interactive "bBuffer in which to insert links: \nsHeading after which to insert links: ")
701 (with-current-buffer org-buffer
702 (goto-char (point-min))
703 (let ((isearch-forward t)
704 (message-re "\\[\\[\\(message:\\)\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"))
705 (if (org-goto-local-search-headings org-heading nil t)
706 (if (not (eobp))
707 (progn
708 (save-excursion
709 (while (re-search-forward
710 message-re (save-excursion (outline-next-heading)) t)
711 (delete-region (match-beginning 0) (match-end 0)))
712 (insert "\n" (org-mac-message-get-links "f")))
713 (flush-lines "^$" (point) (outline-next-heading)))
714 (insert "\n" (org-mac-message-get-links "f")))
715 (goto-char (point-max))
716 (insert "\n")
717 (org-insert-heading nil t)
718 (insert org-heading "\n" (org-mac-message-get-links "f"))))))
721 (provide 'org-mac-link)
723 ;;; org-mac-link.el ends here