Fix breakage due to outline-invisible-p defn change in emacs 26+
[org-mode.git] / contrib / lisp / org-mac-link.el
blob7f4af30bbf01ca72cdcbef79c88c300258d6730a
1 ;;; org-mac-link.el --- Insert org-mode links to items selected in various Mac apps
2 ;;
3 ;; Copyright (c) 2010-2017 Free Software Foundation, Inc.
4 ;;
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>
13 ;; Version: 1.1
14 ;; Keywords: org, mac, hyperlink
16 ;; Version: 1.2
17 ;; Keywords: outlook
18 ;; Author: Mike McLean <mike.mclean@pobox.com>
19 ;; Add support for Microsoft Outlook for Mac as Org mode links
21 ;; Version: 1.3
22 ;; Author: Alan Schmitt <alan.schmitt@polytechnique.org>
23 ;; Consistently use `org-mac-paste-applescript-links'
25 ;; Version 1.4
26 ;; Author: Mike McLean <mike.mclean@pobox.com>
27 ;; Make the path to Microsoft Outlook a `defcustom'
29 ;; Version 1.5
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)
38 ;; any later version.
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/>.
48 ;;; Commentary:
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
56 ;; Suckling.
58 ;; Detailed comments for each application interface are inlined with
59 ;; the code. Here is a brief overview of how the code interacts with
60 ;; each application:
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 ;; Together.app - Grab links to the selected items in the library list
70 ;; Skim.app - Grab a link to the selected page in the topmost pdf document
71 ;; Microsoft Outlook.app - Grab a link to the selected message in the message list
72 ;; DEVONthink Pro Office.app - Grab a link to the selected DEVONthink item(s); open DEVONthink item by reference
73 ;; Evernote.app - Grab a link to the selected Evernote item(s); open Evernote item by ID
76 ;; Installation:
78 ;; add (require 'org-mac-link) to your .emacs, and optionally bind a
79 ;; key to activate the link grabber menu, like this:
81 ;; (add-hook 'org-mode-hook (lambda ()
82 ;; (define-key org-mode-map (kbd "C-c g") 'org-mac-grab-link)))
84 ;; Usage:
86 ;; Type C-c g (or whatever key you defined, as above), or type M-x
87 ;; org-mac-grab-link RET to activate the link grabber. This will present
88 ;; you with a menu to choose an application from which to grab a link
89 ;; to insert at point. You may also type C-g to abort.
91 ;; Customizing:
93 ;; You may customize which applications appear in the grab menu by
94 ;; customizing the group `org-mac-link'. Changes take effect
95 ;; immediately.
98 ;;; Code:
100 (require 'org)
102 (defgroup org-mac-link nil
103 "Options for grabbing links from Mac applications."
104 :tag "Org Mac link"
105 :group 'org-link)
107 (defcustom org-mac-grab-Finder-app-p t
108 "Add menu option [F]inder to grab links from the Finder."
109 :tag "Grab Finder.app links"
110 :group 'org-mac-link
111 :type 'boolean)
113 (defcustom org-mac-grab-Mail-app-p t
114 "Add menu option [m]ail to grab links from Mail.app."
115 :tag "Grab Mail.app links"
116 :group 'org-mac-link
117 :type 'boolean)
119 (defcustom org-mac-grab-Outlook-app-p t
120 "Add menu option [o]utlook to grab links from Microsoft Outlook.app."
121 :tag "Grab Microsoft Outlook.app links"
122 :group 'org-mac-link
123 :type 'boolean)
125 (defcustom org-mac-outlook-path "/Applications/Microsoft Outlook.app"
126 "The path to the installed copy of Microsoft Outlook.app. Do not escape spaces as the AppleScript call will quote this string."
127 :tag "Path to Microsoft Outlook"
128 :group 'org-mac-link
129 :type 'string)
131 (defcustom org-mac-grab-devonthink-app-p t
132 "Add menu option [d]EVONthink to grab links from DEVONthink Pro Office.app."
133 :tag "Grab DEVONthink Pro Office.app links"
134 :group 'org-mac-link
135 :type 'boolean)
137 (defcustom org-mac-grab-Addressbook-app-p t
138 "Add menu option [a]ddressbook to grab links from AddressBook.app."
139 :tag "Grab AddressBook.app links"
140 :group 'org-mac-link
141 :type 'boolean)
143 (defcustom org-mac-grab-Safari-app-p t
144 "Add menu option [s]afari to grab links from Safari.app."
145 :tag "Grab Safari.app links"
146 :group 'org-mac-link
147 :type 'boolean)
149 (defcustom org-mac-grab-Firefox-app-p t
150 "Add menu option [f]irefox to grab links from Firefox.app."
151 :tag "Grab Firefox.app links"
152 :group 'org-mac-link
153 :type 'boolean)
155 (defcustom org-mac-grab-Firefox+Vimperator-p nil
156 "Add menu option [v]imperator to grab links from Firefox.app running the Vimperator plugin."
157 :tag "Grab Vimperator/Firefox.app links"
158 :group 'org-mac-link
159 :type 'boolean)
161 (defcustom org-mac-grab-Chrome-app-p t
162 "Add menu option [c]hrome to grab links from Google Chrome.app."
163 :tag "Grab Google Chrome.app links"
164 :group 'org-mac-link
165 :type 'boolean)
167 (defcustom org-mac-grab-Together-app-p nil
168 "Add menu option [t]ogether to grab links from Together.app."
169 :tag "Grab Together.app links"
170 :group 'org-mac-link
171 :type 'boolean)
173 (defcustom org-mac-grab-Skim-app-p
174 (< 0 (length (shell-command-to-string
175 "mdfind kMDItemCFBundleIdentifier == 'net.sourceforge.skim-app.skim'")))
176 "Add menu option [S]kim to grab page links from Skim.app."
177 :tag "Grab Skim.app page links"
178 :group 'org-mac-link
179 :type 'boolean)
181 (defcustom org-mac-Skim-highlight-selection-p nil
182 "Highlight the active selection when grabbing a link from Skim.app."
183 :tag "Highlight selection in Skim.app"
184 :group 'org-mac-link
185 :type 'boolean)
187 (defcustom org-mac-grab-Acrobat-app-p t
188 "Add menu option [A]crobat to grab page links from Acrobat.app."
189 :tag "Grab Acrobat.app page links"
190 :group 'org-mac-link
191 :type 'boolean)
193 (defgroup org-mac-flagged-mail nil
194 "Options foring linking to flagged Mail.app messages."
195 :tag "Org Mail.app"
196 :group 'org-link)
198 (defcustom org-mac-mail-account nil
199 "The Mail.app account in which to search for flagged messages."
200 :group 'org-mac-flagged-mail
201 :type 'string)
203 (defcustom org-mac-grab-Evernote-app-p
204 (< 0 (length (shell-command-to-string
205 "mdfind kMDItemCFBundleIdentifier == 'com.evernote.Evernote'")))
206 "Add menu option [e]vernote to grab note links from Evernote.app."
207 :tag "Grab Evernote.app note links"
208 :group 'org-mac-link
209 :type 'boolean)
211 (defcustom org-mac-evernote-path (replace-regexp-in-string (rx (* (any " \t\n")) eos)
213 (shell-command-to-string
214 "mdfind kMDItemCFBundleIdentifier == 'com.evernote.Evernote'"))
215 "The path to the installed copy of Evernote.app. Do not escape spaces as the AppleScript call will quote this string."
216 :tag "Path to Evernote"
217 :group 'org-mac-link
218 :type 'string)
221 ;; In mac.c, removed in Emacs 23.
222 (declare-function do-applescript "org-mac-message" (script))
223 (unless (fboundp 'do-applescript)
224 ;; Need to fake this using shell-command-to-string
225 (defun do-applescript (script)
226 (let (start cmd return)
227 (while (string-match "\n" script)
228 (setq script (replace-match "\r" t t script)))
229 (while (string-match "'" script start)
230 (setq start (+ 2 (match-beginning 0))
231 script (replace-match "\\'" t t script)))
232 (setq cmd (concat "osascript -e '" script "'"))
233 (setq return (shell-command-to-string cmd))
234 (concat "\"" (org-trim return) "\""))))
236 (defun org-mac-grab-link ()
237 "Prompt for an application to grab a link from.
238 When done, go grab the link, and insert it at point."
239 (interactive)
240 (let* ((descriptors
241 `(("F" "inder" org-mac-finder-insert-selected ,org-mac-grab-Finder-app-p)
242 ("m" "ail" org-mac-message-insert-selected ,org-mac-grab-Mail-app-p)
243 ("d" "EVONthink Pro Office" org-mac-devonthink-item-insert-selected
244 ,org-mac-grab-devonthink-app-p)
245 ("o" "utlook" org-mac-outlook-message-insert-selected ,org-mac-grab-Outlook-app-p)
246 ("a" "ddressbook" org-mac-addressbook-insert-selected ,org-mac-grab-Addressbook-app-p)
247 ("s" "afari" org-mac-safari-insert-frontmost-url ,org-mac-grab-Safari-app-p)
248 ("f" "irefox" org-mac-firefox-insert-frontmost-url ,org-mac-grab-Firefox-app-p)
249 ("v" "imperator" org-mac-vimperator-insert-frontmost-url ,org-mac-grab-Firefox+Vimperator-p)
250 ("c" "hrome" org-mac-chrome-insert-frontmost-url ,org-mac-grab-Chrome-app-p)
251 ("e" "evernote" org-mac-evernote-note-insert-selected ,org-mac-grab-Evernote-app-p)
252 ("t" "ogether" org-mac-together-insert-selected ,org-mac-grab-Together-app-p)
253 ("S" "kim" org-mac-skim-insert-page ,org-mac-grab-Skim-app-p)
254 ("A" "crobat" org-mac-acrobat-insert-page ,org-mac-grab-Acrobat-app-p)))
255 (menu-string (make-string 0 ?x))
256 input)
258 ;; Create the menu string for the keymap
259 (mapc (lambda (descriptor)
260 (when (elt descriptor 3)
261 (setf menu-string (concat menu-string
262 "[" (elt descriptor 0) "]"
263 (elt descriptor 1) " "))))
264 descriptors)
265 (setf (elt menu-string (- (length menu-string) 1)) ?:)
267 ;; Prompt the user, and grab the link
268 (message menu-string)
269 (setq input (read-char-exclusive))
270 (mapc (lambda (descriptor)
271 (let ((key (elt (elt descriptor 0) 0))
272 (active (elt descriptor 3))
273 (grab-function (elt descriptor 2)))
274 (when (and active (eq input key))
275 (call-interactively grab-function))))
276 descriptors)))
278 (defun org-mac-paste-applescript-links (as-link-list)
279 "Paste in a list of links from an applescript handler.
280 The links are of the form <link>::split::<name>."
281 (let* ((noquote-as-link-list
282 (if (string-prefix-p "\"" as-link-list)
283 (substring as-link-list 1 -1)
284 as-link-list))
285 (link-list
286 (mapcar (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x)
287 (setq x (match-string 1 x)))
289 (split-string noquote-as-link-list "[\r\n]+")))
290 split-link URL description orglink orglink-insert rtn orglink-list)
291 (while link-list
292 (setq split-link (split-string (pop link-list) "::split::"))
293 (setq URL (car split-link))
294 (setq description (cadr split-link))
295 (when (not (string= URL ""))
296 (setq orglink (org-make-link-string URL description))
297 (push orglink orglink-list)))
298 (setq rtn (mapconcat 'identity orglink-list "\n"))
299 (kill-new rtn)
300 rtn))
303 ;; Handle links from Firefox.app
305 ;; This code allows you to grab the current active url from the main
306 ;; Firefox.app window, and insert it as a link into an org-mode
307 ;; document. Unfortunately, firefox does not expose an applescript
308 ;; dictionary, so this is necessarily introduces some limitations.
310 ;; The applescript to grab the url from Firefox.app uses the System
311 ;; Events application to give focus to the firefox application, select
312 ;; the contents of the url bar, and copy it. It then uses the title of
313 ;; the window as the text of the link. There is no way to grab links
314 ;; from other open tabs, and further, if there is more than one window
315 ;; open, it is not clear which one will be used (though emperically it
316 ;; seems that it is always the last active window).
318 (defun org-as-mac-firefox-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 \"l\" using {command down}\n"
329 " keystroke \"a\" using {command down}\n"
330 " keystroke \"c\" using {command down}\n"
331 " end tell\n"
332 " delay 0.15\n"
333 " set theUrl to the clipboard\n"
334 " set the clipboard to oldClipboard\n"
335 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
336 "end tell\n"
337 "activate application (frontmostApplication as text)\n"
338 "set links to {}\n"
339 "copy theResult to the end of links\n"
340 "return links as string\n"))))
341 (car (split-string result "[\r\n]+" t))))
343 (defun org-mac-firefox-get-frontmost-url ()
344 (interactive)
345 (message "Applescript: Getting Firefox url...")
346 (org-mac-paste-applescript-links (org-as-mac-firefox-get-frontmost-url)))
348 (defun org-mac-firefox-insert-frontmost-url ()
349 (interactive)
350 (insert (org-mac-firefox-get-frontmost-url)))
353 ;; Handle links from Google Firefox.app running the Vimperator extension
354 ;; Grab the frontmost url from Firefox+Vimperator. Same limitations are
355 ;; Firefox
357 (defun org-as-mac-vimperator-get-frontmost-url ()
358 (let ((result
359 (do-applescript
360 (concat
361 "set oldClipboard to the clipboard\n"
362 "set frontmostApplication to path to frontmost application\n"
363 "tell application \"Firefox\"\n"
364 " activate\n"
365 " delay 0.15\n"
366 " tell application \"System Events\"\n"
367 " keystroke \"y\"\n"
368 " end tell\n"
369 " delay 0.15\n"
370 " set theUrl to the clipboard\n"
371 " set the clipboard to oldClipboard\n"
372 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
373 "end tell\n"
374 "activate application (frontmostApplication as text)\n"
375 "set links to {}\n"
376 "copy theResult to the end of links\n"
377 "return links as string\n"))))
378 (replace-regexp-in-string
379 "\s+-\s+Vimperator" "" (car (split-string result "[\r\n]+" t)))))
381 (defun org-mac-vimperator-get-frontmost-url ()
382 (interactive)
383 (message "Applescript: Getting Vimperator url...")
384 (org-mac-paste-applescript-links (org-as-mac-vimperator-get-frontmost-url)))
386 (defun org-mac-vimperator-insert-frontmost-url ()
387 (interactive)
388 (insert (org-mac-vimperator-get-frontmost-url)))
391 ;; Handle links from Google Chrome.app
392 ;; Grab the frontmost url from Google Chrome. Same limitations as
393 ;; Firefox because Chrome doesn't publish an Applescript dictionary
395 (defun org-as-mac-chrome-get-frontmost-url ()
396 (let ((result
397 (do-applescript
398 (concat
399 "set frontmostApplication to path to frontmost application\n"
400 "tell application \"Google Chrome\"\n"
401 " set theUrl to get URL of active tab of first window\n"
402 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
403 "end tell\n"
404 "activate application (frontmostApplication as text)\n"
405 "set links to {}\n"
406 "copy theResult to the end of links\n"
407 "return links as string\n"))))
408 (replace-regexp-in-string
409 "^\"\\|\"$" "" (car (split-string result "[\r\n]+" t)))))
411 (defun org-mac-chrome-get-frontmost-url ()
412 (interactive)
413 (message "Applescript: Getting Chrome url...")
414 (org-mac-paste-applescript-links (org-as-mac-chrome-get-frontmost-url)))
416 (defun org-mac-chrome-insert-frontmost-url ()
417 (interactive)
418 (insert (org-mac-chrome-get-frontmost-url)))
421 ;; Handle links from Safari.app
422 ;; Grab the frontmost url from Safari.
424 (defun org-as-mac-safari-get-frontmost-url ()
425 (do-applescript
426 (concat
427 "tell application \"Safari\"\n"
428 " set theUrl to URL of document 1\n"
429 " set theName to the name of the document 1\n"
430 " return theUrl & \"::split::\" & theName & \"\n\"\n"
431 "end tell\n")))
433 (defun org-mac-safari-get-frontmost-url ()
434 (interactive)
435 (message "Applescript: Getting Safari url...")
436 (org-mac-paste-applescript-links
437 (org-as-mac-safari-get-frontmost-url)))
439 (defun org-mac-safari-insert-frontmost-url ()
440 (interactive)
441 (insert (org-mac-safari-get-frontmost-url)))
444 ;; Handle links from together.app
445 (org-link-set-parameters "x-together-item" :follow #'org-mac-together-item-open)
447 (defun org-mac-together-item-open (uid)
448 "Open UID, which is a reference to an item in Together."
449 (shell-command (concat "open -a Together \"x-together-item:" uid "\"")))
451 (defun as-get-selected-together-items ()
452 (do-applescript
453 (concat
454 "tell application \"Together\"\n"
455 " set theLinkList to {}\n"
456 " set theSelection to selected items\n"
457 " repeat with theItem in theSelection\n"
458 " set theLink to (get item link of theItem) & \"::split::\" & (get name of theItem) & \"\n\"\n"
459 " copy theLink to end of theLinkList\n"
460 " end repeat\n"
461 " return theLinkList as string\n"
462 "end tell")))
464 (defun org-mac-together-get-selected ()
465 (interactive)
466 (message "Applescript: Getting Togther items...")
467 (org-mac-paste-applescript-links (as-get-selected-together-items)))
469 (defun org-mac-together-insert-selected ()
470 (interactive)
471 (insert (org-mac-together-get-selected)))
474 ;; Handle links from Finder.app
476 (defun as-get-selected-finder-items ()
477 (do-applescript
478 (concat
479 "tell application \"Finder\"\n"
480 " set theSelection to the selection\n"
481 " set links to {}\n"
482 " repeat with theItem in theSelection\n"
483 " set theLink to \"file://\" & (POSIX path of (theItem as string)) & \"::split::\" & (get the name of theItem) & \"\n\"\n"
484 " copy theLink to the end of links\n"
485 " end repeat\n"
486 " return links as string\n"
487 "end tell\n")))
489 (defun org-mac-finder-item-get-selected ()
490 (interactive)
491 (message "Applescript: Getting Finder items...")
492 (org-mac-paste-applescript-links (as-get-selected-finder-items)))
494 (defun org-mac-finder-insert-selected ()
495 (interactive)
496 (insert (org-mac-finder-item-get-selected)))
499 ;; Handle links from AddressBook.app
500 (org-link-set-parameters "addressbook" :follow #'org-mac-addressbook-item-open)
502 (defun org-mac-addressbook-item-open (uid)
503 "Open UID, which is a reference to an item in the addressbook."
504 (shell-command (concat "open \"addressbook:" uid "\"")))
506 (defun as-get-selected-addressbook-items ()
507 (do-applescript
508 (concat
509 "tell application \"Address Book\"\n"
510 " set theSelection to the selection\n"
511 " set links to {}\n"
512 " repeat with theItem in theSelection\n"
513 " set theLink to \"addressbook://\" & (the id of theItem) & \"::split::\" & (the name of theItem) & \"\n\"\n"
514 " copy theLink to the end of links\n"
515 " end repeat\n"
516 " return links as string\n"
517 "end tell\n")))
519 (defun org-mac-addressbook-item-get-selected ()
520 (interactive)
521 (message "Applescript: Getting Address Book items...")
522 (org-mac-paste-applescript-links (as-get-selected-addressbook-items)))
524 (defun org-mac-addressbook-insert-selected ()
525 (interactive)
526 (insert (org-mac-addressbook-item-get-selected)))
529 ;; Handle links from Skim.app
531 ;; Original code & idea by Christopher Suckling (org-mac-protocol)
533 (org-link-set-parameters "skim" :follow #'org-mac-skim-open)
535 (defun org-mac-skim-open (uri)
536 "Visit page of pdf in Skim"
537 (let* ((page (when (string-match "::\\(.+\\)\\'" uri)
538 (match-string 1 uri)))
539 (document (substring uri 0 (match-beginning 0))))
540 (do-applescript
541 (concat
542 "tell application \"Skim\"\n"
543 "activate\n"
544 "set theDoc to \"" document "\"\n"
545 "set thePage to " page "\n"
546 "open theDoc\n"
547 "go document 1 to page thePage of document 1\n"
548 "end tell"))))
550 (defun as-get-skim-page-link ()
551 (do-applescript
552 (concat
553 "tell application \"Skim\"\n"
554 "set theDoc to front document\n"
555 "set theTitle to (name of theDoc)\n"
556 "set thePath to (path of theDoc)\n"
557 "set thePage to (get index for current page of theDoc)\n"
558 "set theSelection to selection of theDoc\n"
559 "set theContent to contents of (get text for theSelection)\n"
560 "if theContent is missing value then\n"
561 " set theContent to theTitle & \", p. \" & thePage\n"
562 (when org-mac-Skim-highlight-selection-p
563 (concat
564 "else\n"
565 " tell theDoc\n"
566 " set theNote to make note with properties {type:highlight note, selection:theSelection}\n"
567 " set text of theNote to (get text for theSelection)\n"
568 " end tell\n"))
569 "end if\n"
570 "set theLink to \"skim://\" & thePath & \"::\" & thePage & "
571 "\"::split::\" & theContent\n"
572 "end tell\n"
573 "return theLink as string\n")))
575 (defun org-mac-skim-get-page ()
576 (interactive)
577 (message "Applescript: Getting Skim page link...")
578 (org-mac-paste-applescript-links (as-get-skim-page-link)))
580 (defun org-mac-skim-insert-page ()
581 (interactive)
582 (insert (org-mac-skim-get-page)))
584 ;; Handle links from Adobe Acrobat Pro.app
586 ;; Original code & idea by Christopher Suckling (org-mac-protocol)
588 ;; The URI format is path_to_pdf_file::page_number
590 (org-link-set-parameters "acrobat" :follow #'org-mac-acrobat-open)
592 (defun org-mac-acrobat-open (uri)
593 "Visit page of pdf in Acrobat"
594 (let* ((page (when (string-match "::\\(.+\\)\\'" uri)
595 (match-string 1 uri)))
596 (document (substring uri 0 (match-beginning 0))))
597 (do-applescript
598 (concat
599 "tell application \"Adobe Acrobat Pro\"\n"
600 " activate\n"
601 " set theDoc to \"" document "\"\n"
602 " set thePage to " page "\n"
603 " open theDoc\n"
604 " tell PDF Window 1\n"
605 " goto page thePage\n"
606 " end tell\n"
607 "end tell"))))
609 ;; The applescript returns link in the format
610 ;; "adobe:path_to_pdf_file::page_number::split::document_title, p.page_label"
612 (defun org-mac-as-get-acrobat-page-link ()
613 (do-applescript
614 (concat
615 "tell application \"Adobe Acrobat Pro\"\n"
616 " set theDoc to active doc\n"
617 " set theWindow to (PDF Window 1 of theDoc)\n"
618 " set thePath to (file alias of theDoc)\n"
619 " set theTitle to (name of theWindow)\n"
620 " set thePage to (page number of theWindow)\n"
621 " set theLabel to (label text of (page thePage of theWindow))\n"
622 "end tell\n"
623 "set theResult to \"acrobat:\" & thePath & \"::\" & thePage & \"::split::\" & theTitle & \", p.\" & theLabel\n"
624 "return theResult as string\n")))
626 (defun org-mac-acrobat-get-page ()
627 (interactive)
628 (message "Applescript: Getting Acrobat page link...")
629 (org-mac-paste-applescript-links (org-mac-as-get-acrobat-page-link)))
631 (defun org-mac-acrobat-insert-page ()
632 (interactive)
633 (insert (org-mac-acrobat-get-page)))
636 ;; Handle links from Microsoft Outlook.app
638 (org-link-set-parameters "mac-outlook" :follow #'org-mac-outlook-message-open)
640 (defun org-mac-outlook-message-open (msgid)
641 "Open a message in Outlook"
642 (do-applescript
643 (concat
644 "tell application \"" org-mac-outlook-path "\"\n"
645 (format "open message id %s\n" (substring-no-properties msgid))
646 "activate\n"
647 "end tell")))
649 (defun org-as-get-selected-outlook-mail ()
650 "AppleScript to create links to selected messages in Microsoft Outlook.app."
651 (do-applescript
652 (concat
653 "tell application \"" org-mac-outlook-path "\"\n"
654 "set msgCount to count current messages\n"
655 "if (msgCount < 1) then\n"
656 "return\n"
657 "end if\n"
658 "set theLinkList to {}\n"
659 "set theSelection to (get current messages)\n"
660 "repeat with theMessage in theSelection\n"
661 "set theID to id of theMessage as string\n"
662 "set theURL to \"mac-outlook:\" & theID\n"
663 "set theSubject to subject of theMessage\n"
664 "set theLink to theURL & \"::split::\" & theSubject & \"\n\"\n"
665 "copy theLink to end of theLinkList\n"
666 "end repeat\n"
667 "return theLinkList as string\n"
668 "end tell")))
670 (defun org-sh-get-flagged-outlook-mail ()
671 "Shell commands to create links to flagged messages in Microsoft Outlook.app."
672 (mapconcat
673 (lambda (x) ""
674 (concat
675 "mac-outlook:"
676 (mapconcat
677 (lambda (y) "" y)
678 (split-string
679 (shell-command-to-string
680 (format "mdls -raw -name com_microsoft_outlook_recordID -name kMDItemDisplayName \"%s\"" x))
681 "\000")
682 "::split::")
683 "\n"))
684 (with-temp-buffer
685 (let ((coding-system-for-read (or file-name-coding-system 'utf-8))
686 (coding-system-for-write 'utf-8))
687 (shell-command
688 "mdfind com_microsoft_outlook_flagged==1"
689 (current-buffer)))
690 (split-string
691 (buffer-string) "\n" t))
692 ""))
694 (defun org-mac-outlook-message-get-links (&optional select-or-flag)
695 "Create links to the messages currently selected or flagged in Microsoft Outlook.app.
696 This will use AppleScript to get the message-id and the subject of the
697 messages in Microsoft Outlook.app and make a link out of it.
698 When SELECT-OR-FLAG is \"s\", get the selected messages (this is also
699 the default). When SELECT-OR-FLAG is \"f\", get the flagged messages.
700 The Org-syntax text will be pushed to the kill ring, and also returned."
701 (interactive "sLink to (s)elected or (f)lagged messages: ")
702 (setq select-or-flag (or select-or-flag "s"))
703 (message "Org Mac Outlook: searching mailboxes...")
704 (org-mac-paste-applescript-links
705 (if (string= select-or-flag "s")
706 (org-as-get-selected-outlook-mail)
707 (if (string= select-or-flag "f")
708 (org-sh-get-flagged-outlook-mail)
709 (error "Please select \"s\" or \"f\"")))))
711 (defun org-mac-outlook-message-insert-selected ()
712 "Insert a link to the messages currently selected in Microsoft Outlook.app.
713 This will use AppleScript to get the message-id and the subject
714 of the active mail in Microsoft Outlook.app and make a link out
715 of it."
716 (interactive)
717 (insert (org-mac-outlook-message-get-links "s")))
719 (defun org-mac-outlook-message-insert-flagged (org-buffer org-heading)
720 "Asks for an org buffer and a heading within it, and replace message links.
721 If heading exists, delete all mac-outlook:// links within
722 heading's first level. If heading doesn't exist, create it at
723 point-max. Insert list of mac-outlook:// links to flagged mail
724 after heading."
725 (interactive "bBuffer in which to insert links: \nsHeading after which to insert links: ")
726 (with-current-buffer org-buffer
727 (goto-char (point-min))
728 (let ((isearch-forward t)
729 (message-re "\\[\\[\\(mac-outlook:\\)\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"))
730 (if (org-goto-local-search-headings org-heading nil t)
731 (if (not (eobp))
732 (progn
733 (save-excursion
734 (while (re-search-forward
735 message-re (save-excursion (outline-next-heading)) t)
736 (delete-region (match-beginning 0) (match-end 0)))
737 (insert "\n" (org-mac-outlook-message-get-links "f")))
738 (flush-lines "^$" (point) (outline-next-heading)))
739 (insert "\n" (org-mac-outlook-message-get-links "f")))
740 (goto-char (point-max))
741 (insert "\n")
742 (org-insert-heading nil t)
743 (insert org-heading "\n" (org-mac-outlook-message-get-links "f"))))))
745 ;; Handle links from Evernote.app
747 (org-link-set-parameters "mac-evernote" :follow #'org-mac-evernote-note-open)
749 (defun org-mac-evernote-note-open (noteid)
750 "Open a note in Evernote"
751 (do-applescript
752 (concat
753 "tell application \"" org-mac-evernote-path "\"\n"
754 " set theNotes to get every note of every notebook where its local id is \"" (substring-no-properties noteid) "\"\n"
755 " repeat with _note in theNotes\n"
756 " if length of _note is not 0 then\n"
757 " set _selectedNote to _note\n"
758 " end if\n"
759 " end repeat\n"
760 " open note window with item 1 of _selectedNote\n"
761 " activate\n"
762 "end tell")))
764 (defun org-as-get-selected-evernote-notes ()
765 "AppleScript to create links to selected notes in Evernote.app."
766 (do-applescript
767 (concat
768 "tell application \"" org-mac-evernote-path "\"\n"
769 " set noteCount to count selection\n"
770 " if (noteCount < 1) then\n"
771 " return\n"
772 " end if\n"
773 " set theLinkList to {}\n"
774 " set theSelection to selection\n"
775 " repeat with theNote in theSelection\n"
776 " set theTitle to title of theNote\n"
777 " set theID to local id of theNote\n"
778 " set theURL to \"mac-evernote:\" & theID\n"
779 " set theLink to theURL & \"::split::\" & theTitle & \"\n\"\n"
780 " copy theLink to end of theLinkList\n"
781 " end repeat\n"
782 " return theLinkList as string\n"
783 "end tell\n")))
785 (defun org-mac-evernote-note-insert-selected ()
786 "Insert a link to the notes currently selected in Evernote.app.
787 This will use AppleScript to get the note id and the title of the
788 note(s) in Evernote.app and make a link out of it/them."
789 (interactive)
790 (message "Org Mac Evernote: searching notes...")
791 (insert (org-mac-paste-applescript-links
792 (org-as-get-selected-evernote-notes))))
795 ;; Handle links from DEVONthink Pro Office.app
797 (org-link-set-parameters "x-devonthink-item" :follow #'org-devonthink-item-open)
799 (defun org-devonthink-item-open (uid)
800 "Open UID, which is a reference to an item in DEVONthink Pro Office."
801 (shell-command (concat "open \"x-devonthink-item:" uid "\"")))
803 (defun org-as-get-selected-devonthink-item ()
804 "AppleScript to create links to selected items in DEVONthink Pro Office.app."
805 (do-applescript
806 (concat
807 "set theLinkList to {}\n"
808 "tell application \"DEVONthink Pro\"\n"
809 "set selectedRecords to selection\n"
810 "set selectionCount to count of selectedRecords\n"
811 "if (selectionCount < 1) then\n"
812 "return\n"
813 "end if\n"
814 "repeat with theRecord in selectedRecords\n"
815 "set theID to uuid of theRecord\n"
816 "set theURL to \"x-devonthink-item:\" & theID\n"
817 "set theSubject to name of theRecord\n"
818 "set theLink to theURL & \"::split::\" & theSubject & \"\n\"\n"
819 "copy theLink to end of theLinkList\n"
820 "end repeat\n"
821 "end tell\n"
822 "return theLinkList as string"
825 (defun org-mac-devonthink-get-links ()
826 "Create links to the item(s) currently selected in DEVONthink Pro Office.
827 This will use AppleScript to get the `uuid' and the `name' of the
828 selected items in DEVONthink Pro Office.app and make links out of
829 it/them. This function will push the Org-syntax text to the kill
830 ring, and also return it."
831 (message "Org Mac DEVONthink: looking for selected items...")
832 (org-mac-paste-applescript-links (org-as-get-selected-devonthink-item)))
834 (defun org-mac-devonthink-item-insert-selected ()
835 "Insert a link to the item(s) currently selected in DEVONthink Pro Office.
836 This will use AppleScript to get the `uuid'(s) and the name(s) of the
837 selected items in DEVONthink Pro Office and make link(s) out of it/them."
838 (interactive)
839 (insert (org-mac-devonthink-get-links)))
842 ;; Handle links from Mail.app
844 (org-link-set-parameters "message" :follow #'org-mac-message-open)
846 (defun org-mac-message-open (message-id)
847 "Visit the message with MESSAGE-ID.
848 This will use the command `open' with the message URL."
849 (start-process (concat "open message:" message-id) nil
850 "open" (concat "message://<" (substring message-id 2) ">")))
852 (defun org-as-get-selected-mail ()
853 "AppleScript to create links to selected messages in Mail.app."
854 (do-applescript
855 (concat
856 "tell application \"Mail\"\n"
857 "set theLinkList to {}\n"
858 "set theSelection to selection\n"
859 "repeat with theMessage in theSelection\n"
860 "set theID to message id of theMessage\n"
861 "set theSubject to subject of theMessage\n"
862 "set theLink to \"message://\" & theID & \"::split::\" & theSubject\n"
863 "if (theLinkList is not equal to {}) then\n"
864 "set theLink to \"\n\" & theLink\n"
865 "end if\n"
866 "copy theLink to end of theLinkList\n"
867 "end repeat\n"
868 "return theLinkList as string\n"
869 "end tell")))
871 (defun org-as-get-flagged-mail ()
872 "AppleScript to create links to flagged messages in Mail.app."
873 (unless org-mac-mail-account
874 (error "You must set org-mac-mail-account"))
875 (do-applescript
876 (concat
877 ;; Get links
878 "tell application \"Mail\"\n"
879 "set theMailboxes to every mailbox of account \"" org-mac-mail-account "\"\n"
880 "set theLinkList to {}\n"
881 "repeat with aMailbox in theMailboxes\n"
882 "set theSelection to (every message in aMailbox whose flagged status = true)\n"
883 "repeat with theMessage in theSelection\n"
884 "set theID to message id of theMessage\n"
885 "set theSubject to subject of theMessage\n"
886 "set theLink to \"message://\" & theID & \"::split::\" & theSubject & \"\n\"\n"
887 "copy theLink to end of theLinkList\n"
888 "end repeat\n"
889 "end repeat\n"
890 "return theLinkList as string\n"
891 "end tell")))
893 (defun org-mac-message-get-links (&optional select-or-flag)
894 "Create links to the messages currently selected or flagged in Mail.app.
895 This will use AppleScript to get the message-id and the subject of the
896 messages in Mail.app and make a link out of it.
897 When SELECT-OR-FLAG is \"s\", get the selected messages (this is also
898 the default). When SELECT-OR-FLAG is \"f\", get the flagged messages.
899 The Org-syntax text will be pushed to the kill ring, and also returned."
900 (interactive "sLink to (s)elected or (f)lagged messages: ")
901 (setq select-or-flag (or select-or-flag "s"))
902 (message "AppleScript: searching mailboxes...")
903 (org-mac-paste-applescript-links
904 (cond
905 ((string= select-or-flag "s") (org-as-get-selected-mail))
906 ((string= select-or-flag "f") (org-as-get-flagged-mail))
907 (t (error "Please select \"s\" or \"f\"")))))
909 (defun org-mac-message-insert-selected ()
910 "Insert a link to the messages currently selected in Mail.app.
911 This will use AppleScript to get the message-id and the subject of the
912 active mail in Mail.app and make a link out of it."
913 (interactive)
914 (insert (org-mac-message-get-links "s")))
916 ;; The following line is for backward compatibility
917 (defalias 'org-mac-message-insert-link 'org-mac-message-insert-selected)
919 (defun org-mac-message-insert-flagged (org-buffer org-heading)
920 "Asks for an org buffer and a heading within it, and replace message links.
921 If heading exists, delete all message:// links within heading's first
922 level. If heading doesn't exist, create it at point-max. Insert
923 list of message:// links to flagged mail after heading."
924 (interactive "bBuffer in which to insert links: \nsHeading after which to insert links: ")
925 (with-current-buffer org-buffer
926 (goto-char (point-min))
927 (let ((isearch-forward t)
928 (message-re "\\[\\[\\(message:\\)\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"))
929 (if (org-goto-local-search-headings org-heading nil t)
930 (if (not (eobp))
931 (progn
932 (save-excursion
933 (while (re-search-forward
934 message-re (save-excursion (outline-next-heading)) t)
935 (delete-region (match-beginning 0) (match-end 0)))
936 (insert "\n" (org-mac-message-get-links "f")))
937 (flush-lines "^$" (point) (outline-next-heading)))
938 (insert "\n" (org-mac-message-get-links "f")))
939 (goto-char (point-max))
940 (insert "\n")
941 (org-insert-heading nil t)
942 (insert org-heading "\n" (org-mac-message-get-links "f"))))))
945 (provide 'org-mac-link)
947 ;;; org-mac-link.el ends here