Add detailed location to "org-mode fontification error"
[org-mode/org-tableheadings.git] / contrib / lisp / org-mac-link.el
blob076483c38419c4b751d170a64a23f6bf77f7c973
1 ;;; org-mac-link.el --- Insert org-mode links to items selected in various Mac apps
2 ;;
3 ;; Copyright (c) 2010-2016 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
446 (org-add-link-type "x-together-item" 'org-mac-together-item-open)
448 (defun org-mac-together-item-open (uid)
449 "Open UID, which is a reference to an item in Together."
450 (shell-command (concat "open -a Together \"x-together-item:" uid "\"")))
452 (defun as-get-selected-together-items ()
453 (do-applescript
454 (concat
455 "tell application \"Together\"\n"
456 " set theLinkList to {}\n"
457 " set theSelection to selected items\n"
458 " repeat with theItem in theSelection\n"
459 " set theLink to (get item link of theItem) & \"::split::\" & (get name of theItem) & \"\n\"\n"
460 " copy theLink to end of theLinkList\n"
461 " end repeat\n"
462 " return theLinkList as string\n"
463 "end tell")))
465 (defun org-mac-together-get-selected ()
466 (interactive)
467 (message "Applescript: Getting Togther items...")
468 (org-mac-paste-applescript-links (as-get-selected-together-items)))
470 (defun org-mac-together-insert-selected ()
471 (interactive)
472 (insert (org-mac-together-get-selected)))
475 ;; Handle links from Finder.app
477 (defun as-get-selected-finder-items ()
478 (do-applescript
479 (concat
480 "tell application \"Finder\"\n"
481 " set theSelection to the selection\n"
482 " set links to {}\n"
483 " repeat with theItem in theSelection\n"
484 " set theLink to \"file://\" & (POSIX path of (theItem as string)) & \"::split::\" & (get the name of theItem) & \"\n\"\n"
485 " copy theLink to the end of links\n"
486 " end repeat\n"
487 " return links as string\n"
488 "end tell\n")))
490 (defun org-mac-finder-item-get-selected ()
491 (interactive)
492 (message "Applescript: Getting Finder items...")
493 (org-mac-paste-applescript-links (as-get-selected-finder-items)))
495 (defun org-mac-finder-insert-selected ()
496 (interactive)
497 (insert (org-mac-finder-item-get-selected)))
500 ;; Handle links from AddressBook.app
502 (org-add-link-type "addressbook" 'org-mac-addressbook-item-open)
504 (defun org-mac-addressbook-item-open (uid)
505 "Open UID, which is a reference to an item in the addressbook."
506 (shell-command (concat "open \"addressbook:" uid "\"")))
508 (defun as-get-selected-addressbook-items ()
509 (do-applescript
510 (concat
511 "tell application \"Address Book\"\n"
512 " set theSelection to the selection\n"
513 " set links to {}\n"
514 " repeat with theItem in theSelection\n"
515 " set theLink to \"addressbook://\" & (the id of theItem) & \"::split::\" & (the name of theItem) & \"\n\"\n"
516 " copy theLink to the end of links\n"
517 " end repeat\n"
518 " return links as string\n"
519 "end tell\n")))
521 (defun org-mac-addressbook-item-get-selected ()
522 (interactive)
523 (message "Applescript: Getting Address Book items...")
524 (org-mac-paste-applescript-links (as-get-selected-addressbook-items)))
526 (defun org-mac-addressbook-insert-selected ()
527 (interactive)
528 (insert (org-mac-addressbook-item-get-selected)))
531 ;; Handle links from Skim.app
533 ;; Original code & idea by Christopher Suckling (org-mac-protocol)
535 (org-add-link-type "skim" 'org-mac-skim-open)
537 (defun org-mac-skim-open (uri)
538 "Visit page of pdf in Skim"
539 (let* ((page (when (string-match "::\\(.+\\)\\'" uri)
540 (match-string 1 uri)))
541 (document (substring uri 0 (match-beginning 0))))
542 (do-applescript
543 (concat
544 "tell application \"Skim\"\n"
545 "activate\n"
546 "set theDoc to \"" document "\"\n"
547 "set thePage to " page "\n"
548 "open theDoc\n"
549 "go document 1 to page thePage of document 1\n"
550 "end tell"))))
552 (defun as-get-skim-page-link ()
553 (do-applescript
554 (concat
555 "tell application \"Skim\"\n"
556 "set theDoc to front document\n"
557 "set theTitle to (name of theDoc)\n"
558 "set thePath to (path of theDoc)\n"
559 "set thePage to (get index for current page of theDoc)\n"
560 "set theSelection to selection of theDoc\n"
561 "set theContent to contents of (get text for theSelection)\n"
562 "if theContent is missing value then\n"
563 " set theContent to theTitle & \", p. \" & thePage\n"
564 (when org-mac-Skim-highlight-selection-p
565 (concat
566 "else\n"
567 " tell theDoc\n"
568 " set theNote to make note with properties {type:highlight note, selection:theSelection}\n"
569 " set text of theNote to (get text for theSelection)\n"
570 " end tell\n"))
571 "end if\n"
572 "set theLink to \"skim://\" & thePath & \"::\" & thePage & "
573 "\"::split::\" & theContent\n"
574 "end tell\n"
575 "return theLink as string\n")))
577 (defun org-mac-skim-get-page ()
578 (interactive)
579 (message "Applescript: Getting Skim page link...")
580 (org-mac-paste-applescript-links (as-get-skim-page-link)))
582 (defun org-mac-skim-insert-page ()
583 (interactive)
584 (insert (org-mac-skim-get-page)))
586 ;; Handle links from Adobe Acrobat Pro.app
588 ;; Original code & idea by Christopher Suckling (org-mac-protocol)
590 ;; The URI format is path_to_pdf_file::page_number
592 (org-add-link-type "acrobat" 'org-mac-acrobat-open)
594 (defun org-mac-acrobat-open (uri)
595 "Visit page of pdf in Acrobat"
596 (let* ((page (when (string-match "::\\(.+\\)\\'" uri)
597 (match-string 1 uri)))
598 (document (substring uri 0 (match-beginning 0))))
599 (do-applescript
600 (concat
601 "tell application \"Adobe Acrobat Pro\"\n"
602 " activate\n"
603 " set theDoc to \"" document "\"\n"
604 " set thePage to " page "\n"
605 " open theDoc\n"
606 " tell PDF Window 1\n"
607 " goto page thePage\n"
608 " end tell\n"
609 "end tell"))))
611 ;; The applescript returns link in the format
612 ;; "adobe:path_to_pdf_file::page_number::split::document_title, p.page_label"
614 (defun org-mac-as-get-acrobat-page-link ()
615 (do-applescript
616 (concat
617 "tell application \"Adobe Acrobat Pro\"\n"
618 " set theDoc to active doc\n"
619 " set theWindow to (PDF Window 1 of theDoc)\n"
620 " set thePath to (file alias of theDoc)\n"
621 " set theTitle to (name of theWindow)\n"
622 " set thePage to (page number of theWindow)\n"
623 " set theLabel to (label text of (page thePage of theWindow))\n"
624 "end tell\n"
625 "set theResult to \"acrobat:\" & thePath & \"::\" & thePage & \"::split::\" & theTitle & \", p.\" & theLabel\n"
626 "return theResult as string\n")))
628 (defun org-mac-acrobat-get-page ()
629 (interactive)
630 (message "Applescript: Getting Acrobat page link...")
631 (org-mac-paste-applescript-links (org-mac-as-get-acrobat-page-link)))
633 (defun org-mac-acrobat-insert-page ()
634 (interactive)
635 (insert (org-mac-acrobat-get-page)))
638 ;; Handle links from Microsoft Outlook.app
640 (org-add-link-type "mac-outlook" 'org-mac-outlook-message-open)
642 (defun org-mac-outlook-message-open (msgid)
643 "Open a message in Outlook"
644 (do-applescript
645 (concat
646 "tell application \"" org-mac-outlook-path "\"\n"
647 (format "open message id %s\n" (substring-no-properties msgid))
648 "activate\n"
649 "end tell")))
651 (defun org-as-get-selected-outlook-mail ()
652 "AppleScript to create links to selected messages in Microsoft Outlook.app."
653 (do-applescript
654 (concat
655 "tell application \"" org-mac-outlook-path "\"\n"
656 "set msgCount to count current messages\n"
657 "if (msgCount < 1) then\n"
658 "return\n"
659 "end if\n"
660 "set theLinkList to {}\n"
661 "set theSelection to (get current messages)\n"
662 "repeat with theMessage in theSelection\n"
663 "set theID to id of theMessage as string\n"
664 "set theURL to \"mac-outlook:\" & theID\n"
665 "set theSubject to subject of theMessage\n"
666 "set theLink to theURL & \"::split::\" & theSubject & \"\n\"\n"
667 "copy theLink to end of theLinkList\n"
668 "end repeat\n"
669 "return theLinkList as string\n"
670 "end tell")))
672 (defun org-sh-get-flagged-outlook-mail ()
673 "Shell commands to create links to flagged messages in Microsoft Outlook.app."
674 (mapconcat
675 (lambda (x) ""
676 (concat
677 "mac-outlook:"
678 (mapconcat
679 (lambda (y) "" y)
680 (split-string
681 (shell-command-to-string
682 (format "mdls -raw -name com_microsoft_outlook_recordID -name kMDItemDisplayName \"%s\"" x))
683 "\000")
684 "::split::")
685 "\n"))
686 (with-temp-buffer
687 (let ((coding-system-for-read (or file-name-coding-system 'utf-8))
688 (coding-system-for-write 'utf-8))
689 (shell-command
690 "mdfind com_microsoft_outlook_flagged==1"
691 (current-buffer)))
692 (split-string
693 (buffer-string) "\n" t))
694 ""))
696 (defun org-mac-outlook-message-get-links (&optional select-or-flag)
697 "Create links to the messages currently selected or flagged in Microsoft Outlook.app.
698 This will use AppleScript to get the message-id and the subject of the
699 messages in Microsoft Outlook.app and make a link out of it.
700 When SELECT-OR-FLAG is \"s\", get the selected messages (this is also
701 the default). When SELECT-OR-FLAG is \"f\", get the flagged messages.
702 The Org-syntax text will be pushed to the kill ring, and also returned."
703 (interactive "sLink to (s)elected or (f)lagged messages: ")
704 (setq select-or-flag (or select-or-flag "s"))
705 (message "Org Mac Outlook: searching mailboxes...")
706 (org-mac-paste-applescript-links
707 (if (string= select-or-flag "s")
708 (org-as-get-selected-outlook-mail)
709 (if (string= select-or-flag "f")
710 (org-sh-get-flagged-outlook-mail)
711 (error "Please select \"s\" or \"f\"")))))
713 (defun org-mac-outlook-message-insert-selected ()
714 "Insert a link to the messages currently selected in Microsoft Outlook.app.
715 This will use AppleScript to get the message-id and the subject
716 of the active mail in Microsoft Outlook.app and make a link out
717 of it."
718 (interactive)
719 (insert (org-mac-outlook-message-get-links "s")))
721 (defun org-mac-outlook-message-insert-flagged (org-buffer org-heading)
722 "Asks for an org buffer and a heading within it, and replace message links.
723 If heading exists, delete all mac-outlook:// links within
724 heading's first level. If heading doesn't exist, create it at
725 point-max. Insert list of mac-outlook:// links to flagged mail
726 after heading."
727 (interactive "bBuffer in which to insert links: \nsHeading after which to insert links: ")
728 (with-current-buffer org-buffer
729 (goto-char (point-min))
730 (let ((isearch-forward t)
731 (message-re "\\[\\[\\(mac-outlook:\\)\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"))
732 (if (org-goto-local-search-headings org-heading nil t)
733 (if (not (eobp))
734 (progn
735 (save-excursion
736 (while (re-search-forward
737 message-re (save-excursion (outline-next-heading)) t)
738 (delete-region (match-beginning 0) (match-end 0)))
739 (insert "\n" (org-mac-outlook-message-get-links "f")))
740 (flush-lines "^$" (point) (outline-next-heading)))
741 (insert "\n" (org-mac-outlook-message-get-links "f")))
742 (goto-char (point-max))
743 (insert "\n")
744 (org-insert-heading nil t)
745 (insert org-heading "\n" (org-mac-outlook-message-get-links "f"))))))
747 ;; Handle links from Evernote.app
749 (org-add-link-type "mac-evernote" 'org-mac-evernote-note-open)
751 (defun org-mac-evernote-note-open (noteid)
752 "Open a note in Evernote"
753 (do-applescript
754 (concat
755 "tell application \"" org-mac-evernote-path "\"\n"
756 " set theNotes to get every note of every notebook where its local id is \"" (substring-no-properties noteid) "\"\n"
757 " repeat with _note in theNotes\n"
758 " if length of _note is not 0 then\n"
759 " set _selectedNote to _note\n"
760 " end if\n"
761 " end repeat\n"
762 " open note window with item 1 of _selectedNote\n"
763 " activate\n"
764 "end tell")))
766 (defun org-as-get-selected-evernote-notes ()
767 "AppleScript to create links to selected notes in Evernote.app."
768 (do-applescript
769 (concat
770 "tell application \"" org-mac-evernote-path "\"\n"
771 " set noteCount to count selection\n"
772 " if (noteCount < 1) then\n"
773 " return\n"
774 " end if\n"
775 " set theLinkList to {}\n"
776 " set theSelection to selection\n"
777 " repeat with theNote in theSelection\n"
778 " set theTitle to title of theNote\n"
779 " set theID to local id of theNote\n"
780 " set theURL to \"mac-evernote:\" & theID\n"
781 " set theLink to theURL & \"::split::\" & theTitle & \"\n\"\n"
782 " copy theLink to end of theLinkList\n"
783 " end repeat\n"
784 " return theLinkList as string\n"
785 "end tell\n")))
787 (defun org-mac-evernote-note-insert-selected ()
788 "Insert a link to the notes currently selected in Evernote.app.
789 This will use AppleScript to get the note id and the title of the
790 note(s) in Evernote.app and make a link out of it/them."
791 (interactive)
792 (message "Org Mac Evernote: searching notes...")
793 (insert (org-mac-paste-applescript-links
794 (org-as-get-selected-evernote-notes))))
797 ;; Handle links from DEVONthink Pro Office.app
799 (org-add-link-type "x-devonthink-item" 'org-devonthink-item-open)
801 (defun org-devonthink-item-open (uid)
802 "Open UID, which is a reference to an item in DEVONthink Pro Office."
803 (shell-command (concat "open \"x-devonthink-item:" uid "\"")))
805 (defun org-as-get-selected-devonthink-item ()
806 "AppleScript to create links to selected items in DEVONthink Pro Office.app."
807 (do-applescript
808 (concat
809 "set theLinkList to {}\n"
810 "tell application \"DEVONthink Pro\"\n"
811 "set selectedRecords to selection\n"
812 "set selectionCount to count of selectedRecords\n"
813 "if (selectionCount < 1) then\n"
814 "return\n"
815 "end if\n"
816 "repeat with theRecord in selectedRecords\n"
817 "set theID to uuid of theRecord\n"
818 "set theURL to \"x-devonthink-item:\" & theID\n"
819 "set theSubject to name of theRecord\n"
820 "set theLink to theURL & \"::split::\" & theSubject & \"\n\"\n"
821 "copy theLink to end of theLinkList\n"
822 "end repeat\n"
823 "end tell\n"
824 "return theLinkList as string"
827 (defun org-mac-devonthink-get-links ()
828 "Create links to the item(s) currently selected in DEVONthink Pro Office.
829 This will use AppleScript to get the `uuid' and the `name' of the
830 selected items in DEVONthink Pro Office.app and make links out of
831 it/them. This function will push the Org-syntax text to the kill
832 ring, and also return it."
833 (message "Org Mac DEVONthink: looking for selected items...")
834 (org-mac-paste-applescript-links (org-as-get-selected-devonthink-item)))
836 (defun org-mac-devonthink-item-insert-selected ()
837 "Insert a link to the item(s) currently selected in DEVONthink Pro Office.
838 This will use AppleScript to get the `uuid'(s) and the name(s) of the
839 selected items in DEVONthink Pro Office and make link(s) out of it/them."
840 (interactive)
841 (insert (org-mac-devonthink-get-links)))
844 ;; Handle links from Mail.app
846 (org-add-link-type "message" 'org-mac-message-open)
848 (defun org-mac-message-open (message-id)
849 "Visit the message with MESSAGE-ID.
850 This will use the command `open' with the message URL."
851 (start-process (concat "open message:" message-id) nil
852 "open" (concat "message://<" (substring message-id 2) ">")))
854 (defun org-as-get-selected-mail ()
855 "AppleScript to create links to selected messages in Mail.app."
856 (do-applescript
857 (concat
858 "tell application \"Mail\"\n"
859 "set theLinkList to {}\n"
860 "set theSelection to selection\n"
861 "repeat with theMessage in theSelection\n"
862 "set theID to message id of theMessage\n"
863 "set theSubject to subject of theMessage\n"
864 "set theLink to \"message://\" & theID & \"::split::\" & theSubject\n"
865 "if (theLinkList is not equal to {}) then\n"
866 "set theLink to \"\n\" & theLink\n"
867 "end if\n"
868 "copy theLink to end of theLinkList\n"
869 "end repeat\n"
870 "return theLinkList as string\n"
871 "end tell")))
873 (defun org-as-get-flagged-mail ()
874 "AppleScript to create links to flagged messages in Mail.app."
875 (unless org-mac-mail-account
876 (error "You must set org-mac-mail-account"))
877 (do-applescript
878 (concat
879 ;; Get links
880 "tell application \"Mail\"\n"
881 "set theMailboxes to every mailbox of account \"" org-mac-mail-account "\"\n"
882 "set theLinkList to {}\n"
883 "repeat with aMailbox in theMailboxes\n"
884 "set theSelection to (every message in aMailbox whose flagged status = true)\n"
885 "repeat with theMessage in theSelection\n"
886 "set theID to message id of theMessage\n"
887 "set theSubject to subject of theMessage\n"
888 "set theLink to \"message://\" & theID & \"::split::\" & theSubject & \"\n\"\n"
889 "copy theLink to end of theLinkList\n"
890 "end repeat\n"
891 "end repeat\n"
892 "return theLinkList as string\n"
893 "end tell")))
895 (defun org-mac-message-get-links (&optional select-or-flag)
896 "Create links to the messages currently selected or flagged in Mail.app.
897 This will use AppleScript to get the message-id and the subject of the
898 messages in Mail.app and make a link out of it.
899 When SELECT-OR-FLAG is \"s\", get the selected messages (this is also
900 the default). When SELECT-OR-FLAG is \"f\", get the flagged messages.
901 The Org-syntax text will be pushed to the kill ring, and also returned."
902 (interactive "sLink to (s)elected or (f)lagged messages: ")
903 (setq select-or-flag (or select-or-flag "s"))
904 (message "AppleScript: searching mailboxes...")
905 (org-mac-paste-applescript-links
906 (cond
907 ((string= select-or-flag "s") (org-as-get-selected-mail))
908 ((string= select-or-flag "f") (org-as-get-flagged-mail))
909 (t (error "Please select \"s\" or \"f\"")))))
911 (defun org-mac-message-insert-selected ()
912 "Insert a link to the messages currently selected in Mail.app.
913 This will use AppleScript to get the message-id and the subject of the
914 active mail in Mail.app and make a link out of it."
915 (interactive)
916 (insert (org-mac-message-get-links "s")))
918 ;; The following line is for backward compatibility
919 (defalias 'org-mac-message-insert-link 'org-mac-message-insert-selected)
921 (defun org-mac-message-insert-flagged (org-buffer org-heading)
922 "Asks for an org buffer and a heading within it, and replace message links.
923 If heading exists, delete all message:// links within heading's first
924 level. If heading doesn't exist, create it at point-max. Insert
925 list of message:// links to flagged mail after heading."
926 (interactive "bBuffer in which to insert links: \nsHeading after which to insert links: ")
927 (with-current-buffer org-buffer
928 (goto-char (point-min))
929 (let ((isearch-forward t)
930 (message-re "\\[\\[\\(message:\\)\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"))
931 (if (org-goto-local-search-headings org-heading nil t)
932 (if (not (eobp))
933 (progn
934 (save-excursion
935 (while (re-search-forward
936 message-re (save-excursion (outline-next-heading)) t)
937 (delete-region (match-beginning 0) (match-end 0)))
938 (insert "\n" (org-mac-message-get-links "f")))
939 (flush-lines "^$" (point) (outline-next-heading)))
940 (insert "\n" (org-mac-message-get-links "f")))
941 (goto-char (point-max))
942 (insert "\n")
943 (org-insert-heading nil t)
944 (insert org-heading "\n" (org-mac-message-get-links "f"))))))
947 (provide 'org-mac-link)
949 ;;; org-mac-link.el ends here