re-apply an orphaned anonymous lambda function
[org-mode.git] / contrib / lisp / org-mac-link.el
blob2ff6711f9a3b8b79952b82925dd1753d492f90e2
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 ;; Version: 1.2
17 ;; Keywords: outlook
18 ;; Author: Mike McLean <mike.mclean@pobox.com>
19 ;; Add support for Microsoft Outlook for Mac as Org mode links
21 ;; This file is not part of GNU Emacs.
23 ;; This program is free software; you can redistribute it and/or modify
24 ;; it under the terms of the GNU General Public License as published by
25 ;; the Free Software Foundation; either version 3, or (at your option)
26 ;; any later version.
28 ;; This program is distributed in the hope that it will be useful,
29 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
30 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 ;; GNU General Public License for more details.
33 ;; You should have received a copy of the GNU General Public License
34 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
36 ;;; Commentary:
38 ;; This code allows you to grab either the current selected items, or
39 ;; the frontmost url in various mac appliations, and insert them as
40 ;; hyperlinks into the current org-mode document at point.
42 ;; This code is heavily based on, and indeed incorporates,
43 ;; org-mac-message.el written by John Wiegley and Christopher
44 ;; Suckling.
46 ;; Detailed comments for each application interface are inlined with
47 ;; the code. Here is a brief overview of how the code interacts with
48 ;; each application:
50 ;; Finder.app - grab links to the selected files in the frontmost window
51 ;; Mail.app - grab links to the selected messages in the message list
52 ;; AddressBook.app - Grab links to the selected addressbook Cards
53 ;; Firefox.app - Grab the url of the frontmost tab in the frontmost window
54 ;; Vimperator/Firefox.app - Grab the url of the frontmost tab in the frontmost window
55 ;; Safari.app - Grab the url of the frontmost tab in the frontmost window
56 ;; Google Chrome.app - Grab the url of the frontmost tab in the frontmost window
57 ;; Together.app - Grab links to the selected items in the library list
58 ;; Skim.app - Grab a link to the selected page in the topmost pdf document
59 ;; Microsoft Outlook.app - Grab a link to the selected message in the message list
62 ;; Installation:
64 ;; add (require 'org-mac-link) to your .emacs, and optionally bind a
65 ;; key to activate the link grabber menu, like this:
67 ;; (add-hook 'org-mode-hook (lambda ()
68 ;; (define-key org-mode-map (kbd "C-c g") 'org-mac-grab-link)))
70 ;; Usage:
72 ;; Type C-c g (or whatever key you defined, as above), or type M-x
73 ;; org-mac-grab-link RET to activate the link grabber. This will present
74 ;; you with a menu to choose an application from which to grab a link
75 ;; to insert at point. You may also type C-g to abort.
77 ;; Customizing:
79 ;; You may customize which applications appear in the grab menu by
80 ;; customizing the group `org-mac-link'. Changes take effect
81 ;; immediately.
84 ;;; Code:
86 (require 'org)
88 (defgroup org-mac-link nil
89 "Options concerning grabbing links from external Mac
90 applications and inserting them in org documents"
91 :tag "Org Mac link"
92 :group 'org-link)
94 (defcustom org-mac-grab-Finder-app-p t
95 "Enable menu option [F]inder to grab links from the Finder"
96 :tag "Grab Finder.app links"
97 :group 'org-mac-link
98 :type 'boolean)
100 (defcustom org-mac-grab-Mail-app-p t
101 "Enable menu option [m]ail to grab links from Mail.app"
102 :tag "Grab Mail.app links"
103 :group 'org-mac-link
104 :type 'boolean)
106 (defcustom org-mac-grab-Outlook-app-p t
107 "Enable menu option [o]utlook to grab links from Microsoft Outlook.app"
108 :tag "Grab Microsoft Outlook.app links"
109 :group 'org-mac-link
110 :type 'boolean)
112 (defcustom org-mac-grab-Addressbook-app-p t
113 "Enable menu option [a]ddressbook to grab links from AddressBook.app"
114 :tag "Grab AddressBook.app links"
115 :group 'org-mac-link
116 :type 'boolean)
118 (defcustom org-mac-grab-Safari-app-p t
119 "Enable menu option [s]afari to grab links from Safari.app"
120 :tag "Grab Safari.app links"
121 :group 'org-mac-link
122 :type 'boolean)
124 (defcustom org-mac-grab-Firefox-app-p t
125 "Enable menu option [f]irefox to grab links from Firefox.app"
126 :tag "Grab Firefox.app links"
127 :group 'org-mac-link
128 :type 'boolean)
130 (defcustom org-mac-grab-Firefox+Vimperator-p nil
131 "Enable menu option [v]imperator to grab links from Firefox.app running the Vimperator plugin"
132 :tag "Grab Vimperator/Firefox.app links"
133 :group 'org-mac-link
134 :type 'boolean)
136 (defcustom org-mac-grab-Chrome-app-p t
137 "Enable menu option [f]irefox to grab links from Google Chrome.app"
138 :tag "Grab Google Chrome.app links"
139 :group 'org-mac-link
140 :type 'boolean)
142 (defcustom org-mac-grab-Together-app-p nil
143 "Enable menu option [t]ogether to grab links from Together.app"
144 :tag "Grab Together.app links"
145 :group 'org-mac-link
146 :type 'boolean)
148 (defcustom org-mac-grab-Skim-app-p
149 (< 0 (length (shell-command-to-string
150 "mdfind kMDItemCFBundleIdentifier == 'net.sourceforge.skim-app.skim'")))
151 "Enable menu option [S]kim to grab page links from Skim.app"
152 :tag "Grab Skim.app page links"
153 :group 'org-mac-link
154 :type 'boolean)
156 (defcustom org-mac-Skim-highlight-selection-p nil
157 "Highlight (using notes) the selection (if present) when grabbing the a link from Skim.app"
158 :tag "Highlight selection in Skim.app"
159 :group 'org-mac-link
160 :type 'boolean)
162 (defgroup org-mac-flagged-mail nil
163 "Options concerning linking to flagged Mail.app messages."
164 :tag "Org Mail.app"
165 :group 'org-link)
167 (defcustom org-mac-mail-account "customize"
168 "The Mail.app account in which to search for flagged messages."
169 :group 'org-mac-flagged-mail
170 :type 'string)
173 ;; In mac.c, removed in Emacs 23.
174 (declare-function do-applescript "org-mac-message" (script))
175 (unless (fboundp 'do-applescript)
176 ;; Need to fake this using shell-command-to-string
177 (defun do-applescript (script)
178 (let (start cmd return)
179 (while (string-match "\n" script)
180 (setq script (replace-match "\r" t t script)))
181 (while (string-match "'" script start)
182 (setq start (+ 2 (match-beginning 0))
183 script (replace-match "\\'" t t script)))
184 (setq cmd (concat "osascript -e '" script "'"))
185 (setq return (shell-command-to-string cmd))
186 (concat "\"" (org-trim return) "\""))))
189 (defun org-mac-grab-link ()
190 "Prompt the user for an application to grab a link from, then go grab the link, and insert it at point"
191 (interactive)
192 (let* ((descriptors `(("F" "inder" org-mac-finder-insert-selected ,org-mac-grab-Finder-app-p)
193 ("m" "ail" org-mac-message-insert-selected ,org-mac-grab-Mail-app-p)
194 ("o" "utlook" org-mac-outlook-message-insert-selected ,org-mac-grab-Outlook-app-p)
195 ("a" "ddressbook" org-mac-addressbook-insert-selected ,org-mac-grab-Addressbook-app-p)
196 ("s" "afari" org-mac-safari-insert-frontmost-url ,org-mac-grab-Safari-app-p)
197 ("f" "irefox" org-mac-firefox-insert-frontmost-url ,org-mac-grab-Firefox-app-p)
198 ("v" "imperator" org-mac-vimperator-insert-frontmost-url ,org-mac-grab-Firefox+Vimperator-p)
199 ("c" "hrome" org-mac-chrome-insert-frontmost-url ,org-mac-grab-Chrome-app-p)
200 ("t" "ogether" org-mac-together-insert-selected ,org-mac-grab-Together-app-p)
201 ("S" "kim" org-mac-skim-insert-page ,org-mac-grab-Skim-app-p)))
202 (menu-string (make-string 0 ?x))
203 input)
205 ;; Create the menu string for the keymap
206 (mapc '(lambda (descriptor)
207 (when (elt descriptor 3)
208 (setf menu-string (concat menu-string "[" (elt descriptor 0) "]" (elt descriptor 1) " "))))
209 descriptors)
210 (setf (elt menu-string (- (length menu-string) 1)) ?:)
212 ;; Prompt the user, and grab the link
213 (message menu-string)
214 (setq input (read-char-exclusive))
215 (mapc '(lambda (descriptor)
216 (let ((key (elt (elt descriptor 0) 0))
217 (active (elt descriptor 3))
218 (grab-function (elt descriptor 2)))
219 (when (and active (eq input key))
220 (call-interactively grab-function))))
221 descriptors)))
223 (defun org-mac-paste-applescript-links (as-link-list)
224 "Paste in a list of links from an applescript handler. The
225 links are of the form <link>::split::<name>"
226 (let* ((link-list
227 (mapcar
228 (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x) (setq x (match-string 1 x))) x)
229 (split-string as-link-list "[\r\n]+")))
230 split-link URL description orglink orglink-insert rtn orglink-list)
231 (while link-list
232 (setq split-link (split-string (pop link-list) "::split::"))
233 (setq URL (car split-link))
234 (setq description (cadr split-link))
235 (when (not (string= URL ""))
236 (setq orglink (org-make-link-string URL description))
237 (push orglink orglink-list)))
238 (setq rtn (mapconcat 'identity orglink-list "\n"))
239 (kill-new rtn)
240 rtn))
244 ;; Handle links from Firefox.app
246 ;; This code allows you to grab the current active url from the main
247 ;; Firefox.app window, and insert it as a link into an org-mode
248 ;; document. Unfortunately, firefox does not expose an applescript
249 ;; dictionary, so this is necessarily introduces some limitations.
251 ;; The applescript to grab the url from Firefox.app uses the System
252 ;; Events application to give focus to the firefox application, select
253 ;; the contents of the url bar, and copy it. It then uses the title of
254 ;; the window as the text of the link. There is no way to grab links
255 ;; from other open tabs, and further, if there is more than one window
256 ;; open, it is not clear which one will be used (though emperically it
257 ;; seems that it is always the last active window).
259 (defun org-as-mac-firefox-get-frontmost-url ()
260 (let ((result (do-applescript
261 (concat
262 "set oldClipboard to the clipboard\n"
263 "set frontmostApplication to path to frontmost application\n"
264 "tell application \"Firefox\"\n"
265 " activate\n"
266 " delay 0.15\n"
267 " tell application \"System Events\"\n"
268 " keystroke \"l\" using {command down}\n"
269 " keystroke \"a\" using {command down}\n"
270 " keystroke \"c\" using {command down}\n"
271 " end tell\n"
272 " delay 0.15\n"
273 " set theUrl to the clipboard\n"
274 " set the clipboard to oldClipboard\n"
275 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
276 "end tell\n"
277 "activate application (frontmostApplication as text)\n"
278 "set links to {}\n"
279 "copy theResult to the end of links\n"
280 "return links as string\n"))))
281 (car (split-string result "[\r\n]+" t))))
283 (defun org-mac-firefox-get-frontmost-url ()
284 (interactive)
285 (message "Applescript: Getting Firefox url...")
286 (let* ((url-and-title (org-as-mac-firefox-get-frontmost-url))
287 (split-link (split-string url-and-title "::split::"))
288 (URL (car split-link))
289 (description (cadr split-link))
290 (org-link))
291 (when (not (string= URL ""))
292 (setq org-link (org-make-link-string URL description)))
293 (kill-new org-link)
294 org-link))
296 (defun org-mac-firefox-insert-frontmost-url ()
297 (interactive)
298 (insert (org-mac-firefox-get-frontmost-url)))
301 ;; Handle links from Google Firefox.app running the Vimperator extension
302 ;; Grab the frontmost url from Firefox+Vimperator. Same limitations are
303 ;; Firefox
305 (defun org-as-mac-vimperator-get-frontmost-url ()
306 (let ((result (do-applescript
307 (concat
308 "set oldClipboard to the clipboard\n"
309 "set frontmostApplication to path to frontmost application\n"
310 "tell application \"Firefox\"\n"
311 " activate\n"
312 " delay 0.15\n"
313 " tell application \"System Events\"\n"
314 " keystroke \"y\"\n"
315 " end tell\n"
316 " delay 0.15\n"
317 " set theUrl to the clipboard\n"
318 " set the clipboard to oldClipboard\n"
319 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
320 "end tell\n"
321 "activate application (frontmostApplication as text)\n"
322 "set links to {}\n"
323 "copy theResult to the end of links\n"
324 "return links as string\n"))))
325 (replace-regexp-in-string "\s+-\s+Vimperator" "" (car (split-string result "[\r\n]+" t)))))
328 (defun org-mac-vimperator-get-frontmost-url ()
329 (interactive)
330 (message "Applescript: Getting Vimperator url...")
331 (let* ((url-and-title (org-as-mac-vimperator-get-frontmost-url))
332 (split-link (split-string url-and-title "::split::"))
333 (URL (car split-link))
334 (description (cadr split-link))
335 (org-link))
336 (when (not (string= URL ""))
337 (setq org-link (org-make-link-string URL description)))
338 (kill-new org-link)
339 org-link))
341 (defun org-mac-vimperator-insert-frontmost-url ()
342 (interactive)
343 (insert (org-mac-vimperator-get-frontmost-url)))
346 ;; Handle links from Google Chrome.app
347 ;; Grab the frontmost url from Google Chrome. Same limitations as
348 ;; Firefox because Chrome doesn't publish an Applescript dictionary
350 (defun org-as-mac-chrome-get-frontmost-url ()
351 (let ((result (do-applescript
352 (concat
353 "set oldClipboard to the clipboard\n"
354 "set frontmostApplication to path to frontmost application\n"
355 "tell application \"Google Chrome\"\n"
356 " activate\n"
357 " delay 0.15\n"
358 " tell application \"System Events\"\n"
359 " keystroke \"l\" using command down\n"
360 " keystroke \"c\" using command down\n"
361 " end tell\n"
362 " delay 0.15\n"
363 " set theUrl to the clipboard\n"
364 " set the clipboard to oldClipboard\n"
365 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
366 "end tell\n"
367 "activate application (frontmostApplication as text)\n"
368 "set links to {}\n"
369 "copy theResult to the end of links\n"
370 "return links as string\n"))))
371 (car (split-string result "[\r\n]+" t))))
373 (defun org-mac-chrome-get-frontmost-url ()
374 (interactive)
375 (message "Applescript: Getting Chrome url...")
376 (let* ((url-and-title (org-as-mac-chrome-get-frontmost-url))
377 (split-link (split-string url-and-title "::split::"))
378 (URL (car split-link))
379 (description (cadr split-link))
380 (org-link))
381 (when (not (string= URL ""))
382 (setq org-link (org-make-link-string URL description)))
383 (kill-new org-link)
384 org-link))
386 (defun org-mac-chrome-insert-frontmost-url ()
387 (interactive)
388 (insert (org-mac-chrome-get-frontmost-url)))
391 ;; Handle links from Safari.app
392 ;; Grab the frontmost url from Safari.
394 (defun org-as-mac-safari-get-frontmost-url ()
395 (let ((result (do-applescript
396 (concat
397 "tell application \"Safari\"\n"
398 " set theUrl to URL of document 1\n"
399 " set theName to the name of the document 1\n"
400 " return theUrl & \"::split::\" & theName & \"\n\"\n"
401 "end tell\n"))))
402 (car (split-string result "[\r\n]+" t))))
404 (defun org-mac-safari-get-frontmost-url ()
405 (interactive)
406 (message "Applescript: Getting Safari url...")
407 (let* ((url-and-title (org-as-mac-safari-get-frontmost-url))
408 (split-link (split-string url-and-title "::split::"))
409 (URL (car split-link))
410 (description (cadr split-link))
411 (org-link))
412 (when (not (string= URL ""))
413 (setq org-link (org-make-link-string URL description)))
414 (kill-new org-link)
415 org-link))
417 (defun org-mac-safari-insert-frontmost-url ()
418 (interactive)
419 (insert (org-mac-safari-get-frontmost-url)))
424 ;; Handle links from together.app
428 (org-add-link-type "x-together-item" 'org-mac-together-item-open)
430 (defun org-mac-together-item-open (uid)
431 "Open the given uid, which is a reference to an item in Together"
432 (shell-command (concat "open -a Together \"x-together-item:" uid "\"")))
434 (defun as-get-selected-together-items ()
435 (do-applescript
436 (concat
437 "tell application \"Together\"\n"
438 " set theLinkList to {}\n"
439 " set theSelection to selected items\n"
440 " repeat with theItem in theSelection\n"
441 " set theLink to (get item link of theItem) & \"::split::\" & (get name of theItem) & \"\n\"\n"
442 " copy theLink to end of theLinkList\n"
443 " end repeat\n"
444 " return theLinkList as string\n"
445 "end tell")))
447 (defun org-mac-together-get-selected ()
448 (interactive)
449 (message "Applescript: Getting Togther items...")
450 (org-mac-paste-applescript-links (as-get-selected-together-items)))
452 (defun org-mac-together-insert-selected ()
453 (interactive)
454 (insert (org-mac-together-get-selected)))
459 ;; Handle links from Finder.app
463 (defun as-get-selected-finder-items ()
464 (do-applescript
465 (concat
466 "tell application \"Finder\"\n"
467 " set theSelection to the selection\n"
468 " set links to {}\n"
469 " repeat with theItem in theSelection\n"
470 " set theLink to \"file://\" & (POSIX path of (theItem as string)) & \"::split::\" & (get the name of theItem) & \"\n\"\n"
471 " copy theLink to the end of links\n"
472 " end repeat\n"
473 " return links as string\n"
474 "end tell\n")))
476 (defun org-mac-finder-item-get-selected ()
477 (interactive)
478 (message "Applescript: Getting Finder items...")
479 (org-mac-paste-applescript-links (as-get-selected-finder-items)))
481 (defun org-mac-finder-insert-selected ()
482 (interactive)
483 (insert (org-mac-finder-item-get-selected)))
488 ;; Handle links from AddressBook.app
492 (org-add-link-type "addressbook" 'org-mac-addressbook-item-open)
494 (defun org-mac-addressbook-item-open (uid)
495 "Open the given uid, which is a reference to an item in Together"
496 (shell-command (concat "open \"addressbook:" uid "\"")))
498 (defun as-get-selected-addressbook-items ()
499 (do-applescript
500 (concat
501 "tell application \"Address Book\"\n"
502 " set theSelection to the selection\n"
503 " set links to {}\n"
504 " repeat with theItem in theSelection\n"
505 " set theLink to \"addressbook://\" & (the id of theItem) & \"::split::\" & (the name of theItem) & \"\n\"\n"
506 " copy theLink to the end of links\n"
507 " end repeat\n"
508 " return links as string\n"
509 "end tell\n")))
511 (defun org-mac-addressbook-item-get-selected ()
512 (interactive)
513 (message "Applescript: Getting Address Book items...")
514 (org-mac-paste-applescript-links (as-get-selected-addressbook-items)))
516 (defun org-mac-addressbook-insert-selected ()
517 (interactive)
518 (insert (org-mac-addressbook-item-get-selected)))
522 ;; Handle links from Skim.app
524 ;; Original code & idea by Christopher Suckling (org-mac-protocol)
526 (org-add-link-type "skim" 'org-mac-skim-open)
528 (defun org-mac-skim-open (uri)
529 "Visit page of pdf in Skim"
530 (let* ((page (when (string-match "::\\(.+\\)\\'" uri)
531 (match-string 1 uri)))
532 (document (substring uri 0 (match-beginning 0))))
533 (do-applescript
534 (concat
535 "tell application \"Skim\"\n"
536 "activate\n"
537 "set theDoc to \"" document "\"\n"
538 "set thePage to " page "\n"
539 "open theDoc\n"
540 "go document 1 to page thePage of document 1\n"
541 "end tell"))))
544 (defun as-get-skim-page-link ()
545 (do-applescript
546 (concat
547 "tell application \"Skim\"\n"
548 "set theDoc to front document\n"
549 "set theTitle to (name of theDoc)\n"
550 "set thePath to (path of theDoc)\n"
551 "set thePage to (get index for current page of theDoc)\n"
552 "set theSelection to selection of theDoc\n"
553 "set theContent to contents of (get text for theSelection)\n"
554 "if theContent is missing value then\n"
555 " set theContent to theTitle & \", p. \" & thePage\n"
556 (when org-mac-Skim-highlight-selection-p
557 (concat
558 "else\n"
559 " tell theDoc\n"
560 " set theNote to make note with properties {type:highlight note, selection:theSelection}\n"
561 " set text of theNote to (get text for theSelection)\n"
562 " end tell\n"))
563 "end if\n"
564 "set theLink to \"skim://\" & thePath & \"::\" & thePage & "
565 "\"::split::\" & theContent\n"
566 "end tell\n"
567 "return theLink as string\n")))
569 (defun org-mac-skim-get-page ()
570 (interactive)
571 (message "Applescript: Getting Skim page link...")
572 (let* ((link-and-descr (as-get-skim-page-link))
573 (split-link (split-string link-and-descr "::split::"))
574 (link (car split-link))
575 (description (cadr split-link))
576 (org-link))
577 (when (not (string= link ""))
578 (setq org-link (org-make-link-string link description)))
579 (kill-new org-link)
580 org-link))
582 (defun org-mac-skim-insert-page ()
583 (interactive)
584 (insert (org-mac-skim-get-page)))
590 ;; Handle links from Microsoft Outlook.app
593 (org-add-link-type "mac-outlook" 'org-mac-outlook-message-open)
595 (defun org-mac-outlook-message-open (msgid)
596 "Open a message in outlook"
597 (let* ((record-id-string (format "mdfind com_microsoft_outlook_recordID==%s" msgid))
598 (found-message (replace-regexp-in-string "\n$" ""
599 (shell-command-to-string record-id-string))))
600 (if (string= found-message "")
601 (message "org-mac-link: error could not find Outlook message %s" (substring-no-properties msgid))
602 (shell-command (format "open \"`mdfind com_microsoft_outlook_recordID==%s`\"" msgid)))))
604 (defun org-as-get-selected-outlook-mail ()
605 "AppleScript to create links to selected messages in Microsoft Outlook.app."
606 (do-applescript
607 (concat
608 "tell application \"Microsoft Outlook\"\n"
609 "set msgCount to count current messages\n"
610 "if (msgCount < 1) then\n"
611 "return\n"
612 "end if\n"
613 "set theLinkList to {}\n"
614 "set theSelection to (get current messages)\n"
615 "repeat with theMessage in theSelection\n"
616 "set theID to id of theMessage as string\n"
617 "set theURL to \"mac-outlook:\" & theID\n"
618 "set theSubject to subject of theMessage\n"
619 "set theLink to theURL & \"::split::\" & theSubject & \"\n\"\n"
620 "copy theLink to end of theLinkList\n"
621 "end repeat\n"
622 "return theLinkList as string\n"
623 "end tell")))
625 (defun org-sh-get-flagged-outlook-mail ()
626 "Shell commands to create links to flagged messages in Microsoft Outlook.app."
627 (mapconcat
628 (lambda (x) ""
629 (concat
630 "mac-outlook:"
631 (mapconcat
632 (lambda (y) "" y)
633 (split-string
634 (shell-command-to-string
635 (format "mdls -raw -name com_microsoft_outlook_recordID -name kMDItemDisplayName \"%s\"" x))
636 "\000")
637 "::split::")
638 "\n"))
639 (with-temp-buffer
640 (let ((coding-system-for-read (or file-name-coding-system 'utf-8))
641 (coding-system-for-write 'utf-8))
642 (shell-command
643 "mdfind com_microsoft_outlook_flagged==1"
644 (current-buffer)))
645 (split-string
646 (buffer-string) "\n" t))
647 ""))
649 (defun org-mac-outlook-message-get-links (&optional select-or-flag)
650 "Create links to the messages currently selected or flagged in Microsoft Outlook.app.
651 This will use AppleScript to get the message-id and the subject of the
652 messages in Microsoft Outlook.app and make a link out of it.
653 When SELECT-OR-FLAG is \"s\", get the selected messages (this is also
654 the default). When SELECT-OR-FLAG is \"f\", get the flagged messages.
655 The Org-syntax text will be pushed to the kill ring, and also returned."
656 (interactive "sLink to (s)elected or (f)lagged messages: ")
657 (setq select-or-flag (or select-or-flag "s"))
658 (message "Org Mac Outlook: searching mailboxes...")
659 (let* ((as-link-list
660 (if (string= select-or-flag "s")
661 (org-as-get-selected-outlook-mail)
662 (if (string= select-or-flag "f")
663 (org-sh-get-flagged-outlook-mail)
664 (error "Please select \"s\" or \"f\""))))
665 (link-list
666 (mapcar
667 (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x) (setq x (match-string 1 x))) x)
668 (split-string as-link-list "[\r\n]+")))
669 split-link URL description orglink orglink-insert rtn orglink-list)
670 (while link-list
671 (setq split-link (split-string (pop link-list) "::split::"))
672 (setq URL (car split-link))
673 (setq description (cadr split-link))
674 (when (not (string= URL ""))
675 (setq orglink (org-make-link-string URL description))
676 (push orglink orglink-list)))
677 (setq rtn (mapconcat 'identity orglink-list "\n"))
678 (kill-new rtn)
679 rtn))
681 (defun org-mac-outlook-message-insert-selected ()
682 "Insert a link to the messages currently selected in Microsoft Outlook.app.
683 This will use AppleScript to get the message-id and the subject of the
684 active mail in Microsoft Outlook.app and make a link out of it."
685 (interactive)
686 (insert (org-mac-outlook-message-get-links "s")))
688 (defun org-mac-outlook-message-insert-flagged (org-buffer org-heading)
689 "Asks for an org buffer and a heading within it, and replace message links.
690 If heading exists, delete all mac-outlook:// links within heading's first
691 level. If heading doesn't exist, create it at point-max. Insert
692 list of mac-outlook:// links to flagged mail after heading."
693 (interactive "bBuffer in which to insert links: \nsHeading after which to insert links: ")
694 (with-current-buffer org-buffer
695 (goto-char (point-min))
696 (let ((isearch-forward t)
697 (message-re "\\[\\[\\(mac-outlook:\\)\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"))
698 (if (org-goto-local-search-headings org-heading nil t)
699 (if (not (eobp))
700 (progn
701 (save-excursion
702 (while (re-search-forward
703 message-re (save-excursion (outline-next-heading)) t)
704 (delete-region (match-beginning 0) (match-end 0)))
705 (insert "\n" (org-mac-outlook-message-get-links "f")))
706 (flush-lines "^$" (point) (outline-next-heading)))
707 (insert "\n" (org-mac-outlook-message-get-links "f")))
708 (goto-char (point-max))
709 (insert "\n")
710 (org-insert-heading nil t)
711 (insert org-heading "\n" (org-mac-outlook-message-get-links "f"))))))
717 ;; Handle links from Mail.app
720 (org-add-link-type "message" 'org-mac-message-open)
722 (defun org-mac-message-open (message-id)
723 "Visit the message with the given MESSAGE-ID.
724 This will use the command `open' with the message URL."
725 (start-process (concat "open message:" message-id) nil
726 "open" (concat "message://<" (substring message-id 2) ">")))
728 (defun org-as-get-selected-mail ()
729 "AppleScript to create links to selected messages in Mail.app."
730 (do-applescript
731 (concat
732 "tell application \"Mail\"\n"
733 "set theLinkList to {}\n"
734 "set theSelection to selection\n"
735 "repeat with theMessage in theSelection\n"
736 "set theID to message id of theMessage\n"
737 "set theSubject to subject of theMessage\n"
738 "set theLink to \"message://\" & theID & \"::split::\" & theSubject & \"\n\"\n"
739 "copy theLink to end of theLinkList\n"
740 "end repeat\n"
741 "return theLinkList as string\n"
742 "end tell")))
744 (defun org-as-get-flagged-mail ()
745 "AppleScript to create links to flagged messages in Mail.app."
746 (do-applescript
747 (concat
748 ;; Is Growl installed?
749 "tell application \"System Events\"\n"
750 "set growlHelpers to the name of every process whose creator type contains \"GRRR\"\n"
751 "if (count of growlHelpers) > 0 then\n"
752 "set growlHelperApp to item 1 of growlHelpers\n"
753 "else\n"
754 "set growlHelperApp to \"\"\n"
755 "end if\n"
756 "end tell\n"
758 ;; Get links
759 "tell application \"Mail\"\n"
760 "set theMailboxes to every mailbox of account \"" org-mac-mail-account "\"\n"
761 "set theLinkList to {}\n"
762 "repeat with aMailbox in theMailboxes\n"
763 "set theSelection to (every message in aMailbox whose flagged status = true)\n"
764 "repeat with theMessage in theSelection\n"
765 "set theID to message id of theMessage\n"
766 "set theSubject to subject of theMessage\n"
767 "set theLink to \"message://\" & theID & \"::split::\" & theSubject & \"\n\"\n"
768 "copy theLink to end of theLinkList\n"
770 ;; Report progress through Growl
771 ;; This "double tell" idiom is described in detail at
772 ;; http://macscripter.net/viewtopic.php?id=24570 The
773 ;; script compiler needs static knowledge of the
774 ;; growlHelperApp. Hmm, since we're compiling
775 ;; on-the-fly here, this is likely to be way less
776 ;; portable than I'd hoped. It'll work when the name
777 ;; is still "GrowlHelperApp", though.
778 "if growlHelperApp is not \"\" then\n"
779 "tell application \"GrowlHelperApp\"\n"
780 "tell application growlHelperApp\n"
781 "set the allNotificationsList to {\"FlaggedMail\"}\n"
782 "set the enabledNotificationsList to allNotificationsList\n"
783 "register as application \"FlaggedMail\" all notifications allNotificationsList default notifications enabledNotificationsList icon of application \"Mail\"\n"
784 "notify with name \"FlaggedMail\" title \"Importing flagged message\" description theSubject application name \"FlaggedMail\"\n"
785 "end tell\n"
786 "end tell\n"
787 "end if\n"
788 "end repeat\n"
789 "end repeat\n"
790 "return theLinkList as string\n"
791 "end tell")))
793 (defun org-mac-message-get-links (&optional select-or-flag)
794 "Create links to the messages currently selected or flagged in Mail.app.
795 This will use AppleScript to get the message-id and the subject of the
796 messages in Mail.app and make a link out of it.
797 When SELECT-OR-FLAG is \"s\", get the selected messages (this is also
798 the default). When SELECT-OR-FLAG is \"f\", get the flagged messages.
799 The Org-syntax text will be pushed to the kill ring, and also returned."
800 (interactive "sLink to (s)elected or (f)lagged messages: ")
801 (setq select-or-flag (or select-or-flag "s"))
802 (message "AppleScript: searching mailboxes...")
803 (let* ((as-link-list
804 (if (string= select-or-flag "s")
805 (org-as-get-selected-mail)
806 (if (string= select-or-flag "f")
807 (org-as-get-flagged-mail)
808 (error "Please select \"s\" or \"f\""))))
809 (link-list
810 (mapcar
811 (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x) (setq x (match-string 1 x))) x)
812 (split-string as-link-list "[\r\n]+")))
813 split-link URL description orglink orglink-insert rtn orglink-list)
814 (while link-list
815 (setq split-link (split-string (pop link-list) "::split::"))
816 (setq URL (car split-link))
817 (setq description (cadr split-link))
818 (when (not (string= URL ""))
819 (setq orglink (org-make-link-string URL description))
820 (push orglink orglink-list)))
821 (setq rtn (mapconcat 'identity orglink-list "\n"))
822 (kill-new rtn)
823 rtn))
825 (defun org-mac-message-insert-selected ()
826 "Insert a link to the messages currently selected in Mail.app.
827 This will use AppleScript to get the message-id and the subject of the
828 active mail in Mail.app and make a link out of it."
829 (interactive)
830 (insert (org-mac-message-get-links "s")))
832 ;; The following line is for backward compatibility
833 (defalias 'org-mac-message-insert-link 'org-mac-message-insert-selected)
835 (defun org-mac-message-insert-flagged (org-buffer org-heading)
836 "Asks for an org buffer and a heading within it, and replace message links.
837 If heading exists, delete all message:// links within heading's first
838 level. If heading doesn't exist, create it at point-max. Insert
839 list of message:// links to flagged mail after heading."
840 (interactive "bBuffer in which to insert links: \nsHeading after which to insert links: ")
841 (with-current-buffer org-buffer
842 (goto-char (point-min))
843 (let ((isearch-forward t)
844 (message-re "\\[\\[\\(message:\\)\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"))
845 (if (org-goto-local-search-headings org-heading nil t)
846 (if (not (eobp))
847 (progn
848 (save-excursion
849 (while (re-search-forward
850 message-re (save-excursion (outline-next-heading)) t)
851 (delete-region (match-beginning 0) (match-end 0)))
852 (insert "\n" (org-mac-message-get-links "f")))
853 (flush-lines "^$" (point) (outline-next-heading)))
854 (insert "\n" (org-mac-message-get-links "f")))
855 (goto-char (point-max))
856 (insert "\n")
857 (org-insert-heading nil t)
858 (insert org-heading "\n" (org-mac-message-get-links "f"))))))
861 (provide 'org-mac-link)
863 ;;; org-mac-link.el ends here