lisp/org-table.el: fix table alignment
[org-mode/org-tableheadings.git] / contrib / lisp / org-mac-link.el
blob6bd6dc5c97cc68d2466332fdbc5aceadfcb96233
1 ;;; org-mac-link.el --- Insert org-mode links to items selected in various Mac apps
2 ;;
3 ;; Copyright (c) 2010-2018 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 ;; 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
77 ;; Installation:
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)))
85 ;; Usage:
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.
92 ;; Customizing:
94 ;; You may customize which applications appear in the grab menu by
95 ;; customizing the group `org-mac-link'. Changes take effect
96 ;; immediately.
99 ;;; Code:
101 (require 'org)
103 (defgroup org-mac-link nil
104 "Options for grabbing links from Mac applications."
105 :tag "Org Mac link"
106 :group 'org-link)
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"
111 :group 'org-mac-link
112 :type 'boolean)
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"
117 :group 'org-mac-link
118 :type 'boolean)
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"
123 :group 'org-mac-link
124 :type 'boolean)
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"
129 :group 'org-mac-link
130 :type 'string)
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"
135 :group 'org-mac-link
136 :type 'boolean)
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"
141 :group 'org-mac-link
142 :type 'boolean)
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"
147 :group 'org-mac-link
148 :type 'boolean)
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"
153 :group 'org-mac-link
154 :type 'boolean)
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"
159 :group 'org-mac-link
160 :type 'boolean)
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"
165 :group 'org-mac-link
166 :type 'boolean)
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"
171 :group 'org-mac-link
172 :type 'boolean)
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"
177 :group 'org-mac-link
178 :type 'boolean)
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"
185 :group 'org-mac-link
186 :type 'boolean)
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"
191 :group 'org-mac-link
192 :type 'boolean)
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"
197 :group 'org-mac-link
198 :type 'boolean)
200 (defgroup org-mac-flagged-mail nil
201 "Options foring linking to flagged Mail.app messages."
202 :tag "Org Mail.app"
203 :group 'org-link)
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
208 :type 'string)
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"
215 :group 'org-mac-link
216 :type 'boolean)
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"
224 :group 'org-mac-link
225 :type 'string)
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) "\""))))
243 ;;;###autoload
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."
247 (interactive)
248 (let* ((descriptors
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))
265 input)
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) " "))))
273 descriptors)
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))))
285 descriptors)))
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)
293 as-link-list))
294 (link-list
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)
300 (while link-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"))
308 (kill-new rtn)
309 rtn))
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 ()
328 (let ((result
329 (do-applescript
330 (concat
331 "set oldClipboard to the clipboard\n"
332 "set frontmostApplication to path to frontmost application\n"
333 "tell application \"Firefox\"\n"
334 " activate\n"
335 " delay 0.15\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"
340 " end tell\n"
341 " delay 0.15\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"
345 "end tell\n"
346 "activate application (frontmostApplication as text)\n"
347 "set links to {}\n"
348 "copy theResult to the end of links\n"
349 "return links as string\n"))))
350 (car (split-string result "[\r\n]+" t))))
352 ;;;###autoload
353 (defun org-mac-firefox-get-frontmost-url ()
354 (interactive)
355 (message "Applescript: Getting Firefox url...")
356 (org-mac-paste-applescript-links (org-as-mac-firefox-get-frontmost-url)))
358 ;;;###autoload
359 (defun org-mac-firefox-insert-frontmost-url ()
360 (interactive)
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
366 ;; Firefox
368 (defun org-as-mac-vimperator-get-frontmost-url ()
369 (let ((result
370 (do-applescript
371 (concat
372 "set oldClipboard to the clipboard\n"
373 "set frontmostApplication to path to frontmost application\n"
374 "tell application \"Firefox\"\n"
375 " activate\n"
376 " delay 0.15\n"
377 " tell application \"System Events\"\n"
378 " keystroke \"y\"\n"
379 " end tell\n"
380 " delay 0.15\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"
384 "end tell\n"
385 "activate application (frontmostApplication as text)\n"
386 "set links to {}\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)))))
392 ;;;###autoload
393 (defun org-mac-vimperator-get-frontmost-url ()
394 (interactive)
395 (message "Applescript: Getting Vimperator url...")
396 (org-mac-paste-applescript-links (org-as-mac-vimperator-get-frontmost-url)))
398 ;;;###autoload
399 (defun org-mac-vimperator-insert-frontmost-url ()
400 (interactive)
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 ()
409 (let ((result
410 (do-applescript
411 (concat
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"
416 "end tell\n"
417 "activate application (frontmostApplication as text)\n"
418 "set links to {}\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)))))
424 ;;;###autoload
425 (defun org-mac-chrome-get-frontmost-url ()
426 (interactive)
427 (message "Applescript: Getting Chrome url...")
428 (org-mac-paste-applescript-links (org-as-mac-chrome-get-frontmost-url)))
430 ;;;###autoload
431 (defun org-mac-chrome-insert-frontmost-url ()
432 (interactive)
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
439 ;; dictionary
441 (defun org-as-mac-brave-get-frontmost-url ()
442 (let ((result
443 (do-applescript
444 (concat
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"
449 "end tell\n"
450 "activate application (frontmostApplication as text)\n"
451 "set links to {}\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)))))
457 ;;;###autoload
458 (defun org-mac-brave-get-frontmost-url ()
459 (interactive)
460 (message "Applescript: Getting Brave url...")
461 (org-mac-paste-applescript-links (org-as-mac-brave-get-frontmost-url)))
463 ;;;###autoload
464 (defun org-mac-brave-insert-frontmost-url ()
465 (interactive)
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 ()
473 (do-applescript
474 (concat
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"
479 "end tell\n")))
481 ;;;###autoload
482 (defun org-mac-safari-get-frontmost-url ()
483 (interactive)
484 (message "Applescript: Getting Safari url...")
485 (org-mac-paste-applescript-links
486 (org-as-mac-safari-get-frontmost-url)))
488 ;;;###autoload
489 (defun org-mac-safari-insert-frontmost-url ()
490 (interactive)
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 ()
502 (do-applescript
503 (concat
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"
510 " end repeat\n"
511 " return theLinkList as string\n"
512 "end tell")))
514 ;;;###autoload
515 (defun org-mac-together-get-selected ()
516 (interactive)
517 (message "Applescript: Getting Togther items...")
518 (org-mac-paste-applescript-links (as-get-selected-together-items)))
520 ;;;###autoload
521 (defun org-mac-together-insert-selected ()
522 (interactive)
523 (insert (org-mac-together-get-selected)))
526 ;; Handle links from Finder.app
528 (defun as-get-selected-finder-items ()
529 (do-applescript
530 (concat
531 "tell application \"Finder\"\n"
532 " set theSelection to the selection\n"
533 " set links to {}\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"
537 " end repeat\n"
538 " return links as string\n"
539 "end tell\n")))
541 ;;;###autoload
542 (defun org-mac-finder-item-get-selected ()
543 (interactive)
544 (message "Applescript: Getting Finder items...")
545 (org-mac-paste-applescript-links (as-get-selected-finder-items)))
547 ;;;###autoload
548 (defun org-mac-finder-insert-selected ()
549 (interactive)
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 ()
561 (do-applescript
562 (concat
563 "tell application \"Address Book\"\n"
564 " set theSelection to the selection\n"
565 " set links to {}\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"
569 " end repeat\n"
570 " return links as string\n"
571 "end tell\n")))
573 ;;;###autoload
574 (defun org-mac-addressbook-item-get-selected ()
575 (interactive)
576 (message "Applescript: Getting Address Book items...")
577 (org-mac-paste-applescript-links (as-get-selected-addressbook-items)))
579 ;;;###autoload
580 (defun org-mac-addressbook-insert-selected ()
581 (interactive)
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))))
596 (do-applescript
597 (concat
598 "tell application \"Skim\"\n"
599 "activate\n"
600 "set theDoc to \"" document "\"\n"
601 "set thePage to " page "\n"
602 "open theDoc\n"
603 "go document 1 to page thePage of document 1\n"
604 "end tell"))))
606 (defun as-get-skim-page-link ()
607 (do-applescript
608 (concat
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
619 (concat
620 "else\n"
621 " tell theDoc\n"
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"
624 " end tell\n"))
625 "end if\n"
626 "set theLink to \"skim://\" & thePath & \"::\" & thePage & "
627 "\"::split::\" & theContent\n"
628 "end tell\n"
629 "return theLink as string\n")))
631 ;;;###autoload
632 (defun org-mac-skim-get-page ()
633 (interactive)
634 (message "Applescript: Getting Skim page link...")
635 (org-mac-paste-applescript-links (as-get-skim-page-link)))
637 ;;;###autoload
638 (defun org-mac-skim-insert-page ()
639 (interactive)
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))))
655 (do-applescript
656 (concat
657 "tell application \"Adobe Acrobat Pro\"\n"
658 " activate\n"
659 " set theDoc to \"" document "\"\n"
660 " set thePage to " page "\n"
661 " open theDoc\n"
662 " tell PDF Window 1\n"
663 " goto page thePage\n"
664 " end tell\n"
665 "end tell"))))
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 ()
671 (do-applescript
672 (concat
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"
680 "end tell\n"
681 "set theResult to \"acrobat:\" & thePath & \"::\" & thePage & \"::split::\" & theTitle & \", p.\" & theLabel\n"
682 "return theResult as string\n")))
684 ;;;###autoload
685 (defun org-mac-acrobat-get-page ()
686 (interactive)
687 (message "Applescript: Getting Acrobat page link...")
688 (org-mac-paste-applescript-links (org-mac-as-get-acrobat-page-link)))
690 ;;;###autoload
691 (defun org-mac-acrobat-insert-page ()
692 (interactive)
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"
702 (do-applescript
703 (concat
704 "tell application \"" org-mac-outlook-path "\"\n"
705 (format "open message id %s\n" (substring-no-properties msgid))
706 "activate\n"
707 "end tell")))
709 (defun org-as-get-selected-outlook-mail ()
710 "AppleScript to create links to selected messages in Microsoft Outlook.app."
711 (do-applescript
712 (concat
713 "tell application \"" org-mac-outlook-path "\"\n"
714 "set msgCount to count current messages\n"
715 "if (msgCount < 1) then\n"
716 "return\n"
717 "end if\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"
726 "end repeat\n"
727 "return theLinkList as string\n"
728 "end tell")))
730 (defun org-sh-get-flagged-outlook-mail ()
731 "Shell commands to create links to flagged messages in Microsoft Outlook.app."
732 (mapconcat
733 (lambda (x) ""
734 (concat
735 "mac-outlook:"
736 (mapconcat
737 (lambda (y) "" y)
738 (split-string
739 (shell-command-to-string
740 (format "mdls -raw -name com_microsoft_outlook_recordID -name kMDItemDisplayName \"%s\"" x))
741 "\000")
742 "::split::")
743 "\n"))
744 (with-temp-buffer
745 (let ((coding-system-for-read (or file-name-coding-system 'utf-8))
746 (coding-system-for-write 'utf-8))
747 (shell-command
748 "mdfind com_microsoft_outlook_flagged==1"
749 (current-buffer)))
750 (split-string
751 (buffer-string) "\n" t))
752 ""))
754 ;;;###autoload
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\"")))))
772 ;;;###autoload
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
777 of it."
778 (interactive)
779 (insert (org-mac-outlook-message-get-links "s")))
781 ;;;###autoload
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
787 after heading."
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)
794 (if (not (eobp))
795 (progn
796 (save-excursion
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))
804 (insert "\n")
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"
814 (do-applescript
815 (concat
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"
821 " end if\n"
822 " end repeat\n"
823 " open note window with item 1 of _selectedNote\n"
824 " activate\n"
825 "end tell")))
827 (defun org-as-get-selected-evernote-notes ()
828 "AppleScript to create links to selected notes in Evernote.app."
829 (do-applescript
830 (concat
831 "tell application \"" org-mac-evernote-path "\"\n"
832 " set noteCount to count selection\n"
833 " if (noteCount < 1) then\n"
834 " return\n"
835 " end if\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"
844 " end repeat\n"
845 " return theLinkList as string\n"
846 "end tell\n")))
848 ;;;###autoload
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."
853 (interactive)
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."
869 (do-applescript
870 (concat
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"
876 "return\n"
877 "end if\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"
884 "end repeat\n"
885 "end tell\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)))
898 ;;;###autoload
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."
903 (interactive)
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."
919 (do-applescript
920 (concat
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"
930 "end if\n"
931 "copy theLink to end of theLinkList\n"
932 "end repeat\n"
933 "return theLinkList as string\n"
934 "end tell")))
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"))
940 (do-applescript
941 (concat
942 ;; Get links
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"
953 "end repeat\n"
954 "end repeat\n"
955 "return theLinkList as string\n"
956 "end tell")))
958 ;;;###autoload
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
970 (cond
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\"")))))
975 ;;;###autoload
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."
980 (interactive)
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)
986 ;;;###autoload
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)
998 (if (not (eobp))
999 (progn
1000 (save-excursion
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))
1008 (insert "\n")
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