org-mac-link.el: Don't fail on machines without Growl installed
[org-mode.git] / contrib / lisp / org-mac-link.el
blobe05a67d0e44c7a6d9ba7aa4028be1e013a07117f
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-2014 Free Software Foundation, Inc.
5 ;;
6 ;; Author: Anthony Lander <anthony.lander@gmail.com>
7 ;; John Wiegley <johnw@gnu.org>
8 ;; Christopher Suckling <suckling at gmail dot com>
9 ;; Daniil Frumin <difrumin@gmail.com>
10 ;; Alan Schmitt <alan.schmitt@polytechnique.org>
11 ;; Mike McLean <mike.mclean@pobox.com>
14 ;; Version: 1.1
15 ;; Keywords: org, mac, hyperlink
17 ;; Version: 1.2
18 ;; Keywords: outlook
19 ;; Author: Mike McLean <mike.mclean@pobox.com>
20 ;; Add support for Microsoft Outlook for Mac as Org mode links
22 ;; This file is not part of GNU Emacs.
24 ;; This program is free software; you can redistribute it and/or modify
25 ;; it under the terms of the GNU General Public License as published by
26 ;; the Free Software Foundation; either version 3, or (at your option)
27 ;; any later version.
29 ;; This program is distributed in the hope that it will be useful,
30 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
31 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 ;; GNU General Public License for more details.
34 ;; You should have received a copy of the GNU General Public License
35 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
37 ;;; Commentary:
39 ;; This code allows you to grab either the current selected items, or
40 ;; the frontmost url in various mac appliations, and insert them as
41 ;; hyperlinks into the current org-mode document at point.
43 ;; This code is heavily based on, and indeed incorporates,
44 ;; org-mac-message.el written by John Wiegley and Christopher
45 ;; Suckling.
47 ;; Detailed comments for each application interface are inlined with
48 ;; the code. Here is a brief overview of how the code interacts with
49 ;; each application:
51 ;; Finder.app - grab links to the selected files in the frontmost window
52 ;; Mail.app - grab links to the selected messages in the message list
53 ;; AddressBook.app - Grab links to the selected addressbook Cards
54 ;; Firefox.app - Grab the url of the frontmost tab in the frontmost window
55 ;; Vimperator/Firefox.app - Grab the url of the frontmost tab in the frontmost window
56 ;; Safari.app - Grab the url of the frontmost tab in the frontmost window
57 ;; Google Chrome.app - Grab the url of the frontmost tab in the frontmost window
58 ;; Together.app - Grab links to the selected items in the library list
59 ;; Skim.app - Grab a link to the selected page in the topmost pdf document
60 ;; Microsoft Outlook.app - Grab a link to the selected message in the message list
61 ;; DEVONthink Pro Office.app - Grab a link to the selected DEVONthink item(s); open DEVONthink item by reference
64 ;; Installation:
66 ;; add (require 'org-mac-link) to your .emacs, and optionally bind a
67 ;; key to activate the link grabber menu, like this:
69 ;; (add-hook 'org-mode-hook (lambda ()
70 ;; (define-key org-mode-map (kbd "C-c g") 'org-mac-grab-link)))
72 ;; Usage:
74 ;; Type C-c g (or whatever key you defined, as above), or type M-x
75 ;; org-mac-grab-link RET to activate the link grabber. This will present
76 ;; you with a menu to choose an application from which to grab a link
77 ;; to insert at point. You may also type C-g to abort.
79 ;; Customizing:
81 ;; You may customize which applications appear in the grab menu by
82 ;; customizing the group `org-mac-link'. Changes take effect
83 ;; immediately.
86 ;;; Code:
88 (require 'org)
90 (defgroup org-mac-link nil
91 "Options for grabbing links from Mac applications."
92 :tag "Org Mac link"
93 :group 'org-link)
95 (defcustom org-mac-grab-Finder-app-p t
96 "Add menu option [F]inder to grab links from the Finder."
97 :tag "Grab Finder.app links"
98 :group 'org-mac-link
99 :type 'boolean)
101 (defcustom org-mac-grab-Mail-app-p t
102 "Add menu option [m]ail to grab links from Mail.app."
103 :tag "Grab Mail.app links"
104 :group 'org-mac-link
105 :type 'boolean)
107 (defcustom org-mac-grab-Outlook-app-p t
108 "Add menu option [o]utlook to grab links from Microsoft Outlook.app."
109 :tag "Grab Microsoft Outlook.app links"
110 :group 'org-mac-link
111 :type 'boolean)
113 (defcustom org-mac-grab-devonthink-app-p t
114 "Add menu option [d]EVONthink to grab links from DEVONthink Pro Office.app."
115 :tag "Grab DEVONthink Pro Office.app links"
116 :group 'org-mac-link
117 :type 'boolean)
119 (defcustom org-mac-grab-Addressbook-app-p t
120 "Add menu option [a]ddressbook to grab links from AddressBook.app."
121 :tag "Grab AddressBook.app links"
122 :group 'org-mac-link
123 :type 'boolean)
125 (defcustom org-mac-grab-Safari-app-p t
126 "Add menu option [s]afari to grab links from Safari.app."
127 :tag "Grab Safari.app links"
128 :group 'org-mac-link
129 :type 'boolean)
131 (defcustom org-mac-grab-Firefox-app-p t
132 "Add menu option [f]irefox to grab links from Firefox.app."
133 :tag "Grab Firefox.app links"
134 :group 'org-mac-link
135 :type 'boolean)
137 (defcustom org-mac-grab-Firefox+Vimperator-p nil
138 "Add menu option [v]imperator to grab links from Firefox.app running the Vimperator plugin."
139 :tag "Grab Vimperator/Firefox.app links"
140 :group 'org-mac-link
141 :type 'boolean)
143 (defcustom org-mac-grab-Chrome-app-p t
144 "Add menu option [c]hrome to grab links from Google Chrome.app."
145 :tag "Grab Google Chrome.app links"
146 :group 'org-mac-link
147 :type 'boolean)
149 (defcustom org-mac-grab-Together-app-p nil
150 "Add menu option [t]ogether to grab links from Together.app."
151 :tag "Grab Together.app links"
152 :group 'org-mac-link
153 :type 'boolean)
155 (defcustom org-mac-grab-Skim-app-p
156 (< 0 (length (shell-command-to-string
157 "mdfind kMDItemCFBundleIdentifier == 'net.sourceforge.skim-app.skim'")))
158 "Add menu option [S]kim to grab page links from Skim.app."
159 :tag "Grab Skim.app page links"
160 :group 'org-mac-link
161 :type 'boolean)
163 (defcustom org-mac-Skim-highlight-selection-p nil
164 "Highlight the active selection when grabbing a link from Skim.app."
165 :tag "Highlight selection in Skim.app"
166 :group 'org-mac-link
167 :type 'boolean)
169 (defgroup org-mac-flagged-mail nil
170 "Options foring linking to flagged Mail.app messages."
171 :tag "Org Mail.app"
172 :group 'org-link)
174 (defcustom org-mac-mail-account nil
175 "The Mail.app account in which to search for flagged messages."
176 :group 'org-mac-flagged-mail
177 :type 'string)
180 ;; In mac.c, removed in Emacs 23.
181 (declare-function do-applescript "org-mac-message" (script))
182 (unless (fboundp 'do-applescript)
183 ;; Need to fake this using shell-command-to-string
184 (defun do-applescript (script)
185 (let (start cmd return)
186 (while (string-match "\n" script)
187 (setq script (replace-match "\r" t t script)))
188 (while (string-match "'" script start)
189 (setq start (+ 2 (match-beginning 0))
190 script (replace-match "\\'" t t script)))
191 (setq cmd (concat "osascript -e '" script "'"))
192 (setq return (shell-command-to-string cmd))
193 (concat "\"" (org-trim return) "\""))))
195 (defun org-mac-grab-link ()
196 "Prompt for an application to grab a link from.
197 When done, go grab the link, and insert it at point."
198 (interactive)
199 (let* ((descriptors
200 `(("F" "inder" org-mac-finder-insert-selected ,org-mac-grab-Finder-app-p)
201 ("m" "ail" org-mac-message-insert-selected ,org-mac-grab-Mail-app-p)
202 ("d" "EVONthink Pro Office" org-mac-devonthink-item-insert-selected
203 ,org-mac-grab-devonthink-app-p)
204 ("o" "utlook" org-mac-outlook-message-insert-selected ,org-mac-grab-Outlook-app-p)
205 ("a" "ddressbook" org-mac-addressbook-insert-selected ,org-mac-grab-Addressbook-app-p)
206 ("s" "afari" org-mac-safari-insert-frontmost-url ,org-mac-grab-Safari-app-p)
207 ("f" "irefox" org-mac-firefox-insert-frontmost-url ,org-mac-grab-Firefox-app-p)
208 ("v" "imperator" org-mac-vimperator-insert-frontmost-url ,org-mac-grab-Firefox+Vimperator-p)
209 ("c" "hrome" org-mac-chrome-insert-frontmost-url ,org-mac-grab-Chrome-app-p)
210 ("t" "ogether" org-mac-together-insert-selected ,org-mac-grab-Together-app-p)
211 ("S" "kim" org-mac-skim-insert-page ,org-mac-grab-Skim-app-p)))
212 (menu-string (make-string 0 ?x))
213 input)
215 ;; Create the menu string for the keymap
216 (mapc '(lambda (descriptor)
217 (when (elt descriptor 3)
218 (setf menu-string (concat menu-string
219 "[" (elt descriptor 0) "]"
220 (elt descriptor 1) " "))))
221 descriptors)
222 (setf (elt menu-string (- (length menu-string) 1)) ?:)
224 ;; Prompt the user, and grab the link
225 (message menu-string)
226 (setq input (read-char-exclusive))
227 (mapc '(lambda (descriptor)
228 (let ((key (elt (elt descriptor 0) 0))
229 (active (elt descriptor 3))
230 (grab-function (elt descriptor 2)))
231 (when (and active (eq input key))
232 (call-interactively grab-function))))
233 descriptors)))
235 (defun org-mac-paste-applescript-links (as-link-list)
236 "Paste in a list of links from an applescript handler.
237 The links are of the form <link>::split::<name>."
238 (let* ((link-list
239 (mapcar (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x)
240 (setq x (match-string 1 x)))
242 (split-string as-link-list "[\r\n]+")))
243 split-link URL description orglink orglink-insert rtn orglink-list)
244 (while link-list
245 (setq split-link (split-string (pop link-list) "::split::"))
246 (setq URL (car split-link))
247 (setq description (cadr split-link))
248 (when (not (string= URL ""))
249 (setq orglink (org-make-link-string URL description))
250 (push orglink orglink-list)))
251 (setq rtn (mapconcat 'identity orglink-list "\n"))
252 (kill-new rtn)
253 rtn))
256 ;; Handle links from Firefox.app
258 ;; This code allows you to grab the current active url from the main
259 ;; Firefox.app window, and insert it as a link into an org-mode
260 ;; document. Unfortunately, firefox does not expose an applescript
261 ;; dictionary, so this is necessarily introduces some limitations.
263 ;; The applescript to grab the url from Firefox.app uses the System
264 ;; Events application to give focus to the firefox application, select
265 ;; the contents of the url bar, and copy it. It then uses the title of
266 ;; the window as the text of the link. There is no way to grab links
267 ;; from other open tabs, and further, if there is more than one window
268 ;; open, it is not clear which one will be used (though emperically it
269 ;; seems that it is always the last active window).
271 (defun org-as-mac-firefox-get-frontmost-url ()
272 (let ((result
273 (do-applescript
274 (concat
275 "set oldClipboard to the clipboard\n"
276 "set frontmostApplication to path to frontmost application\n"
277 "tell application \"Firefox\"\n"
278 " activate\n"
279 " delay 0.15\n"
280 " tell application \"System Events\"\n"
281 " keystroke \"l\" using {command down}\n"
282 " keystroke \"a\" using {command down}\n"
283 " keystroke \"c\" using {command down}\n"
284 " end tell\n"
285 " delay 0.15\n"
286 " set theUrl to the clipboard\n"
287 " set the clipboard to oldClipboard\n"
288 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
289 "end tell\n"
290 "activate application (frontmostApplication as text)\n"
291 "set links to {}\n"
292 "copy theResult to the end of links\n"
293 "return links as string\n"))))
294 (car (split-string result "[\r\n]+" t))))
296 (defun org-mac-firefox-get-frontmost-url ()
297 (interactive)
298 (message "Applescript: Getting Firefox url...")
299 (let* ((url-and-title (org-as-mac-firefox-get-frontmost-url))
300 (split-link (split-string url-and-title "::split::"))
301 (URL (car split-link))
302 (description (cadr split-link))
303 (org-link))
304 (when (not (string= URL ""))
305 (setq org-link (org-make-link-string URL description)))
306 (kill-new org-link)
307 org-link))
309 (defun org-mac-firefox-insert-frontmost-url ()
310 (interactive)
311 (insert (org-mac-firefox-get-frontmost-url)))
314 ;; Handle links from Google Firefox.app running the Vimperator extension
315 ;; Grab the frontmost url from Firefox+Vimperator. Same limitations are
316 ;; Firefox
318 (defun org-as-mac-vimperator-get-frontmost-url ()
319 (let ((result
320 (do-applescript
321 (concat
322 "set oldClipboard to the clipboard\n"
323 "set frontmostApplication to path to frontmost application\n"
324 "tell application \"Firefox\"\n"
325 " activate\n"
326 " delay 0.15\n"
327 " tell application \"System Events\"\n"
328 " keystroke \"y\"\n"
329 " end tell\n"
330 " delay 0.15\n"
331 " set theUrl to the clipboard\n"
332 " set the clipboard to oldClipboard\n"
333 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
334 "end tell\n"
335 "activate application (frontmostApplication as text)\n"
336 "set links to {}\n"
337 "copy theResult to the end of links\n"
338 "return links as string\n"))))
339 (replace-regexp-in-string
340 "\s+-\s+Vimperator" "" (car (split-string result "[\r\n]+" t)))))
342 (defun org-mac-vimperator-get-frontmost-url ()
343 (interactive)
344 (message "Applescript: Getting Vimperator url...")
345 (let* ((url-and-title (org-as-mac-vimperator-get-frontmost-url))
346 (split-link (split-string url-and-title "::split::"))
347 (URL (car split-link))
348 (description (cadr split-link))
349 (org-link))
350 (when (not (string= URL ""))
351 (setq org-link (org-make-link-string URL description)))
352 (kill-new org-link)
353 org-link))
355 (defun org-mac-vimperator-insert-frontmost-url ()
356 (interactive)
357 (insert (org-mac-vimperator-get-frontmost-url)))
360 ;; Handle links from Google Chrome.app
361 ;; Grab the frontmost url from Google Chrome. Same limitations as
362 ;; Firefox because Chrome doesn't publish an Applescript dictionary
364 (defun org-as-mac-chrome-get-frontmost-url ()
365 (let ((result
366 (do-applescript
367 (concat
368 "set frontmostApplication to path to frontmost application\n"
369 "tell application \"Google Chrome\"\n"
370 " set theUrl to get URL of active tab of first window\n"
371 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
372 "end tell\n"
373 "activate application (frontmostApplication as text)\n"
374 "set links to {}\n"
375 "copy theResult to the end of links\n"
376 "return links as string\n"))))
377 (replace-regexp-in-string
378 "^\"\\|\"$" "" (car (split-string result "[\r\n]+" t)))))
380 (defun org-mac-chrome-get-frontmost-url ()
381 (interactive)
382 (message "Applescript: Getting Chrome url...")
383 (let* ((url-and-title (org-as-mac-chrome-get-frontmost-url))
384 (split-link (split-string url-and-title "::split::"))
385 (URL (car split-link))
386 (description (cadr split-link))
387 (org-link))
388 (when (not (string= URL ""))
389 (setq org-link (org-make-link-string URL description)))
390 (kill-new org-link)
391 org-link))
393 (defun org-mac-chrome-insert-frontmost-url ()
394 (interactive)
395 (insert (org-mac-chrome-get-frontmost-url)))
398 ;; Handle links from Safari.app
399 ;; Grab the frontmost url from Safari.
401 (defun org-as-mac-safari-get-frontmost-url ()
402 (let ((result
403 (do-applescript
404 (concat
405 "tell application \"Safari\"\n"
406 " set theUrl to URL of document 1\n"
407 " set theName to the name of the document 1\n"
408 " return theUrl & \"::split::\" & theName & \"\n\"\n"
409 "end tell\n"))))
410 (car (split-string result "[\r\n]+" t))))
412 (defun org-mac-safari-get-frontmost-url ()
413 (interactive)
414 (message "Applescript: Getting Safari url...")
415 (let* ((url-and-title (org-as-mac-safari-get-frontmost-url))
416 (split-link (split-string url-and-title "::split::"))
417 (URL (car split-link))
418 (description (cadr split-link))
419 (org-link))
420 (when (not (string= URL ""))
421 (setq org-link (org-make-link-string URL description)))
422 (kill-new org-link)
423 org-link))
425 (defun org-mac-safari-insert-frontmost-url ()
426 (interactive)
427 (insert (org-mac-safari-get-frontmost-url)))
430 ;; Handle links from together.app
432 (org-add-link-type "x-together-item" 'org-mac-together-item-open)
434 (defun org-mac-together-item-open (uid)
435 "Open UID, which is a reference to an item in Together."
436 (shell-command (concat "open -a Together \"x-together-item:" uid "\"")))
438 (defun as-get-selected-together-items ()
439 (do-applescript
440 (concat
441 "tell application \"Together\"\n"
442 " set theLinkList to {}\n"
443 " set theSelection to selected items\n"
444 " repeat with theItem in theSelection\n"
445 " set theLink to (get item link of theItem) & \"::split::\" & (get name of theItem) & \"\n\"\n"
446 " copy theLink to end of theLinkList\n"
447 " end repeat\n"
448 " return theLinkList as string\n"
449 "end tell")))
451 (defun org-mac-together-get-selected ()
452 (interactive)
453 (message "Applescript: Getting Togther items...")
454 (org-mac-paste-applescript-links (as-get-selected-together-items)))
456 (defun org-mac-together-insert-selected ()
457 (interactive)
458 (insert (org-mac-together-get-selected)))
461 ;; 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)))
486 ;; Handle links from AddressBook.app
488 (org-add-link-type "addressbook" 'org-mac-addressbook-item-open)
490 (defun org-mac-addressbook-item-open (uid)
491 "Open UID, which is a reference to an item in the addressbook."
492 (shell-command (concat "open \"addressbook:" uid "\"")))
494 (defun as-get-selected-addressbook-items ()
495 (do-applescript
496 (concat
497 "tell application \"Address Book\"\n"
498 " set theSelection to the selection\n"
499 " set links to {}\n"
500 " repeat with theItem in theSelection\n"
501 " set theLink to \"addressbook://\" & (the id of theItem) & \"::split::\" & (the name of theItem) & \"\n\"\n"
502 " copy theLink to the end of links\n"
503 " end repeat\n"
504 " return links as string\n"
505 "end tell\n")))
507 (defun org-mac-addressbook-item-get-selected ()
508 (interactive)
509 (message "Applescript: Getting Address Book items...")
510 (org-mac-paste-applescript-links (as-get-selected-addressbook-items)))
512 (defun org-mac-addressbook-insert-selected ()
513 (interactive)
514 (insert (org-mac-addressbook-item-get-selected)))
517 ;; Handle links from Skim.app
519 ;; Original code & idea by Christopher Suckling (org-mac-protocol)
521 (org-add-link-type "skim" 'org-mac-skim-open)
523 (defun org-mac-skim-open (uri)
524 "Visit page of pdf in Skim"
525 (let* ((page (when (string-match "::\\(.+\\)\\'" uri)
526 (match-string 1 uri)))
527 (document (substring uri 0 (match-beginning 0))))
528 (do-applescript
529 (concat
530 "tell application \"Skim\"\n"
531 "activate\n"
532 "set theDoc to \"" document "\"\n"
533 "set thePage to " page "\n"
534 "open theDoc\n"
535 "go document 1 to page thePage of document 1\n"
536 "end tell"))))
538 (defun as-get-skim-page-link ()
539 (do-applescript
540 (concat
541 "tell application \"Skim\"\n"
542 "set theDoc to front document\n"
543 "set theTitle to (name of theDoc)\n"
544 "set thePath to (path of theDoc)\n"
545 "set thePage to (get index for current page of theDoc)\n"
546 "set theSelection to selection of theDoc\n"
547 "set theContent to contents of (get text for theSelection)\n"
548 "if theContent is missing value then\n"
549 " set theContent to theTitle & \", p. \" & thePage\n"
550 (when org-mac-Skim-highlight-selection-p
551 (concat
552 "else\n"
553 " tell theDoc\n"
554 " set theNote to make note with properties {type:highlight note, selection:theSelection}\n"
555 " set text of theNote to (get text for theSelection)\n"
556 " end tell\n"))
557 "end if\n"
558 "set theLink to \"skim://\" & thePath & \"::\" & thePage & "
559 "\"::split::\" & theContent\n"
560 "end tell\n"
561 "return theLink as string\n")))
563 (defun org-mac-skim-get-page ()
564 (interactive)
565 (message "Applescript: Getting Skim page link...")
566 (let* ((link-and-descr (as-get-skim-page-link))
567 (split-link (split-string link-and-descr "::split::"))
568 (link (car split-link))
569 (description (cadr split-link))
570 (org-link))
571 (when (not (string= link ""))
572 (setq org-link (org-make-link-string link description)))
573 (kill-new org-link)
574 org-link))
576 (defun org-mac-skim-insert-page ()
577 (interactive)
578 (insert (org-mac-skim-get-page)))
581 ;; Handle links from Microsoft Outlook.app
583 (org-add-link-type "mac-outlook" 'org-mac-outlook-message-open)
585 (defun org-mac-outlook-message-open (msgid)
586 "Open a message in Outlook"
587 (do-applescript
588 (concat
589 "tell application \"Microsoft Outlook\"\n"
590 (format "open message id %s\n" (substring-no-properties msgid))
591 "activate\n"
592 "end tell")))
594 (defun org-as-get-selected-outlook-mail ()
595 "AppleScript to create links to selected messages in Microsoft Outlook.app."
596 (do-applescript
597 (concat
598 "tell application \"Microsoft Outlook\"\n"
599 "set msgCount to count current messages\n"
600 "if (msgCount < 1) then\n"
601 "return\n"
602 "end if\n"
603 "set theLinkList to {}\n"
604 "set theSelection to (get current messages)\n"
605 "repeat with theMessage in theSelection\n"
606 "set theID to id of theMessage as string\n"
607 "set theURL to \"mac-outlook:\" & theID\n"
608 "set theSubject to subject of theMessage\n"
609 "set theLink to theURL & \"::split::\" & theSubject & \"\n\"\n"
610 "copy theLink to end of theLinkList\n"
611 "end repeat\n"
612 "return theLinkList as string\n"
613 "end tell")))
615 (defun org-sh-get-flagged-outlook-mail ()
616 "Shell commands to create links to flagged messages in Microsoft Outlook.app."
617 (mapconcat
618 (lambda (x) ""
619 (concat
620 "mac-outlook:"
621 (mapconcat
622 (lambda (y) "" y)
623 (split-string
624 (shell-command-to-string
625 (format "mdls -raw -name com_microsoft_outlook_recordID -name kMDItemDisplayName \"%s\"" x))
626 "\000")
627 "::split::")
628 "\n"))
629 (with-temp-buffer
630 (let ((coding-system-for-read (or file-name-coding-system 'utf-8))
631 (coding-system-for-write 'utf-8))
632 (shell-command
633 "mdfind com_microsoft_outlook_flagged==1"
634 (current-buffer)))
635 (split-string
636 (buffer-string) "\n" t))
637 ""))
639 (defun org-mac-outlook-message-get-links (&optional select-or-flag)
640 "Create links to the messages currently selected or flagged in Microsoft Outlook.app.
641 This will use AppleScript to get the message-id and the subject of the
642 messages in Microsoft Outlook.app and make a link out of it.
643 When SELECT-OR-FLAG is \"s\", get the selected messages (this is also
644 the default). When SELECT-OR-FLAG is \"f\", get the flagged messages.
645 The Org-syntax text will be pushed to the kill ring, and also returned."
646 (interactive "sLink to (s)elected or (f)lagged messages: ")
647 (setq select-or-flag (or select-or-flag "s"))
648 (message "Org Mac Outlook: searching mailboxes...")
649 (let* ((as-link-list
650 (if (string= select-or-flag "s")
651 (org-as-get-selected-outlook-mail)
652 (if (string= select-or-flag "f")
653 (org-sh-get-flagged-outlook-mail)
654 (error "Please select \"s\" or \"f\""))))
655 (link-list
656 (mapcar
657 (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x) (setq x (match-string 1 x))) x)
658 (split-string as-link-list "[\r\n]+")))
659 split-link URL description orglink orglink-insert rtn orglink-list)
660 (while link-list
661 (setq split-link (split-string (pop link-list) "::split::"))
662 (setq URL (car split-link))
663 (setq description (cadr split-link))
664 (when (not (string= URL ""))
665 (setq orglink (org-make-link-string URL description))
666 (push orglink orglink-list)))
667 (setq rtn (mapconcat 'identity orglink-list "\n"))
668 (kill-new rtn)
669 rtn))
671 (defun org-mac-outlook-message-insert-selected ()
672 "Insert a link to the messages currently selected in Microsoft Outlook.app.
673 This will use AppleScript to get the message-id and the subject
674 of the active mail in Microsoft Outlook.app and make a link out
675 of it."
676 (interactive)
677 (insert (org-mac-outlook-message-get-links "s")))
679 (defun org-mac-outlook-message-insert-flagged (org-buffer org-heading)
680 "Asks for an org buffer and a heading within it, and replace message links.
681 If heading exists, delete all mac-outlook:// links within
682 heading's first level. If heading doesn't exist, create it at
683 point-max. Insert list of mac-outlook:// links to flagged mail
684 after heading."
685 (interactive "bBuffer in which to insert links: \nsHeading after which to insert links: ")
686 (with-current-buffer org-buffer
687 (goto-char (point-min))
688 (let ((isearch-forward t)
689 (message-re "\\[\\[\\(mac-outlook:\\)\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"))
690 (if (org-goto-local-search-headings org-heading nil t)
691 (if (not (eobp))
692 (progn
693 (save-excursion
694 (while (re-search-forward
695 message-re (save-excursion (outline-next-heading)) t)
696 (delete-region (match-beginning 0) (match-end 0)))
697 (insert "\n" (org-mac-outlook-message-get-links "f")))
698 (flush-lines "^$" (point) (outline-next-heading)))
699 (insert "\n" (org-mac-outlook-message-get-links "f")))
700 (goto-char (point-max))
701 (insert "\n")
702 (org-insert-heading nil t)
703 (insert org-heading "\n" (org-mac-outlook-message-get-links "f"))))))
706 ;; Handle links from DEVONthink Pro Office.app
708 (org-add-link-type "x-devonthink-item" 'org-devonthink-item-open)
710 (defun org-devonthink-item-open (uid)
711 "Open UID, which is a reference to an item in DEVONthink Pro Office."
712 (shell-command (concat "open \"x-devonthink-item:" uid "\"")))
714 (defun org-as-get-selected-devonthink-item ()
715 "AppleScript to create links to selected items in DEVONthink Pro Office.app."
716 (do-applescript
717 (concat
718 "set theLinkList to {}\n"
719 "tell application \"DEVONthink Pro\"\n"
720 "set selectedRecords to selection\n"
721 "set selectionCount to count of selectedRecords\n"
722 "if (selectionCount < 1) then\n"
723 "return\n"
724 "end if\n"
725 "repeat with theRecord in selectedRecords\n"
726 "set theID to uuid of theRecord\n"
727 "set theURL to \"x-devonthink-item:\" & theID\n"
728 "set theSubject to name of theRecord\n"
729 "set theLink to theURL & \"::split::\" & theSubject & \"\n\"\n"
730 "copy theLink to end of theLinkList\n"
731 "end repeat\n"
732 "end tell\n"
733 "return theLinkList as string"
736 (defun org-mac-devonthink-get-links ()
737 "Create links to the item(s) currently selected in DEVONthink Pro Office.
738 This will use AppleScript to get the `uuid' and the `name' of the
739 selected items in DEVONthink Pro Office.app and make links out of
740 it/them. This function will push the Org-syntax text to the kill
741 ring, and also return it."
742 (message "Org Mac DEVONthink: looking for selected items...")
743 (let* ((as-link-list (org-as-get-selected-devonthink-item))
744 (link-list (if as-link-list
745 (mapcar
746 (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x)
747 (setq x (match-string 1 x)))
749 (split-string as-link-list "[\r\n]+"))
750 nil))
751 orglink-list)
752 (while link-list
753 (let* ((current-item (pop link-list)))
754 (message "current item: %s" current-item)
755 (when (and current-item (not (string= current-item "")))
756 (let* ((split-link (split-string current-item "::split::"))
757 (orglink (org-make-link-string
758 (url-encode-url (car split-link))
759 (cadr split-link))))
760 (push orglink orglink-list)))))
761 (kill-new (mapconcat 'identity orglink-list "\n"))))
763 (defun org-mac-devonthink-item-insert-selected ()
764 "Insert a link to the item(s) currently selected in DEVONthink Pro Office.
765 This will use AppleScript to get the `uuid'(s) and the name(s) of the
766 selected items in DEVONthink Pro Office and make link(s) out of it/them."
767 (interactive)
768 (insert (org-mac-devonthink-get-links)))
771 ;; Handle links from Mail.app
773 (org-add-link-type "message" 'org-mac-message-open)
775 (defun org-mac-message-open (message-id)
776 "Visit the message with MESSAGE-ID.
777 This will use the command `open' with the message URL."
778 (start-process (concat "open message:" message-id) nil
779 "open" (concat "message://<" (substring message-id 2) ">")))
781 (defun org-as-get-selected-mail ()
782 "AppleScript to create links to selected messages in Mail.app."
783 (do-applescript
784 (concat
785 "tell application \"Mail\"\n"
786 "set theLinkList to {}\n"
787 "set theSelection to selection\n"
788 "repeat with theMessage in theSelection\n"
789 "set theID to message id of theMessage\n"
790 "set theSubject to subject of theMessage\n"
791 "set theLink to \"message://\" & theID & \"::split::\" & theSubject\n"
792 "if (theLinkList is not equal to {}) then\n"
793 "set theLink to \"\n\" & theLink\n"
794 "end if\n"
795 "copy theLink to end of theLinkList\n"
796 "end repeat\n"
797 "return theLinkList as string\n"
798 "end tell")))
800 (defun org-as-get-flagged-mail ()
801 "AppleScript to create links to flagged messages in Mail.app."
802 (unless org-mac-mail-account
803 (error "You must set org-mac-mail-account"))
804 (do-applescript
805 (concat
806 ;; Get links
807 "tell application \"Mail\"\n"
808 "set theMailboxes to every mailbox of account \"" org-mac-mail-account "\"\n"
809 "set theLinkList to {}\n"
810 "repeat with aMailbox in theMailboxes\n"
811 "set theSelection to (every message in aMailbox whose flagged status = true)\n"
812 "repeat with theMessage in theSelection\n"
813 "set theID to message id of theMessage\n"
814 "set theSubject to subject of theMessage\n"
815 "set theLink to \"message://\" & theID & \"::split::\" & theSubject & \"\n\"\n"
816 "copy theLink to end of theLinkList\n"
817 "end repeat\n"
818 "end repeat\n"
819 "return theLinkList as string\n"
820 "end tell")))
822 (defun org-mac-message-get-links (&optional select-or-flag)
823 "Create links to the messages currently selected or flagged in Mail.app.
824 This will use AppleScript to get the message-id and the subject of the
825 messages in Mail.app and make a link out of it.
826 When SELECT-OR-FLAG is \"s\", get the selected messages (this is also
827 the default). When SELECT-OR-FLAG is \"f\", get the flagged messages.
828 The Org-syntax text will be pushed to the kill ring, and also returned."
829 (interactive "sLink to (s)elected or (f)lagged messages: ")
830 (setq select-or-flag (or select-or-flag "s"))
831 (message "AppleScript: searching mailboxes...")
832 (let* ((as-link-list
833 (if (string= select-or-flag "s")
834 (org-as-get-selected-mail)
835 (if (string= select-or-flag "f")
836 (org-as-get-flagged-mail)
837 (error "Please select \"s\" or \"f\""))))
838 (link-list
839 (mapcar
840 (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x) (setq x (match-string 1 x))) x)
841 (split-string (substring as-link-list 1 -1) "[\r\n]+")))
842 split-link URL description orglink orglink-insert rtn orglink-list)
843 (while link-list
844 (setq split-link (split-string (pop link-list) "::split::"))
845 (setq URL (car split-link))
846 (setq description (cadr split-link))
847 (when (not (string= URL ""))
848 (setq orglink (org-make-link-string URL description))
849 (push orglink orglink-list)))
850 (setq rtn (mapconcat 'identity orglink-list "\n"))
851 (kill-new rtn)
852 rtn))
854 (defun org-mac-message-insert-selected ()
855 "Insert a link to the messages currently selected in Mail.app.
856 This will use AppleScript to get the message-id and the subject of the
857 active mail in Mail.app and make a link out of it."
858 (interactive)
859 (insert (org-mac-message-get-links "s")))
861 ;; The following line is for backward compatibility
862 (defalias 'org-mac-message-insert-link 'org-mac-message-insert-selected)
864 (defun org-mac-message-insert-flagged (org-buffer org-heading)
865 "Asks for an org buffer and a heading within it, and replace message links.
866 If heading exists, delete all message:// links within heading's first
867 level. If heading doesn't exist, create it at point-max. Insert
868 list of message:// links to flagged mail after heading."
869 (interactive "bBuffer in which to insert links: \nsHeading after which to insert links: ")
870 (with-current-buffer org-buffer
871 (goto-char (point-min))
872 (let ((isearch-forward t)
873 (message-re "\\[\\[\\(message:\\)\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"))
874 (if (org-goto-local-search-headings org-heading nil t)
875 (if (not (eobp))
876 (progn
877 (save-excursion
878 (while (re-search-forward
879 message-re (save-excursion (outline-next-heading)) t)
880 (delete-region (match-beginning 0) (match-end 0)))
881 (insert "\n" (org-mac-message-get-links "f")))
882 (flush-lines "^$" (point) (outline-next-heading)))
883 (insert "\n" (org-mac-message-get-links "f")))
884 (goto-char (point-max))
885 (insert "\n")
886 (org-insert-heading nil t)
887 (insert org-heading "\n" (org-mac-message-get-links "f"))))))
890 (provide 'org-mac-link)
892 ;;; org-mac-link.el ends here