1 ;;; org-mac-link.el --- Insert org-mode links to items selected in various Mac apps
3 ;; Copyright (c) 2010-2018 Free Software Foundation, Inc.
5 ;; Author: Anthony Lander <anthony.lander@gmail.com>
6 ;; John Wiegley <johnw@gnu.org>
7 ;; Christopher Suckling <suckling at gmail dot com>
8 ;; Daniil Frumin <difrumin@gmail.com>
9 ;; Alan Schmitt <alan.schmitt@polytechnique.org>
10 ;; Mike McLean <mike.mclean@pobox.com>
14 ;; Keywords: org, mac, hyperlink
18 ;; Author: Mike McLean <mike.mclean@pobox.com>
19 ;; Add support for Microsoft Outlook for Mac as Org mode links
22 ;; Author: Alan Schmitt <alan.schmitt@polytechnique.org>
23 ;; Consistently use `org-mac-paste-applescript-links'
26 ;; Author: Mike McLean <mike.mclean@pobox.com>
27 ;; Make the path to Microsoft Outlook a `defcustom'
30 ;; Author: Mike McLean <mike.mclean@pobox.com>
31 ;; Add Support for Evernote
33 ;; This file is not part of GNU Emacs.
35 ;; This program is free software; you can redistribute it and/or modify
36 ;; it under the terms of the GNU General Public License as published by
37 ;; the Free Software Foundation; either version 3, or (at your option)
40 ;; This program is distributed in the hope that it will be useful,
41 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
42 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 ;; GNU General Public License for more details.
45 ;; You should have received a copy of the GNU General Public License
46 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
50 ;; This code allows you to grab either the current selected items, or
51 ;; the frontmost url in various mac appliations, and insert them as
52 ;; hyperlinks into the current org-mode document at point.
54 ;; This code is heavily based on, and indeed incorporates,
55 ;; org-mac-message.el written by John Wiegley and Christopher
58 ;; Detailed comments for each application interface are inlined with
59 ;; the code. Here is a brief overview of how the code interacts with
62 ;; Finder.app - grab links to the selected files in the frontmost window
63 ;; Mail.app - grab links to the selected messages in the message list
64 ;; AddressBook.app - Grab links to the selected addressbook Cards
65 ;; Firefox.app - Grab the url of the frontmost tab in the frontmost window
66 ;; Vimperator/Firefox.app - Grab the url of the frontmost tab in the frontmost window
67 ;; Safari.app - Grab the url of the frontmost tab in the frontmost window
68 ;; Google Chrome.app - Grab the url of the frontmost tab in the frontmost window
69 ;; Brave.app - Grab the url of the frontmost tab in the frontmost window
70 ;; Together.app - Grab links to the selected items in the library list
71 ;; Skim.app - Grab a link to the selected page in the topmost pdf document
72 ;; Microsoft Outlook.app - Grab a link to the selected message in the message list
73 ;; DEVONthink Pro Office.app - Grab a link to the selected DEVONthink item(s); open DEVONthink item by reference
74 ;; Evernote.app - Grab a link to the selected Evernote item(s); open Evernote item by ID
79 ;; add (require 'org-mac-link) to your .emacs, and optionally bind a
80 ;; key to activate the link grabber menu, like this:
82 ;; (add-hook 'org-mode-hook (lambda ()
83 ;; (define-key org-mode-map (kbd "C-c g") 'org-mac-grab-link)))
87 ;; Type C-c g (or whatever key you defined, as above), or type M-x
88 ;; org-mac-grab-link RET to activate the link grabber. This will present
89 ;; you with a menu to choose an application from which to grab a link
90 ;; to insert at point. You may also type C-g to abort.
94 ;; You may customize which applications appear in the grab menu by
95 ;; customizing the group `org-mac-link'. Changes take effect
103 (defgroup org-mac-link nil
104 "Options for grabbing links from Mac applications."
108 (defcustom org-mac-grab-Finder-app-p t
109 "Add menu option [F]inder to grab links from the Finder."
110 :tag
"Grab Finder.app links"
114 (defcustom org-mac-grab-Mail-app-p t
115 "Add menu option [m]ail to grab links from Mail.app."
116 :tag
"Grab Mail.app links"
120 (defcustom org-mac-grab-Outlook-app-p t
121 "Add menu option [o]utlook to grab links from Microsoft Outlook.app."
122 :tag
"Grab Microsoft Outlook.app links"
126 (defcustom org-mac-outlook-path
"/Applications/Microsoft Outlook.app"
127 "The path to the installed copy of Microsoft Outlook.app. Do not escape spaces as the AppleScript call will quote this string."
128 :tag
"Path to Microsoft Outlook"
132 (defcustom org-mac-grab-devonthink-app-p t
133 "Add menu option [d]EVONthink to grab links from DEVONthink Pro Office.app."
134 :tag
"Grab DEVONthink Pro Office.app links"
138 (defcustom org-mac-grab-Addressbook-app-p t
139 "Add menu option [a]ddressbook to grab links from AddressBook.app."
140 :tag
"Grab AddressBook.app links"
144 (defcustom org-mac-grab-Safari-app-p t
145 "Add menu option [s]afari to grab links from Safari.app."
146 :tag
"Grab Safari.app links"
150 (defcustom org-mac-grab-Firefox-app-p t
151 "Add menu option [f]irefox to grab links from Firefox.app."
152 :tag
"Grab Firefox.app links"
156 (defcustom org-mac-grab-Firefox
+Vimperator-p nil
157 "Add menu option [v]imperator to grab links from Firefox.app running the Vimperator plugin."
158 :tag
"Grab Vimperator/Firefox.app links"
162 (defcustom org-mac-grab-Chrome-app-p t
163 "Add menu option [c]hrome to grab links from Google Chrome.app."
164 :tag
"Grab Google Chrome.app links"
168 (defcustom org-mac-grab-Brave-app-p t
169 "Add menu option [b]rave to grab links from Brave.app."
170 :tag
"Grab Brave.app links"
174 (defcustom org-mac-grab-Together-app-p nil
175 "Add menu option [t]ogether to grab links from Together.app."
176 :tag
"Grab Together.app links"
180 (defcustom org-mac-grab-Skim-app-p
181 (< 0 (length (shell-command-to-string
182 "mdfind kMDItemCFBundleIdentifier == 'net.sourceforge.skim-app.skim'")))
183 "Add menu option [S]kim to grab page links from Skim.app."
184 :tag
"Grab Skim.app page links"
188 (defcustom org-mac-Skim-highlight-selection-p nil
189 "Highlight the active selection when grabbing a link from Skim.app."
190 :tag
"Highlight selection in Skim.app"
194 (defcustom org-mac-grab-Acrobat-app-p t
195 "Add menu option [A]crobat to grab page links from Acrobat.app."
196 :tag
"Grab Acrobat.app page links"
200 (defgroup org-mac-flagged-mail nil
201 "Options foring linking to flagged Mail.app messages."
205 (defcustom org-mac-mail-account nil
206 "The Mail.app account in which to search for flagged messages."
207 :group
'org-mac-flagged-mail
210 (defcustom org-mac-grab-Evernote-app-p
211 (< 0 (length (shell-command-to-string
212 "mdfind kMDItemCFBundleIdentifier == 'com.evernote.Evernote'")))
213 "Add menu option [e]vernote to grab note links from Evernote.app."
214 :tag
"Grab Evernote.app note links"
218 (defcustom org-mac-evernote-path
(replace-regexp-in-string (rx (* (any " \t\n")) eos
)
220 (shell-command-to-string
221 "mdfind kMDItemCFBundleIdentifier == 'com.evernote.Evernote'"))
222 "The path to the installed copy of Evernote.app. Do not escape spaces as the AppleScript call will quote this string."
223 :tag
"Path to Evernote"
228 ;; In mac.c, removed in Emacs 23.
229 (declare-function do-applescript
"org-mac-message" (script))
230 (unless (fboundp 'do-applescript
)
231 ;; Need to fake this using shell-command-to-string
232 (defun do-applescript (script)
233 (let (start cmd return
)
234 (while (string-match "\n" script
)
235 (setq script
(replace-match "\r" t t script
)))
236 (while (string-match "'" script start
)
237 (setq start
(+ 2 (match-beginning 0))
238 script
(replace-match "\\'" t t script
)))
239 (setq cmd
(concat "osascript -e '" script
"'"))
240 (setq return
(shell-command-to-string cmd
))
241 (concat "\"" (org-trim return
) "\""))))
244 (defun org-mac-grab-link ()
245 "Prompt for an application to grab a link from.
246 When done, go grab the link, and insert it at point."
249 `(("F" "inder" org-mac-finder-insert-selected
,org-mac-grab-Finder-app-p
)
250 ("m" "ail" org-mac-message-insert-selected
,org-mac-grab-Mail-app-p
)
251 ("d" "EVONthink Pro Office" org-mac-devonthink-item-insert-selected
252 ,org-mac-grab-devonthink-app-p
)
253 ("o" "utlook" org-mac-outlook-message-insert-selected
,org-mac-grab-Outlook-app-p
)
254 ("a" "ddressbook" org-mac-addressbook-insert-selected
,org-mac-grab-Addressbook-app-p
)
255 ("s" "afari" org-mac-safari-insert-frontmost-url
,org-mac-grab-Safari-app-p
)
256 ("f" "irefox" org-mac-firefox-insert-frontmost-url
,org-mac-grab-Firefox-app-p
)
257 ("v" "imperator" org-mac-vimperator-insert-frontmost-url
,org-mac-grab-Firefox
+Vimperator-p
)
258 ("c" "hrome" org-mac-chrome-insert-frontmost-url
,org-mac-grab-Chrome-app-p
)
259 ("b" "rave" org-mac-brave-insert-frontmost-url
,org-mac-grab-Brave-app-p
)
260 ("e" "evernote" org-mac-evernote-note-insert-selected
,org-mac-grab-Evernote-app-p
)
261 ("t" "ogether" org-mac-together-insert-selected
,org-mac-grab-Together-app-p
)
262 ("S" "kim" org-mac-skim-insert-page
,org-mac-grab-Skim-app-p
)
263 ("A" "crobat" org-mac-acrobat-insert-page
,org-mac-grab-Acrobat-app-p
)))
264 (menu-string (make-string 0 ?x
))
267 ;; Create the menu string for the keymap
268 (mapc (lambda (descriptor)
269 (when (elt descriptor
3)
270 (setf menu-string
(concat menu-string
271 "[" (elt descriptor
0) "]"
272 (elt descriptor
1) " "))))
274 (setf (elt menu-string
(- (length menu-string
) 1)) ?
:)
276 ;; Prompt the user, and grab the link
277 (message menu-string
)
278 (setq input
(read-char-exclusive))
279 (mapc (lambda (descriptor)
280 (let ((key (elt (elt descriptor
0) 0))
281 (active (elt descriptor
3))
282 (grab-function (elt descriptor
2)))
283 (when (and active
(eq input key
))
284 (call-interactively grab-function
))))
287 (defun org-mac-paste-applescript-links (as-link-list)
288 "Paste in a list of links from an applescript handler.
289 The links are of the form <link>::split::<name>."
290 (let* ((noquote-as-link-list
291 (if (string-prefix-p "\"" as-link-list
)
292 (substring as-link-list
1 -
1)
295 (mapcar (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x
)
296 (setq x
(match-string 1 x
)))
298 (split-string noquote-as-link-list
"[\r\n]+")))
299 split-link URL description orglink orglink-insert rtn orglink-list
)
301 (setq split-link
(split-string (pop link-list
) "::split::"))
302 (setq URL
(car split-link
))
303 (setq description
(cadr split-link
))
304 (when (not (string= URL
""))
305 (setq orglink
(org-make-link-string URL description
))
306 (push orglink orglink-list
)))
307 (setq rtn
(mapconcat 'identity orglink-list
"\n"))
312 ;; Handle links from Firefox.app
314 ;; This code allows you to grab the current active url from the main
315 ;; Firefox.app window, and insert it as a link into an org-mode
316 ;; document. Unfortunately, firefox does not expose an applescript
317 ;; dictionary, so this is necessarily introduces some limitations.
319 ;; The applescript to grab the url from Firefox.app uses the System
320 ;; Events application to give focus to the firefox application, select
321 ;; the contents of the url bar, and copy it. It then uses the title of
322 ;; the window as the text of the link. There is no way to grab links
323 ;; from other open tabs, and further, if there is more than one window
324 ;; open, it is not clear which one will be used (though emperically it
325 ;; seems that it is always the last active window).
327 (defun org-as-mac-firefox-get-frontmost-url ()
331 "set oldClipboard to the clipboard\n"
332 "set frontmostApplication to path to frontmost application\n"
333 "tell application \"Firefox\"\n"
336 " tell application \"System Events\"\n"
337 " keystroke \"l\" using {command down}\n"
338 " keystroke \"a\" using {command down}\n"
339 " keystroke \"c\" using {command down}\n"
342 " set theUrl to the clipboard\n"
343 " set the clipboard to oldClipboard\n"
344 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
346 "activate application (frontmostApplication as text)\n"
348 "copy theResult to the end of links\n"
349 "return links as string\n"))))
350 (car (split-string result
"[\r\n]+" t
))))
353 (defun org-mac-firefox-get-frontmost-url ()
355 (message "Applescript: Getting Firefox url...")
356 (org-mac-paste-applescript-links (org-as-mac-firefox-get-frontmost-url)))
359 (defun org-mac-firefox-insert-frontmost-url ()
361 (insert (org-mac-firefox-get-frontmost-url)))
364 ;; Handle links from Google Firefox.app running the Vimperator extension
365 ;; Grab the frontmost url from Firefox+Vimperator. Same limitations are
368 (defun org-as-mac-vimperator-get-frontmost-url ()
372 "set oldClipboard to the clipboard\n"
373 "set frontmostApplication to path to frontmost application\n"
374 "tell application \"Firefox\"\n"
377 " tell application \"System Events\"\n"
381 " set theUrl to the clipboard\n"
382 " set the clipboard to oldClipboard\n"
383 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
385 "activate application (frontmostApplication as text)\n"
387 "copy theResult to the end of links\n"
388 "return links as string\n"))))
389 (replace-regexp-in-string
390 "\s+-\s+Vimperator" "" (car (split-string result
"[\r\n]+" t
)))))
393 (defun org-mac-vimperator-get-frontmost-url ()
395 (message "Applescript: Getting Vimperator url...")
396 (org-mac-paste-applescript-links (org-as-mac-vimperator-get-frontmost-url)))
399 (defun org-mac-vimperator-insert-frontmost-url ()
401 (insert (org-mac-vimperator-get-frontmost-url)))
404 ;; Handle links from Google Chrome.app
405 ;; Grab the frontmost url from Google Chrome. Same limitations as
406 ;; Firefox because Chrome doesn't publish an Applescript dictionary
408 (defun org-as-mac-chrome-get-frontmost-url ()
412 "set frontmostApplication to path to frontmost application\n"
413 "tell application \"Google Chrome\"\n"
414 " set theUrl to get URL of active tab of first window\n"
415 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
417 "activate application (frontmostApplication as text)\n"
419 "copy theResult to the end of links\n"
420 "return links as string\n"))))
421 (replace-regexp-in-string
422 "^\"\\|\"$" "" (car (split-string result
"[\r\n]+" t
)))))
425 (defun org-mac-chrome-get-frontmost-url ()
427 (message "Applescript: Getting Chrome url...")
428 (org-mac-paste-applescript-links (org-as-mac-chrome-get-frontmost-url)))
431 (defun org-mac-chrome-insert-frontmost-url ()
433 (insert (org-mac-chrome-get-frontmost-url)))
436 ;; Handle links from Brave.app
437 ;; Grab the frontmost url from Brave. Same limitations as
438 ;; Firefox/Chrome because Brave doesn't publish an Applescript
441 (defun org-as-mac-brave-get-frontmost-url ()
445 "set frontmostApplication to path to frontmost application\n"
446 "tell application \"Brave\"\n"
447 " set theUrl to get URL of active tab of first window\n"
448 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
450 "activate application (frontmostApplication as text)\n"
452 "copy theResult to the end of links\n"
453 "return links as string\n"))))
454 (replace-regexp-in-string
455 "^\"\\|\"$" "" (car (split-string result
"[\r\n]+" t
)))))
458 (defun org-mac-brave-get-frontmost-url ()
460 (message "Applescript: Getting Brave url...")
461 (org-mac-paste-applescript-links (org-as-mac-brave-get-frontmost-url)))
464 (defun org-mac-brave-insert-frontmost-url ()
466 (insert (org-mac-brave-get-frontmost-url)))
469 ;; Handle links from Safari.app
470 ;; Grab the frontmost url from Safari.
472 (defun org-as-mac-safari-get-frontmost-url ()
475 "tell application \"Safari\"\n"
476 " set theUrl to URL of document 1\n"
477 " set theName to the name of the document 1\n"
478 " return theUrl & \"::split::\" & theName & \"\n\"\n"
482 (defun org-mac-safari-get-frontmost-url ()
484 (message "Applescript: Getting Safari url...")
485 (org-mac-paste-applescript-links
486 (org-as-mac-safari-get-frontmost-url)))
489 (defun org-mac-safari-insert-frontmost-url ()
491 (insert (org-mac-safari-get-frontmost-url)))
494 ;; Handle links from together.app
495 (org-link-set-parameters "x-together-item" :follow
#'org-mac-together-item-open
)
497 (defun org-mac-together-item-open (uid)
498 "Open UID, which is a reference to an item in Together."
499 (shell-command (concat "open -a Together \"x-together-item:" uid
"\"")))
501 (defun as-get-selected-together-items ()
504 "tell application \"Together\"\n"
505 " set theLinkList to {}\n"
506 " set theSelection to selected items\n"
507 " repeat with theItem in theSelection\n"
508 " set theLink to (get item link of theItem) & \"::split::\" & (get name of theItem) & \"\n\"\n"
509 " copy theLink to end of theLinkList\n"
511 " return theLinkList as string\n"
515 (defun org-mac-together-get-selected ()
517 (message "Applescript: Getting Togther items...")
518 (org-mac-paste-applescript-links (as-get-selected-together-items)))
521 (defun org-mac-together-insert-selected ()
523 (insert (org-mac-together-get-selected)))
526 ;; Handle links from Finder.app
528 (defun as-get-selected-finder-items ()
531 "tell application \"Finder\"\n"
532 " set theSelection to the selection\n"
534 " repeat with theItem in theSelection\n"
535 " set theLink to \"file://\" & (POSIX path of (theItem as string)) & \"::split::\" & (get the name of theItem) & \"\n\"\n"
536 " copy theLink to the end of links\n"
538 " return links as string\n"
542 (defun org-mac-finder-item-get-selected ()
544 (message "Applescript: Getting Finder items...")
545 (org-mac-paste-applescript-links (as-get-selected-finder-items)))
548 (defun org-mac-finder-insert-selected ()
550 (insert (org-mac-finder-item-get-selected)))
553 ;; Handle links from AddressBook.app
554 (org-link-set-parameters "addressbook" :follow
#'org-mac-addressbook-item-open
)
556 (defun org-mac-addressbook-item-open (uid)
557 "Open UID, which is a reference to an item in the addressbook."
558 (shell-command (concat "open \"addressbook:" uid
"\"")))
560 (defun as-get-selected-addressbook-items ()
563 "tell application \"Address Book\"\n"
564 " set theSelection to the selection\n"
566 " repeat with theItem in theSelection\n"
567 " set theLink to \"addressbook://\" & (the id of theItem) & \"::split::\" & (the name of theItem) & \"\n\"\n"
568 " copy theLink to the end of links\n"
570 " return links as string\n"
574 (defun org-mac-addressbook-item-get-selected ()
576 (message "Applescript: Getting Address Book items...")
577 (org-mac-paste-applescript-links (as-get-selected-addressbook-items)))
580 (defun org-mac-addressbook-insert-selected ()
582 (insert (org-mac-addressbook-item-get-selected)))
585 ;; Handle links from Skim.app
587 ;; Original code & idea by Christopher Suckling (org-mac-protocol)
589 (org-link-set-parameters "skim" :follow
#'org-mac-skim-open
)
591 (defun org-mac-skim-open (uri)
592 "Visit page of pdf in Skim"
593 (let* ((page (when (string-match "::\\(.+\\)\\'" uri
)
594 (match-string 1 uri
)))
595 (document (substring uri
0 (match-beginning 0))))
598 "tell application \"Skim\"\n"
600 "set theDoc to \"" document
"\"\n"
601 "set thePage to " page
"\n"
603 "go document 1 to page thePage of document 1\n"
606 (defun as-get-skim-page-link ()
609 "tell application \"Skim\"\n"
610 "set theDoc to front document\n"
611 "set theTitle to (name of theDoc)\n"
612 "set thePath to (path of theDoc)\n"
613 "set thePage to (get index for current page of theDoc)\n"
614 "set theSelection to selection of theDoc\n"
615 "set theContent to contents of (get text for theSelection)\n"
616 "if theContent is missing value then\n"
617 " set theContent to theTitle & \", p. \" & thePage\n"
618 (when org-mac-Skim-highlight-selection-p
622 " set theNote to make note with properties {type:highlight note, selection:theSelection}\n"
623 " set text of theNote to (get text for theSelection)\n"
626 "set theLink to \"skim://\" & thePath & \"::\" & thePage & "
627 "\"::split::\" & theContent\n"
629 "return theLink as string\n")))
632 (defun org-mac-skim-get-page ()
634 (message "Applescript: Getting Skim page link...")
635 (org-mac-paste-applescript-links (as-get-skim-page-link)))
638 (defun org-mac-skim-insert-page ()
640 (insert (org-mac-skim-get-page)))
642 ;; Handle links from Adobe Acrobat Pro.app
644 ;; Original code & idea by Christopher Suckling (org-mac-protocol)
646 ;; The URI format is path_to_pdf_file::page_number
648 (org-link-set-parameters "acrobat" :follow
#'org-mac-acrobat-open
)
650 (defun org-mac-acrobat-open (uri)
651 "Visit page of pdf in Acrobat"
652 (let* ((page (when (string-match "::\\(.+\\)\\'" uri
)
653 (match-string 1 uri
)))
654 (document (substring uri
0 (match-beginning 0))))
657 "tell application \"Adobe Acrobat Pro\"\n"
659 " set theDoc to \"" document
"\"\n"
660 " set thePage to " page
"\n"
662 " tell PDF Window 1\n"
663 " goto page thePage\n"
667 ;; The applescript returns link in the format
668 ;; "adobe:path_to_pdf_file::page_number::split::document_title, p.page_label"
670 (defun org-mac-as-get-acrobat-page-link ()
673 "tell application \"Adobe Acrobat Pro\"\n"
674 " set theDoc to active doc\n"
675 " set theWindow to (PDF Window 1 of theDoc)\n"
676 " set thePath to (file alias of theDoc)\n"
677 " set theTitle to (name of theWindow)\n"
678 " set thePage to (page number of theWindow)\n"
679 " set theLabel to (label text of (page thePage of theWindow))\n"
681 "set theResult to \"acrobat:\" & thePath & \"::\" & thePage & \"::split::\" & theTitle & \", p.\" & theLabel\n"
682 "return theResult as string\n")))
685 (defun org-mac-acrobat-get-page ()
687 (message "Applescript: Getting Acrobat page link...")
688 (org-mac-paste-applescript-links (org-mac-as-get-acrobat-page-link)))
691 (defun org-mac-acrobat-insert-page ()
693 (insert (org-mac-acrobat-get-page)))
696 ;; Handle links from Microsoft Outlook.app
698 (org-link-set-parameters "mac-outlook" :follow
#'org-mac-outlook-message-open
)
700 (defun org-mac-outlook-message-open (msgid)
701 "Open a message in Outlook"
704 "tell application \"" org-mac-outlook-path
"\"\n"
705 (format "open message id %s\n" (substring-no-properties msgid
))
709 (defun org-as-get-selected-outlook-mail ()
710 "AppleScript to create links to selected messages in Microsoft Outlook.app."
713 "tell application \"" org-mac-outlook-path
"\"\n"
714 "set msgCount to count current messages\n"
715 "if (msgCount < 1) then\n"
718 "set theLinkList to {}\n"
719 "set theSelection to (get current messages)\n"
720 "repeat with theMessage in theSelection\n"
721 "set theID to id of theMessage as string\n"
722 "set theURL to \"mac-outlook:\" & theID\n"
723 "set theSubject to subject of theMessage\n"
724 "set theLink to theURL & \"::split::\" & theSubject & \"\n\"\n"
725 "copy theLink to end of theLinkList\n"
727 "return theLinkList as string\n"
730 (defun org-sh-get-flagged-outlook-mail ()
731 "Shell commands to create links to flagged messages in Microsoft Outlook.app."
739 (shell-command-to-string
740 (format "mdls -raw -name com_microsoft_outlook_recordID -name kMDItemDisplayName \"%s\"" x
))
745 (let ((coding-system-for-read (or file-name-coding-system
'utf-8
))
746 (coding-system-for-write 'utf-8
))
748 "mdfind com_microsoft_outlook_flagged==1"
751 (buffer-string) "\n" t
))
755 (defun org-mac-outlook-message-get-links (&optional select-or-flag
)
756 "Create links to the messages currently selected or flagged in Microsoft Outlook.app.
757 This will use AppleScript to get the message-id and the subject of the
758 messages in Microsoft Outlook.app and make a link out of it.
759 When SELECT-OR-FLAG is \"s\", get the selected messages (this is also
760 the default). When SELECT-OR-FLAG is \"f\", get the flagged messages.
761 The Org-syntax text will be pushed to the kill ring, and also returned."
762 (interactive "sLink to (s)elected or (f)lagged messages: ")
763 (setq select-or-flag
(or select-or-flag
"s"))
764 (message "Org Mac Outlook: searching mailboxes...")
765 (org-mac-paste-applescript-links
766 (if (string= select-or-flag
"s")
767 (org-as-get-selected-outlook-mail)
768 (if (string= select-or-flag
"f")
769 (org-sh-get-flagged-outlook-mail)
770 (error "Please select \"s\" or \"f\"")))))
773 (defun org-mac-outlook-message-insert-selected ()
774 "Insert a link to the messages currently selected in Microsoft Outlook.app.
775 This will use AppleScript to get the message-id and the subject
776 of the active mail in Microsoft Outlook.app and make a link out
779 (insert (org-mac-outlook-message-get-links "s")))
782 (defun org-mac-outlook-message-insert-flagged (org-buffer org-heading
)
783 "Asks for an org buffer and a heading within it, and replace message links.
784 If heading exists, delete all mac-outlook:// links within
785 heading's first level. If heading doesn't exist, create it at
786 point-max. Insert list of mac-outlook:// links to flagged mail
788 (interactive "bBuffer in which to insert links: \nsHeading after which to insert links: ")
789 (with-current-buffer org-buffer
790 (goto-char (point-min))
791 (let ((isearch-forward t
)
792 (message-re "\\[\\[\\(mac-outlook:\\)\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"))
793 (if (org-goto-local-search-headings org-heading nil t
)
797 (while (re-search-forward
798 message-re
(save-excursion (outline-next-heading)) t
)
799 (delete-region (match-beginning 0) (match-end 0)))
800 (insert "\n" (org-mac-outlook-message-get-links "f")))
801 (flush-lines "^$" (point) (outline-next-heading)))
802 (insert "\n" (org-mac-outlook-message-get-links "f")))
803 (goto-char (point-max))
805 (org-insert-heading nil t
)
806 (insert org-heading
"\n" (org-mac-outlook-message-get-links "f"))))))
808 ;; Handle links from Evernote.app
810 (org-link-set-parameters "mac-evernote" :follow
#'org-mac-evernote-note-open
)
812 (defun org-mac-evernote-note-open (noteid)
813 "Open a note in Evernote"
816 "tell application \"" org-mac-evernote-path
"\"\n"
817 " set theNotes to get every note of every notebook where its local id is \"" (substring-no-properties noteid
) "\"\n"
818 " repeat with _note in theNotes\n"
819 " if length of _note is not 0 then\n"
820 " set _selectedNote to _note\n"
823 " open note window with item 1 of _selectedNote\n"
827 (defun org-as-get-selected-evernote-notes ()
828 "AppleScript to create links to selected notes in Evernote.app."
831 "tell application \"" org-mac-evernote-path
"\"\n"
832 " set noteCount to count selection\n"
833 " if (noteCount < 1) then\n"
836 " set theLinkList to {}\n"
837 " set theSelection to selection\n"
838 " repeat with theNote in theSelection\n"
839 " set theTitle to title of theNote\n"
840 " set theID to local id of theNote\n"
841 " set theURL to \"mac-evernote:\" & theID\n"
842 " set theLink to theURL & \"::split::\" & theTitle & \"\n\"\n"
843 " copy theLink to end of theLinkList\n"
845 " return theLinkList as string\n"
849 (defun org-mac-evernote-note-insert-selected ()
850 "Insert a link to the notes currently selected in Evernote.app.
851 This will use AppleScript to get the note id and the title of the
852 note(s) in Evernote.app and make a link out of it/them."
854 (message "Org Mac Evernote: searching notes...")
855 (insert (org-mac-paste-applescript-links
856 (org-as-get-selected-evernote-notes))))
859 ;; Handle links from DEVONthink Pro Office.app
861 (org-link-set-parameters "x-devonthink-item" :follow
#'org-devonthink-item-open
)
863 (defun org-devonthink-item-open (uid)
864 "Open UID, which is a reference to an item in DEVONthink Pro Office."
865 (shell-command (concat "open \"x-devonthink-item:" uid
"\"")))
867 (defun org-as-get-selected-devonthink-item ()
868 "AppleScript to create links to selected items in DEVONthink Pro Office.app."
871 "set theLinkList to {}\n"
872 "tell application \"DEVONthink Pro\"\n"
873 "set selectedRecords to selection\n"
874 "set selectionCount to count of selectedRecords\n"
875 "if (selectionCount < 1) then\n"
878 "repeat with theRecord in selectedRecords\n"
879 "set theID to uuid of theRecord\n"
880 "set theURL to \"x-devonthink-item:\" & theID\n"
881 "set theSubject to name of theRecord\n"
882 "set theLink to theURL & \"::split::\" & theSubject & \"\n\"\n"
883 "copy theLink to end of theLinkList\n"
886 "return theLinkList as string"
889 (defun org-mac-devonthink-get-links ()
890 "Create links to the item(s) currently selected in DEVONthink Pro Office.
891 This will use AppleScript to get the `uuid' and the `name' of the
892 selected items in DEVONthink Pro Office.app and make links out of
893 it/them. This function will push the Org-syntax text to the kill
894 ring, and also return it."
895 (message "Org Mac DEVONthink: looking for selected items...")
896 (org-mac-paste-applescript-links (org-as-get-selected-devonthink-item)))
899 (defun org-mac-devonthink-item-insert-selected ()
900 "Insert a link to the item(s) currently selected in DEVONthink Pro Office.
901 This will use AppleScript to get the `uuid'(s) and the name(s) of the
902 selected items in DEVONthink Pro Office and make link(s) out of it/them."
904 (insert (org-mac-devonthink-get-links)))
907 ;; Handle links from Mail.app
909 (org-link-set-parameters "message" :follow
#'org-mac-message-open
)
911 (defun org-mac-message-open (message-id)
912 "Visit the message with MESSAGE-ID.
913 This will use the command `open' with the message URL."
914 (start-process (concat "open message:" message-id
) nil
915 "open" (concat "message://<" (substring message-id
2) ">")))
917 (defun org-as-get-selected-mail ()
918 "AppleScript to create links to selected messages in Mail.app."
921 "tell application \"Mail\"\n"
922 "set theLinkList to {}\n"
923 "set theSelection to selection\n"
924 "repeat with theMessage in theSelection\n"
925 "set theID to message id of theMessage\n"
926 "set theSubject to subject of theMessage\n"
927 "set theLink to \"message://\" & theID & \"::split::\" & theSubject\n"
928 "if (theLinkList is not equal to {}) then\n"
929 "set theLink to \"\n\" & theLink\n"
931 "copy theLink to end of theLinkList\n"
933 "return theLinkList as string\n"
936 (defun org-as-get-flagged-mail ()
937 "AppleScript to create links to flagged messages in Mail.app."
938 (unless org-mac-mail-account
939 (error "You must set org-mac-mail-account"))
943 "tell application \"Mail\"\n"
944 "set theMailboxes to every mailbox of account \"" org-mac-mail-account
"\"\n"
945 "set theLinkList to {}\n"
946 "repeat with aMailbox in theMailboxes\n"
947 "set theSelection to (every message in aMailbox whose flagged status = true)\n"
948 "repeat with theMessage in theSelection\n"
949 "set theID to message id of theMessage\n"
950 "set theSubject to subject of theMessage\n"
951 "set theLink to \"message://\" & theID & \"::split::\" & theSubject & \"\n\"\n"
952 "copy theLink to end of theLinkList\n"
955 "return theLinkList as string\n"
959 (defun org-mac-message-get-links (&optional select-or-flag
)
960 "Create links to the messages currently selected or flagged in Mail.app.
961 This will use AppleScript to get the message-id and the subject of the
962 messages in Mail.app and make a link out of it.
963 When SELECT-OR-FLAG is \"s\", get the selected messages (this is also
964 the default). When SELECT-OR-FLAG is \"f\", get the flagged messages.
965 The Org-syntax text will be pushed to the kill ring, and also returned."
966 (interactive "sLink to (s)elected or (f)lagged messages: ")
967 (setq select-or-flag
(or select-or-flag
"s"))
968 (message "AppleScript: searching mailboxes...")
969 (org-mac-paste-applescript-links
971 ((string= select-or-flag
"s") (org-as-get-selected-mail))
972 ((string= select-or-flag
"f") (org-as-get-flagged-mail))
973 (t (error "Please select \"s\" or \"f\"")))))
976 (defun org-mac-message-insert-selected ()
977 "Insert a link to the messages currently selected in Mail.app.
978 This will use AppleScript to get the message-id and the subject of the
979 active mail in Mail.app and make a link out of it."
981 (insert (org-mac-message-get-links "s")))
983 ;; The following line is for backward compatibility
984 (defalias 'org-mac-message-insert-link
'org-mac-message-insert-selected
)
987 (defun org-mac-message-insert-flagged (org-buffer org-heading
)
988 "Asks for an org buffer and a heading within it, and replace message links.
989 If heading exists, delete all message:// links within heading's first
990 level. If heading doesn't exist, create it at point-max. Insert
991 list of message:// links to flagged mail after heading."
992 (interactive "bBuffer in which to insert links: \nsHeading after which to insert links: ")
993 (with-current-buffer org-buffer
994 (goto-char (point-min))
995 (let ((isearch-forward t
)
996 (message-re "\\[\\[\\(message:\\)\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"))
997 (if (org-goto-local-search-headings org-heading nil t
)
1001 (while (re-search-forward
1002 message-re
(save-excursion (outline-next-heading)) t
)
1003 (delete-region (match-beginning 0) (match-end 0)))
1004 (insert "\n" (org-mac-message-get-links "f")))
1005 (flush-lines "^$" (point) (outline-next-heading)))
1006 (insert "\n" (org-mac-message-get-links "f")))
1007 (goto-char (point-max))
1009 (org-insert-heading nil t
)
1010 (insert org-heading
"\n" (org-mac-message-get-links "f"))))))
1013 (provide 'org-mac-link
)
1015 ;;; org-mac-link.el ends here