1 ;;; org-mac-link.el --- Grab links and url from various mac
2 ;; Application and insert them as links into org-mode documents
4 ;; Copyright (c) 2010-2014 Free Software Foundation, Inc.
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>
15 ;; Keywords: org, mac, hyperlink
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)
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/>.
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
47 ;; Detailed comments for each application interface are inlined with
48 ;; the code. Here is a brief overview of how the code interacts with
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
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)))
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.
81 ;; You may customize which applications appear in the grab menu by
82 ;; customizing the group `org-mac-link'. Changes take effect
90 (defgroup org-mac-link nil
91 "Options for grabbing links from Mac applications."
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"
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"
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"
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"
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"
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"
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"
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"
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"
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"
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"
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"
169 (defgroup org-mac-flagged-mail nil
170 "Options foring linking to flagged Mail.app messages."
174 (defcustom org-mac-mail-account
"customize"
175 "The Mail.app account in which to search for flagged messages."
176 :group
'org-mac-flagged-mail
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."
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
))
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) " "))))
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
))))
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>."
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
)
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"))
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 ()
275 "set oldClipboard to the clipboard\n"
276 "set frontmostApplication to path to frontmost application\n"
277 "tell application \"Firefox\"\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"
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"
290 "activate application (frontmostApplication as text)\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 ()
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
))
304 (when (not (string= URL
""))
305 (setq org-link
(org-make-link-string URL description
)))
309 (defun org-mac-firefox-insert-frontmost-url ()
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
318 (defun org-as-mac-vimperator-get-frontmost-url ()
322 "set oldClipboard to the clipboard\n"
323 "set frontmostApplication to path to frontmost application\n"
324 "tell application \"Firefox\"\n"
327 " tell application \"System Events\"\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"
335 "activate application (frontmostApplication as text)\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 ()
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
))
350 (when (not (string= URL
""))
351 (setq org-link
(org-make-link-string URL description
)))
355 (defun org-mac-vimperator-insert-frontmost-url ()
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 ()
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"
373 "activate application (frontmostApplication as text)\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 ()
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
))
388 (when (not (string= URL
""))
389 (setq org-link
(org-make-link-string URL description
)))
393 (defun org-mac-chrome-insert-frontmost-url ()
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 ()
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"
410 (car (split-string result
"[\r\n]+" t
))))
412 (defun org-mac-safari-get-frontmost-url ()
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
))
420 (when (not (string= URL
""))
421 (setq org-link
(org-make-link-string URL description
)))
425 (defun org-mac-safari-insert-frontmost-url ()
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 ()
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"
448 " return theLinkList as string\n"
451 (defun org-mac-together-get-selected ()
453 (message "Applescript: Getting Togther items...")
454 (org-mac-paste-applescript-links (as-get-selected-together-items)))
456 (defun org-mac-together-insert-selected ()
458 (insert (org-mac-together-get-selected)))
461 ;; Handle links from Finder.app
463 (defun as-get-selected-finder-items ()
466 "tell application \"Finder\"\n"
467 " set theSelection to the selection\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"
473 " return links as string\n"
476 (defun org-mac-finder-item-get-selected ()
478 (message "Applescript: Getting Finder items...")
479 (org-mac-paste-applescript-links (as-get-selected-finder-items)))
481 (defun org-mac-finder-insert-selected ()
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 ()
497 "tell application \"Address Book\"\n"
498 " set theSelection to the selection\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"
504 " return links as string\n"
507 (defun org-mac-addressbook-item-get-selected ()
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 ()
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))))
530 "tell application \"Skim\"\n"
532 "set theDoc to \"" document
"\"\n"
533 "set thePage to " page
"\n"
535 "go document 1 to page thePage of document 1\n"
538 (defun as-get-skim-page-link ()
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
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"
558 "set theLink to \"skim://\" & thePath & \"::\" & thePage & "
559 "\"::split::\" & theContent\n"
561 "return theLink as string\n")))
563 (defun org-mac-skim-get-page ()
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
))
571 (when (not (string= link
""))
572 (setq org-link
(org-make-link-string link description
)))
576 (defun org-mac-skim-insert-page ()
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"
589 "tell application \"Microsoft Outlook\"\n"
590 (format "open message id %s\n" (substring-no-properties msgid
))
594 (defun org-as-get-selected-outlook-mail ()
595 "AppleScript to create links to selected messages in Microsoft Outlook.app."
598 "tell application \"Microsoft Outlook\"\n"
599 "set msgCount to count current messages\n"
600 "if (msgCount < 1) then\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"
612 "return theLinkList as string\n"
615 (defun org-sh-get-flagged-outlook-mail ()
616 "Shell commands to create links to flagged messages in Microsoft Outlook.app."
624 (shell-command-to-string
625 (format "mdls -raw -name com_microsoft_outlook_recordID -name kMDItemDisplayName \"%s\"" x
))
630 (let ((coding-system-for-read (or file-name-coding-system
'utf-8
))
631 (coding-system-for-write 'utf-8
))
633 "mdfind com_microsoft_outlook_flagged==1"
636 (buffer-string) "\n" t
))
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...")
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\""))))
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
)
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"))
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
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
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
)
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))
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."
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"
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"
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
746 (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x
)
747 (setq x
(match-string 1 x
)))
749 (split-string as-link-list
"[\r\n]+"))
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
))
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."
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."
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"
795 "copy theLink to end of theLinkList\n"
797 "return theLinkList as string\n"
800 (defun org-as-get-flagged-mail ()
801 "AppleScript to create links to flagged messages in Mail.app."
804 ;; Is Growl installed?
805 "tell application \"System Events\"\n"
806 "set growlHelpers to the name of every process whose creator type contains \"GRRR\"\n"
807 "if (count of growlHelpers) > 0 then\n"
808 "set growlHelperApp to item 1 of growlHelpers\n"
810 "set growlHelperApp to \"\"\n"
815 "tell application \"Mail\"\n"
816 "set theMailboxes to every mailbox of account \"" org-mac-mail-account
"\"\n"
817 "set theLinkList to {}\n"
818 "repeat with aMailbox in theMailboxes\n"
819 "set theSelection to (every message in aMailbox whose flagged status = true)\n"
820 "repeat with theMessage in theSelection\n"
821 "set theID to message id of theMessage\n"
822 "set theSubject to subject of theMessage\n"
823 "set theLink to \"message://\" & theID & \"::split::\" & theSubject & \"\n\"\n"
824 "copy theLink to end of theLinkList\n"
826 ;; Report progress through Growl
827 ;; This "double tell" idiom is described in detail at
828 ;; http://macscripter.net/viewtopic.php?id=24570 The
829 ;; script compiler needs static knowledge of the
830 ;; growlHelperApp. Hmm, since we're compiling
831 ;; on-the-fly here, this is likely to be way less
832 ;; portable than I'd hoped. It'll work when the name
833 ;; is still "GrowlHelperApp", though.
834 "if growlHelperApp is not \"\" then\n"
835 "tell application \"GrowlHelperApp\"\n"
836 "tell application growlHelperApp\n"
837 "set the allNotificationsList to {\"FlaggedMail\"}\n"
838 "set the enabledNotificationsList to allNotificationsList\n"
839 "register as application \"FlaggedMail\" all notifications allNotificationsList default notifications enabledNotificationsList icon of application \"Mail\"\n"
840 "notify with name \"FlaggedMail\" title \"Importing flagged message\" description theSubject application name \"FlaggedMail\"\n"
846 "return theLinkList as string\n"
849 (defun org-mac-message-get-links (&optional select-or-flag
)
850 "Create links to the messages currently selected or flagged in Mail.app.
851 This will use AppleScript to get the message-id and the subject of the
852 messages in Mail.app and make a link out of it.
853 When SELECT-OR-FLAG is \"s\", get the selected messages (this is also
854 the default). When SELECT-OR-FLAG is \"f\", get the flagged messages.
855 The Org-syntax text will be pushed to the kill ring, and also returned."
856 (interactive "sLink to (s)elected or (f)lagged messages: ")
857 (setq select-or-flag
(or select-or-flag
"s"))
858 (message "AppleScript: searching mailboxes...")
860 (if (string= select-or-flag
"s")
861 (org-as-get-selected-mail)
862 (if (string= select-or-flag
"f")
863 (org-as-get-flagged-mail)
864 (error "Please select \"s\" or \"f\""))))
867 (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x
) (setq x
(match-string 1 x
))) x
)
868 (split-string (substring as-link-list
1 -
1) "[\r\n]+")))
869 split-link URL description orglink orglink-insert rtn orglink-list
)
871 (setq split-link
(split-string (pop link-list
) "::split::"))
872 (setq URL
(car split-link
))
873 (setq description
(cadr split-link
))
874 (when (not (string= URL
""))
875 (setq orglink
(org-make-link-string URL description
))
876 (push orglink orglink-list
)))
877 (setq rtn
(mapconcat 'identity orglink-list
"\n"))
881 (defun org-mac-message-insert-selected ()
882 "Insert a link to the messages currently selected in Mail.app.
883 This will use AppleScript to get the message-id and the subject of the
884 active mail in Mail.app and make a link out of it."
886 (insert (org-mac-message-get-links "s")))
888 ;; The following line is for backward compatibility
889 (defalias 'org-mac-message-insert-link
'org-mac-message-insert-selected
)
891 (defun org-mac-message-insert-flagged (org-buffer org-heading
)
892 "Asks for an org buffer and a heading within it, and replace message links.
893 If heading exists, delete all message:// links within heading's first
894 level. If heading doesn't exist, create it at point-max. Insert
895 list of message:// links to flagged mail after heading."
896 (interactive "bBuffer in which to insert links: \nsHeading after which to insert links: ")
897 (with-current-buffer org-buffer
898 (goto-char (point-min))
899 (let ((isearch-forward t
)
900 (message-re "\\[\\[\\(message:\\)\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"))
901 (if (org-goto-local-search-headings org-heading nil t
)
905 (while (re-search-forward
906 message-re
(save-excursion (outline-next-heading)) t
)
907 (delete-region (match-beginning 0) (match-end 0)))
908 (insert "\n" (org-mac-message-get-links "f")))
909 (flush-lines "^$" (point) (outline-next-heading)))
910 (insert "\n" (org-mac-message-get-links "f")))
911 (goto-char (point-max))
913 (org-insert-heading nil t
)
914 (insert org-heading
"\n" (org-mac-message-get-links "f"))))))
917 (provide 'org-mac-link
)
919 ;;; org-mac-link.el ends here