org-capture: Properly handle `org-extend-today-until' in templates
[org-mode/org-tableheadings.git] / contrib / lisp / org-mac-link.el
blob3dfe43607923609de7d6f60d43128da8b72f3781
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 ;; 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 ;;;###autoload
237 (defun org-mac-grab-link ()
238 "Prompt for an application to grab a link from.
239 When done, go grab the link, and insert it at point."
240 (interactive)
241 (let* ((descriptors
242 `(("F" "inder" org-mac-finder-insert-selected ,org-mac-grab-Finder-app-p)
243 ("m" "ail" org-mac-message-insert-selected ,org-mac-grab-Mail-app-p)
244 ("d" "EVONthink Pro Office" org-mac-devonthink-item-insert-selected
245 ,org-mac-grab-devonthink-app-p)
246 ("o" "utlook" org-mac-outlook-message-insert-selected ,org-mac-grab-Outlook-app-p)
247 ("a" "ddressbook" org-mac-addressbook-insert-selected ,org-mac-grab-Addressbook-app-p)
248 ("s" "afari" org-mac-safari-insert-frontmost-url ,org-mac-grab-Safari-app-p)
249 ("f" "irefox" org-mac-firefox-insert-frontmost-url ,org-mac-grab-Firefox-app-p)
250 ("v" "imperator" org-mac-vimperator-insert-frontmost-url ,org-mac-grab-Firefox+Vimperator-p)
251 ("c" "hrome" org-mac-chrome-insert-frontmost-url ,org-mac-grab-Chrome-app-p)
252 ("e" "evernote" org-mac-evernote-note-insert-selected ,org-mac-grab-Evernote-app-p)
253 ("t" "ogether" org-mac-together-insert-selected ,org-mac-grab-Together-app-p)
254 ("S" "kim" org-mac-skim-insert-page ,org-mac-grab-Skim-app-p)
255 ("A" "crobat" org-mac-acrobat-insert-page ,org-mac-grab-Acrobat-app-p)))
256 (menu-string (make-string 0 ?x))
257 input)
259 ;; Create the menu string for the keymap
260 (mapc (lambda (descriptor)
261 (when (elt descriptor 3)
262 (setf menu-string (concat menu-string
263 "[" (elt descriptor 0) "]"
264 (elt descriptor 1) " "))))
265 descriptors)
266 (setf (elt menu-string (- (length menu-string) 1)) ?:)
268 ;; Prompt the user, and grab the link
269 (message menu-string)
270 (setq input (read-char-exclusive))
271 (mapc (lambda (descriptor)
272 (let ((key (elt (elt descriptor 0) 0))
273 (active (elt descriptor 3))
274 (grab-function (elt descriptor 2)))
275 (when (and active (eq input key))
276 (call-interactively grab-function))))
277 descriptors)))
279 (defun org-mac-paste-applescript-links (as-link-list)
280 "Paste in a list of links from an applescript handler.
281 The links are of the form <link>::split::<name>."
282 (let* ((noquote-as-link-list
283 (if (string-prefix-p "\"" as-link-list)
284 (substring as-link-list 1 -1)
285 as-link-list))
286 (link-list
287 (mapcar (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x)
288 (setq x (match-string 1 x)))
290 (split-string noquote-as-link-list "[\r\n]+")))
291 split-link URL description orglink orglink-insert rtn orglink-list)
292 (while link-list
293 (setq split-link (split-string (pop link-list) "::split::"))
294 (setq URL (car split-link))
295 (setq description (cadr split-link))
296 (when (not (string= URL ""))
297 (setq orglink (org-make-link-string URL description))
298 (push orglink orglink-list)))
299 (setq rtn (mapconcat 'identity orglink-list "\n"))
300 (kill-new rtn)
301 rtn))
304 ;; Handle links from Firefox.app
306 ;; This code allows you to grab the current active url from the main
307 ;; Firefox.app window, and insert it as a link into an org-mode
308 ;; document. Unfortunately, firefox does not expose an applescript
309 ;; dictionary, so this is necessarily introduces some limitations.
311 ;; The applescript to grab the url from Firefox.app uses the System
312 ;; Events application to give focus to the firefox application, select
313 ;; the contents of the url bar, and copy it. It then uses the title of
314 ;; the window as the text of the link. There is no way to grab links
315 ;; from other open tabs, and further, if there is more than one window
316 ;; open, it is not clear which one will be used (though emperically it
317 ;; seems that it is always the last active window).
319 (defun org-as-mac-firefox-get-frontmost-url ()
320 (let ((result
321 (do-applescript
322 (concat
323 "set oldClipboard to the clipboard\n"
324 "set frontmostApplication to path to frontmost application\n"
325 "tell application \"Firefox\"\n"
326 " activate\n"
327 " delay 0.15\n"
328 " tell application \"System Events\"\n"
329 " keystroke \"l\" using {command down}\n"
330 " keystroke \"a\" using {command down}\n"
331 " keystroke \"c\" using {command down}\n"
332 " end tell\n"
333 " delay 0.15\n"
334 " set theUrl to the clipboard\n"
335 " set the clipboard to oldClipboard\n"
336 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
337 "end tell\n"
338 "activate application (frontmostApplication as text)\n"
339 "set links to {}\n"
340 "copy theResult to the end of links\n"
341 "return links as string\n"))))
342 (car (split-string result "[\r\n]+" t))))
344 ;;;###autoload
345 (defun org-mac-firefox-get-frontmost-url ()
346 (interactive)
347 (message "Applescript: Getting Firefox url...")
348 (org-mac-paste-applescript-links (org-as-mac-firefox-get-frontmost-url)))
350 ;;;###autoload
351 (defun org-mac-firefox-insert-frontmost-url ()
352 (interactive)
353 (insert (org-mac-firefox-get-frontmost-url)))
356 ;; Handle links from Google Firefox.app running the Vimperator extension
357 ;; Grab the frontmost url from Firefox+Vimperator. Same limitations are
358 ;; Firefox
360 (defun org-as-mac-vimperator-get-frontmost-url ()
361 (let ((result
362 (do-applescript
363 (concat
364 "set oldClipboard to the clipboard\n"
365 "set frontmostApplication to path to frontmost application\n"
366 "tell application \"Firefox\"\n"
367 " activate\n"
368 " delay 0.15\n"
369 " tell application \"System Events\"\n"
370 " keystroke \"y\"\n"
371 " end tell\n"
372 " delay 0.15\n"
373 " set theUrl to the clipboard\n"
374 " set the clipboard to oldClipboard\n"
375 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
376 "end tell\n"
377 "activate application (frontmostApplication as text)\n"
378 "set links to {}\n"
379 "copy theResult to the end of links\n"
380 "return links as string\n"))))
381 (replace-regexp-in-string
382 "\s+-\s+Vimperator" "" (car (split-string result "[\r\n]+" t)))))
384 ;;;###autoload
385 (defun org-mac-vimperator-get-frontmost-url ()
386 (interactive)
387 (message "Applescript: Getting Vimperator url...")
388 (org-mac-paste-applescript-links (org-as-mac-vimperator-get-frontmost-url)))
390 ;;;###autoload
391 (defun org-mac-vimperator-insert-frontmost-url ()
392 (interactive)
393 (insert (org-mac-vimperator-get-frontmost-url)))
396 ;; Handle links from Google Chrome.app
397 ;; Grab the frontmost url from Google Chrome. Same limitations as
398 ;; Firefox because Chrome doesn't publish an Applescript dictionary
400 (defun org-as-mac-chrome-get-frontmost-url ()
401 (let ((result
402 (do-applescript
403 (concat
404 "set frontmostApplication to path to frontmost application\n"
405 "tell application \"Google Chrome\"\n"
406 " set theUrl to get URL of active tab of first window\n"
407 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
408 "end tell\n"
409 "activate application (frontmostApplication as text)\n"
410 "set links to {}\n"
411 "copy theResult to the end of links\n"
412 "return links as string\n"))))
413 (replace-regexp-in-string
414 "^\"\\|\"$" "" (car (split-string result "[\r\n]+" t)))))
416 ;;;###autoload
417 (defun org-mac-chrome-get-frontmost-url ()
418 (interactive)
419 (message "Applescript: Getting Chrome url...")
420 (org-mac-paste-applescript-links (org-as-mac-chrome-get-frontmost-url)))
422 ;;;###autoload
423 (defun org-mac-chrome-insert-frontmost-url ()
424 (interactive)
425 (insert (org-mac-chrome-get-frontmost-url)))
428 ;; Handle links from Safari.app
429 ;; Grab the frontmost url from Safari.
431 (defun org-as-mac-safari-get-frontmost-url ()
432 (do-applescript
433 (concat
434 "tell application \"Safari\"\n"
435 " set theUrl to URL of document 1\n"
436 " set theName to the name of the document 1\n"
437 " return theUrl & \"::split::\" & theName & \"\n\"\n"
438 "end tell\n")))
440 ;;;###autoload
441 (defun org-mac-safari-get-frontmost-url ()
442 (interactive)
443 (message "Applescript: Getting Safari url...")
444 (org-mac-paste-applescript-links
445 (org-as-mac-safari-get-frontmost-url)))
447 ;;;###autoload
448 (defun org-mac-safari-insert-frontmost-url ()
449 (interactive)
450 (insert (org-mac-safari-get-frontmost-url)))
453 ;; Handle links from together.app
454 (org-link-set-parameters "x-together-item" :follow #'org-mac-together-item-open)
456 (defun org-mac-together-item-open (uid)
457 "Open UID, which is a reference to an item in Together."
458 (shell-command (concat "open -a Together \"x-together-item:" uid "\"")))
460 (defun as-get-selected-together-items ()
461 (do-applescript
462 (concat
463 "tell application \"Together\"\n"
464 " set theLinkList to {}\n"
465 " set theSelection to selected items\n"
466 " repeat with theItem in theSelection\n"
467 " set theLink to (get item link of theItem) & \"::split::\" & (get name of theItem) & \"\n\"\n"
468 " copy theLink to end of theLinkList\n"
469 " end repeat\n"
470 " return theLinkList as string\n"
471 "end tell")))
473 ;;;###autoload
474 (defun org-mac-together-get-selected ()
475 (interactive)
476 (message "Applescript: Getting Togther items...")
477 (org-mac-paste-applescript-links (as-get-selected-together-items)))
479 ;;;###autoload
480 (defun org-mac-together-insert-selected ()
481 (interactive)
482 (insert (org-mac-together-get-selected)))
485 ;; Handle links from Finder.app
487 (defun as-get-selected-finder-items ()
488 (do-applescript
489 (concat
490 "tell application \"Finder\"\n"
491 " set theSelection to the selection\n"
492 " set links to {}\n"
493 " repeat with theItem in theSelection\n"
494 " set theLink to \"file://\" & (POSIX path of (theItem as string)) & \"::split::\" & (get the name of theItem) & \"\n\"\n"
495 " copy theLink to the end of links\n"
496 " end repeat\n"
497 " return links as string\n"
498 "end tell\n")))
500 ;;;###autoload
501 (defun org-mac-finder-item-get-selected ()
502 (interactive)
503 (message "Applescript: Getting Finder items...")
504 (org-mac-paste-applescript-links (as-get-selected-finder-items)))
506 ;;;###autoload
507 (defun org-mac-finder-insert-selected ()
508 (interactive)
509 (insert (org-mac-finder-item-get-selected)))
512 ;; Handle links from AddressBook.app
513 (org-link-set-parameters "addressbook" :follow #'org-mac-addressbook-item-open)
515 (defun org-mac-addressbook-item-open (uid)
516 "Open UID, which is a reference to an item in the addressbook."
517 (shell-command (concat "open \"addressbook:" uid "\"")))
519 (defun as-get-selected-addressbook-items ()
520 (do-applescript
521 (concat
522 "tell application \"Address Book\"\n"
523 " set theSelection to the selection\n"
524 " set links to {}\n"
525 " repeat with theItem in theSelection\n"
526 " set theLink to \"addressbook://\" & (the id of theItem) & \"::split::\" & (the name of theItem) & \"\n\"\n"
527 " copy theLink to the end of links\n"
528 " end repeat\n"
529 " return links as string\n"
530 "end tell\n")))
532 ;;;###autoload
533 (defun org-mac-addressbook-item-get-selected ()
534 (interactive)
535 (message "Applescript: Getting Address Book items...")
536 (org-mac-paste-applescript-links (as-get-selected-addressbook-items)))
538 ;;;###autoload
539 (defun org-mac-addressbook-insert-selected ()
540 (interactive)
541 (insert (org-mac-addressbook-item-get-selected)))
544 ;; Handle links from Skim.app
546 ;; Original code & idea by Christopher Suckling (org-mac-protocol)
548 (org-link-set-parameters "skim" :follow #'org-mac-skim-open)
550 (defun org-mac-skim-open (uri)
551 "Visit page of pdf in Skim"
552 (let* ((page (when (string-match "::\\(.+\\)\\'" uri)
553 (match-string 1 uri)))
554 (document (substring uri 0 (match-beginning 0))))
555 (do-applescript
556 (concat
557 "tell application \"Skim\"\n"
558 "activate\n"
559 "set theDoc to \"" document "\"\n"
560 "set thePage to " page "\n"
561 "open theDoc\n"
562 "go document 1 to page thePage of document 1\n"
563 "end tell"))))
565 (defun as-get-skim-page-link ()
566 (do-applescript
567 (concat
568 "tell application \"Skim\"\n"
569 "set theDoc to front document\n"
570 "set theTitle to (name of theDoc)\n"
571 "set thePath to (path of theDoc)\n"
572 "set thePage to (get index for current page of theDoc)\n"
573 "set theSelection to selection of theDoc\n"
574 "set theContent to contents of (get text for theSelection)\n"
575 "if theContent is missing value then\n"
576 " set theContent to theTitle & \", p. \" & thePage\n"
577 (when org-mac-Skim-highlight-selection-p
578 (concat
579 "else\n"
580 " tell theDoc\n"
581 " set theNote to make note with properties {type:highlight note, selection:theSelection}\n"
582 " set text of theNote to (get text for theSelection)\n"
583 " end tell\n"))
584 "end if\n"
585 "set theLink to \"skim://\" & thePath & \"::\" & thePage & "
586 "\"::split::\" & theContent\n"
587 "end tell\n"
588 "return theLink as string\n")))
590 ;;;###autoload
591 (defun org-mac-skim-get-page ()
592 (interactive)
593 (message "Applescript: Getting Skim page link...")
594 (org-mac-paste-applescript-links (as-get-skim-page-link)))
596 ;;;###autoload
597 (defun org-mac-skim-insert-page ()
598 (interactive)
599 (insert (org-mac-skim-get-page)))
601 ;; Handle links from Adobe Acrobat Pro.app
603 ;; Original code & idea by Christopher Suckling (org-mac-protocol)
605 ;; The URI format is path_to_pdf_file::page_number
607 (org-link-set-parameters "acrobat" :follow #'org-mac-acrobat-open)
609 (defun org-mac-acrobat-open (uri)
610 "Visit page of pdf in Acrobat"
611 (let* ((page (when (string-match "::\\(.+\\)\\'" uri)
612 (match-string 1 uri)))
613 (document (substring uri 0 (match-beginning 0))))
614 (do-applescript
615 (concat
616 "tell application \"Adobe Acrobat Pro\"\n"
617 " activate\n"
618 " set theDoc to \"" document "\"\n"
619 " set thePage to " page "\n"
620 " open theDoc\n"
621 " tell PDF Window 1\n"
622 " goto page thePage\n"
623 " end tell\n"
624 "end tell"))))
626 ;; The applescript returns link in the format
627 ;; "adobe:path_to_pdf_file::page_number::split::document_title, p.page_label"
629 (defun org-mac-as-get-acrobat-page-link ()
630 (do-applescript
631 (concat
632 "tell application \"Adobe Acrobat Pro\"\n"
633 " set theDoc to active doc\n"
634 " set theWindow to (PDF Window 1 of theDoc)\n"
635 " set thePath to (file alias of theDoc)\n"
636 " set theTitle to (name of theWindow)\n"
637 " set thePage to (page number of theWindow)\n"
638 " set theLabel to (label text of (page thePage of theWindow))\n"
639 "end tell\n"
640 "set theResult to \"acrobat:\" & thePath & \"::\" & thePage & \"::split::\" & theTitle & \", p.\" & theLabel\n"
641 "return theResult as string\n")))
643 ;;;###autoload
644 (defun org-mac-acrobat-get-page ()
645 (interactive)
646 (message "Applescript: Getting Acrobat page link...")
647 (org-mac-paste-applescript-links (org-mac-as-get-acrobat-page-link)))
649 ;;;###autoload
650 (defun org-mac-acrobat-insert-page ()
651 (interactive)
652 (insert (org-mac-acrobat-get-page)))
655 ;; Handle links from Microsoft Outlook.app
657 (org-link-set-parameters "mac-outlook" :follow #'org-mac-outlook-message-open)
659 (defun org-mac-outlook-message-open (msgid)
660 "Open a message in Outlook"
661 (do-applescript
662 (concat
663 "tell application \"" org-mac-outlook-path "\"\n"
664 (format "open message id %s\n" (substring-no-properties msgid))
665 "activate\n"
666 "end tell")))
668 (defun org-as-get-selected-outlook-mail ()
669 "AppleScript to create links to selected messages in Microsoft Outlook.app."
670 (do-applescript
671 (concat
672 "tell application \"" org-mac-outlook-path "\"\n"
673 "set msgCount to count current messages\n"
674 "if (msgCount < 1) then\n"
675 "return\n"
676 "end if\n"
677 "set theLinkList to {}\n"
678 "set theSelection to (get current messages)\n"
679 "repeat with theMessage in theSelection\n"
680 "set theID to id of theMessage as string\n"
681 "set theURL to \"mac-outlook:\" & theID\n"
682 "set theSubject to subject of theMessage\n"
683 "set theLink to theURL & \"::split::\" & theSubject & \"\n\"\n"
684 "copy theLink to end of theLinkList\n"
685 "end repeat\n"
686 "return theLinkList as string\n"
687 "end tell")))
689 (defun org-sh-get-flagged-outlook-mail ()
690 "Shell commands to create links to flagged messages in Microsoft Outlook.app."
691 (mapconcat
692 (lambda (x) ""
693 (concat
694 "mac-outlook:"
695 (mapconcat
696 (lambda (y) "" y)
697 (split-string
698 (shell-command-to-string
699 (format "mdls -raw -name com_microsoft_outlook_recordID -name kMDItemDisplayName \"%s\"" x))
700 "\000")
701 "::split::")
702 "\n"))
703 (with-temp-buffer
704 (let ((coding-system-for-read (or file-name-coding-system 'utf-8))
705 (coding-system-for-write 'utf-8))
706 (shell-command
707 "mdfind com_microsoft_outlook_flagged==1"
708 (current-buffer)))
709 (split-string
710 (buffer-string) "\n" t))
711 ""))
713 ;;;###autoload
714 (defun org-mac-outlook-message-get-links (&optional select-or-flag)
715 "Create links to the messages currently selected or flagged in Microsoft Outlook.app.
716 This will use AppleScript to get the message-id and the subject of the
717 messages in Microsoft Outlook.app and make a link out of it.
718 When SELECT-OR-FLAG is \"s\", get the selected messages (this is also
719 the default). When SELECT-OR-FLAG is \"f\", get the flagged messages.
720 The Org-syntax text will be pushed to the kill ring, and also returned."
721 (interactive "sLink to (s)elected or (f)lagged messages: ")
722 (setq select-or-flag (or select-or-flag "s"))
723 (message "Org Mac Outlook: searching mailboxes...")
724 (org-mac-paste-applescript-links
725 (if (string= select-or-flag "s")
726 (org-as-get-selected-outlook-mail)
727 (if (string= select-or-flag "f")
728 (org-sh-get-flagged-outlook-mail)
729 (error "Please select \"s\" or \"f\"")))))
731 ;;;###autoload
732 (defun org-mac-outlook-message-insert-selected ()
733 "Insert a link to the messages currently selected in Microsoft Outlook.app.
734 This will use AppleScript to get the message-id and the subject
735 of the active mail in Microsoft Outlook.app and make a link out
736 of it."
737 (interactive)
738 (insert (org-mac-outlook-message-get-links "s")))
740 ;;;###autoload
741 (defun org-mac-outlook-message-insert-flagged (org-buffer org-heading)
742 "Asks for an org buffer and a heading within it, and replace message links.
743 If heading exists, delete all mac-outlook:// links within
744 heading's first level. If heading doesn't exist, create it at
745 point-max. Insert list of mac-outlook:// links to flagged mail
746 after heading."
747 (interactive "bBuffer in which to insert links: \nsHeading after which to insert links: ")
748 (with-current-buffer org-buffer
749 (goto-char (point-min))
750 (let ((isearch-forward t)
751 (message-re "\\[\\[\\(mac-outlook:\\)\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"))
752 (if (org-goto-local-search-headings org-heading nil t)
753 (if (not (eobp))
754 (progn
755 (save-excursion
756 (while (re-search-forward
757 message-re (save-excursion (outline-next-heading)) t)
758 (delete-region (match-beginning 0) (match-end 0)))
759 (insert "\n" (org-mac-outlook-message-get-links "f")))
760 (flush-lines "^$" (point) (outline-next-heading)))
761 (insert "\n" (org-mac-outlook-message-get-links "f")))
762 (goto-char (point-max))
763 (insert "\n")
764 (org-insert-heading nil t)
765 (insert org-heading "\n" (org-mac-outlook-message-get-links "f"))))))
767 ;; Handle links from Evernote.app
769 (org-link-set-parameters "mac-evernote" :follow #'org-mac-evernote-note-open)
771 (defun org-mac-evernote-note-open (noteid)
772 "Open a note in Evernote"
773 (do-applescript
774 (concat
775 "tell application \"" org-mac-evernote-path "\"\n"
776 " set theNotes to get every note of every notebook where its local id is \"" (substring-no-properties noteid) "\"\n"
777 " repeat with _note in theNotes\n"
778 " if length of _note is not 0 then\n"
779 " set _selectedNote to _note\n"
780 " end if\n"
781 " end repeat\n"
782 " open note window with item 1 of _selectedNote\n"
783 " activate\n"
784 "end tell")))
786 (defun org-as-get-selected-evernote-notes ()
787 "AppleScript to create links to selected notes in Evernote.app."
788 (do-applescript
789 (concat
790 "tell application \"" org-mac-evernote-path "\"\n"
791 " set noteCount to count selection\n"
792 " if (noteCount < 1) then\n"
793 " return\n"
794 " end if\n"
795 " set theLinkList to {}\n"
796 " set theSelection to selection\n"
797 " repeat with theNote in theSelection\n"
798 " set theTitle to title of theNote\n"
799 " set theID to local id of theNote\n"
800 " set theURL to \"mac-evernote:\" & theID\n"
801 " set theLink to theURL & \"::split::\" & theTitle & \"\n\"\n"
802 " copy theLink to end of theLinkList\n"
803 " end repeat\n"
804 " return theLinkList as string\n"
805 "end tell\n")))
807 ;;;###autoload
808 (defun org-mac-evernote-note-insert-selected ()
809 "Insert a link to the notes currently selected in Evernote.app.
810 This will use AppleScript to get the note id and the title of the
811 note(s) in Evernote.app and make a link out of it/them."
812 (interactive)
813 (message "Org Mac Evernote: searching notes...")
814 (insert (org-mac-paste-applescript-links
815 (org-as-get-selected-evernote-notes))))
818 ;; Handle links from DEVONthink Pro Office.app
820 (org-link-set-parameters "x-devonthink-item" :follow #'org-devonthink-item-open)
822 (defun org-devonthink-item-open (uid)
823 "Open UID, which is a reference to an item in DEVONthink Pro Office."
824 (shell-command (concat "open \"x-devonthink-item:" uid "\"")))
826 (defun org-as-get-selected-devonthink-item ()
827 "AppleScript to create links to selected items in DEVONthink Pro Office.app."
828 (do-applescript
829 (concat
830 "set theLinkList to {}\n"
831 "tell application \"DEVONthink Pro\"\n"
832 "set selectedRecords to selection\n"
833 "set selectionCount to count of selectedRecords\n"
834 "if (selectionCount < 1) then\n"
835 "return\n"
836 "end if\n"
837 "repeat with theRecord in selectedRecords\n"
838 "set theID to uuid of theRecord\n"
839 "set theURL to \"x-devonthink-item:\" & theID\n"
840 "set theSubject to name of theRecord\n"
841 "set theLink to theURL & \"::split::\" & theSubject & \"\n\"\n"
842 "copy theLink to end of theLinkList\n"
843 "end repeat\n"
844 "end tell\n"
845 "return theLinkList as string"
848 (defun org-mac-devonthink-get-links ()
849 "Create links to the item(s) currently selected in DEVONthink Pro Office.
850 This will use AppleScript to get the `uuid' and the `name' of the
851 selected items in DEVONthink Pro Office.app and make links out of
852 it/them. This function will push the Org-syntax text to the kill
853 ring, and also return it."
854 (message "Org Mac DEVONthink: looking for selected items...")
855 (org-mac-paste-applescript-links (org-as-get-selected-devonthink-item)))
857 ;;;###autoload
858 (defun org-mac-devonthink-item-insert-selected ()
859 "Insert a link to the item(s) currently selected in DEVONthink Pro Office.
860 This will use AppleScript to get the `uuid'(s) and the name(s) of the
861 selected items in DEVONthink Pro Office and make link(s) out of it/them."
862 (interactive)
863 (insert (org-mac-devonthink-get-links)))
866 ;; Handle links from Mail.app
868 (org-link-set-parameters "message" :follow #'org-mac-message-open)
870 (defun org-mac-message-open (message-id)
871 "Visit the message with MESSAGE-ID.
872 This will use the command `open' with the message URL."
873 (start-process (concat "open message:" message-id) nil
874 "open" (concat "message://<" (substring message-id 2) ">")))
876 (defun org-as-get-selected-mail ()
877 "AppleScript to create links to selected messages in Mail.app."
878 (do-applescript
879 (concat
880 "tell application \"Mail\"\n"
881 "set theLinkList to {}\n"
882 "set theSelection to selection\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"
887 "if (theLinkList is not equal to {}) then\n"
888 "set theLink to \"\n\" & theLink\n"
889 "end if\n"
890 "copy theLink to end of theLinkList\n"
891 "end repeat\n"
892 "return theLinkList as string\n"
893 "end tell")))
895 (defun org-as-get-flagged-mail ()
896 "AppleScript to create links to flagged messages in Mail.app."
897 (unless org-mac-mail-account
898 (error "You must set org-mac-mail-account"))
899 (do-applescript
900 (concat
901 ;; Get links
902 "tell application \"Mail\"\n"
903 "set theMailboxes to every mailbox of account \"" org-mac-mail-account "\"\n"
904 "set theLinkList to {}\n"
905 "repeat with aMailbox in theMailboxes\n"
906 "set theSelection to (every message in aMailbox whose flagged status = true)\n"
907 "repeat with theMessage in theSelection\n"
908 "set theID to message id of theMessage\n"
909 "set theSubject to subject of theMessage\n"
910 "set theLink to \"message://\" & theID & \"::split::\" & theSubject & \"\n\"\n"
911 "copy theLink to end of theLinkList\n"
912 "end repeat\n"
913 "end repeat\n"
914 "return theLinkList as string\n"
915 "end tell")))
917 ;;;###autoload
918 (defun org-mac-message-get-links (&optional select-or-flag)
919 "Create links to the messages currently selected or flagged in Mail.app.
920 This will use AppleScript to get the message-id and the subject of the
921 messages in Mail.app and make a link out of it.
922 When SELECT-OR-FLAG is \"s\", get the selected messages (this is also
923 the default). When SELECT-OR-FLAG is \"f\", get the flagged messages.
924 The Org-syntax text will be pushed to the kill ring, and also returned."
925 (interactive "sLink to (s)elected or (f)lagged messages: ")
926 (setq select-or-flag (or select-or-flag "s"))
927 (message "AppleScript: searching mailboxes...")
928 (org-mac-paste-applescript-links
929 (cond
930 ((string= select-or-flag "s") (org-as-get-selected-mail))
931 ((string= select-or-flag "f") (org-as-get-flagged-mail))
932 (t (error "Please select \"s\" or \"f\"")))))
934 ;;;###autoload
935 (defun org-mac-message-insert-selected ()
936 "Insert a link to the messages currently selected in Mail.app.
937 This will use AppleScript to get the message-id and the subject of the
938 active mail in Mail.app and make a link out of it."
939 (interactive)
940 (insert (org-mac-message-get-links "s")))
942 ;; The following line is for backward compatibility
943 (defalias 'org-mac-message-insert-link 'org-mac-message-insert-selected)
945 ;;;###autoload
946 (defun org-mac-message-insert-flagged (org-buffer org-heading)
947 "Asks for an org buffer and a heading within it, and replace message links.
948 If heading exists, delete all message:// links within heading's first
949 level. If heading doesn't exist, create it at point-max. Insert
950 list of message:// links to flagged mail after heading."
951 (interactive "bBuffer in which to insert links: \nsHeading after which to insert links: ")
952 (with-current-buffer org-buffer
953 (goto-char (point-min))
954 (let ((isearch-forward t)
955 (message-re "\\[\\[\\(message:\\)\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"))
956 (if (org-goto-local-search-headings org-heading nil t)
957 (if (not (eobp))
958 (progn
959 (save-excursion
960 (while (re-search-forward
961 message-re (save-excursion (outline-next-heading)) t)
962 (delete-region (match-beginning 0) (match-end 0)))
963 (insert "\n" (org-mac-message-get-links "f")))
964 (flush-lines "^$" (point) (outline-next-heading)))
965 (insert "\n" (org-mac-message-get-links "f")))
966 (goto-char (point-max))
967 (insert "\n")
968 (org-insert-heading nil t)
969 (insert org-heading "\n" (org-mac-message-get-links "f"))))))
972 (provide 'org-mac-link)
974 ;;; org-mac-link.el ends here