Merge branch 'maint'
[org-mode.git] / contrib / lisp / org-mac-link-grabber.el
blob59f4820f7b86ba8c79391c112525c38c4a067903
1 ;;; org-mac-link-grabber.el --- Grab links and url from various mac
2 ;; Application and insert them as links into org-mode documents
3 ;;
4 ;; Copyright (c) 2010-2013 Free Software Foundation, Inc.
5 ;;
6 ;; Author: Anthony Lander <anthony.lander@gmail.com>
7 ;; Version: 1.0.1
8 ;; Keywords: org, mac, hyperlink
9 ;;
10 ;; This file is not part of GNU Emacs.
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; any later version.
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This code allows you to grab either the current selected items, or
28 ;; the frontmost url in various mac appliations, and insert them as
29 ;; hyperlinks into the current org-mode document at point.
31 ;; This code is heavily based on, and indeed requires,
32 ;; org-mac-message.el written by John Weigley and Christopher
33 ;; Suckling.
35 ;; Detailed comments for each application interface are inlined with
36 ;; the code. Here is a brief overview of how the code interacts with
37 ;; each application:
39 ;; Finder.app - grab links to the selected files in the frontmost window
40 ;; Mail.app - grab links to the selected messages in the message list
41 ;; AddressBook.app - Grab links to the selected addressbook Cards
42 ;; Firefox.app - Grab the url of the frontmost tab in the frontmost window
43 ;; Vimperator/Firefox.app - Grab the url of the frontmost tab in the frontmost window
44 ;; Safari.app - Grab the url of the frontmost tab in the frontmost window
45 ;; Google Chrome.app - Grab the url of the frontmost tab in the frontmost window
46 ;; Together.app - Grab links to the selected items in the library list
49 ;; Installation:
51 ;; add (require 'org-mac-link-grabber) to your .emacs, and optionally
52 ;; bind a key to activate the link grabber menu, like this:
54 ;; (add-hook 'org-mode-hook (lambda ()
55 ;; (define-key org-mode-map (kbd "C-c g") 'omlg-grab-link)))
58 ;; Usage:
60 ;; Type C-c g (or whatever key you defined, as above), or type M-x
61 ;; omlg-grab-link RET to activate the link grabber. This will present
62 ;; you with a menu to choose an application from which to grab a link
63 ;; to insert at point. You may also type C-g to abort.
65 ;; Customizing:
67 ;; You may customize which applications appear in the grab menu by
68 ;; customizing the group org-mac-link-grabber. Changes take effect
69 ;; immediately.
72 ;;; Code:
74 (require 'org)
75 (require 'org-mac-message)
77 (defgroup org-mac-link-grabber nil
78 "Options concerning grabbing links from external Mac
79 applications and inserting them in org documents"
80 :tag "Org Mac link grabber"
81 :group 'org-link)
83 (defcustom org-mac-grab-Finder-app-p t
84 "Enable menu option [F]inder to grab links from the Finder"
85 :tag "Grab Finder.app links"
86 :group 'org-mac-link-grabber
87 :type 'boolean)
89 (defcustom org-mac-grab-Mail-app-p t
90 "Enable menu option [m]ail to grab links from Mail.app"
91 :tag "Grab Mail.app links"
92 :group 'org-mac-link-grabber
93 :type 'boolean)
95 (defcustom org-mac-grab-Addressbook-app-p t
96 "Enable menu option [a]ddressbook to grab links from AddressBook.app"
97 :tag "Grab AddressBook.app links"
98 :group 'org-mac-link-grabber
99 :type 'boolean)
101 (defcustom org-mac-grab-Safari-app-p t
102 "Enable menu option [s]afari to grab links from Safari.app"
103 :tag "Grab Safari.app links"
104 :group 'org-mac-link-grabber
105 :type 'boolean)
107 (defcustom org-mac-grab-Firefox-app-p t
108 "Enable menu option [f]irefox to grab links from Firefox.app"
109 :tag "Grab Firefox.app links"
110 :group 'org-mac-link-grabber
111 :type 'boolean)
113 (defcustom org-mac-grab-Firefox+Vimperator-p nil
114 "Enable menu option [v]imperator to grab links from Firefox.app running the Vimperator plugin"
115 :tag "Grab Vimperator/Firefox.app links"
116 :group 'org-mac-link-grabber
117 :type 'boolean)
119 (defcustom org-mac-grab-Chrome-app-p t
120 "Enable menu option [f]irefox to grab links from Google Chrome.app"
121 :tag "Grab Google Chrome.app links"
122 :group 'org-mac-link-grabber
123 :type 'boolean)
125 (defcustom org-mac-grab-Together-app-p nil
126 "Enable menu option [t]ogether to grab links from Together.app"
127 :tag "Grab Together.app links"
128 :group 'org-mac-link-grabber
129 :type 'boolean)
131 (defcustom org-mac-grab-Skim-app-p
132 (< 0 (length (shell-command-to-string
133 "mdfind kMDItemCFBundleIdentifier == 'net.sourceforge.skim-app.skim'")))
134 "Enable menu option [S]kim to grab page links from Skim.app"
135 :tag "Grab Skim.app page links"
136 :group 'org-mac-link-grabber
137 :type 'boolean)
139 (defcustom org-mac-Skim-highlight-selection-p nil
140 "Highlight (using notes) the selection (if present) when grabbing the a link from Skim.app"
141 :tag "Highlight selection in Skim.app"
142 :group 'org-mac-link-grabber
143 :type 'boolean)
146 (defun omlg-grab-link ()
147 "Prompt the user for an application to grab a link from, then go grab the link, and insert it at point"
148 (interactive)
149 (let* ((descriptors `(("F" "inder" org-mac-finder-insert-selected ,org-mac-grab-Finder-app-p)
150 ("m" "ail" org-mac-message-insert-selected ,org-mac-grab-Mail-app-p)
151 ("a" "ddressbook" org-mac-addressbook-insert-selected ,org-mac-grab-Addressbook-app-p)
152 ("s" "afari" org-mac-safari-insert-frontmost-url ,org-mac-grab-Safari-app-p)
153 ("f" "irefox" org-mac-firefox-insert-frontmost-url ,org-mac-grab-Firefox-app-p)
154 ("v" "imperator" org-mac-vimperator-insert-frontmost-url ,org-mac-grab-Firefox+Vimperator-p)
155 ("c" "hrome" org-mac-chrome-insert-frontmost-url ,org-mac-grab-Chrome-app-p)
156 ("t" "ogether" org-mac-together-insert-selected ,org-mac-grab-Together-app-p)
157 ("S" "kim" org-mac-skim-insert-page ,org-mac-grab-Skim-app-p)))
158 (menu-string (make-string 0 ?x))
159 input)
161 ;; Create the menu string for the keymap
162 (mapc '(lambda (descriptor)
163 (when (elt descriptor 3)
164 (setf menu-string (concat menu-string "[" (elt descriptor 0) "]" (elt descriptor 1) " "))))
165 descriptors)
166 (setf (elt menu-string (- (length menu-string) 1)) ?:)
168 ;; Prompt the user, and grab the link
169 (message menu-string)
170 (setq input (read-char-exclusive))
171 (mapc '(lambda (descriptor)
172 (let ((key (elt (elt descriptor 0) 0))
173 (active (elt descriptor 3))
174 (grab-function (elt descriptor 2)))
175 (when (and active (eq input key))
176 (call-interactively grab-function))))
177 descriptors)))
179 (defalias 'omgl-grab-link 'omlg-grab-link
180 "Renamed, and this alias will be obsolete next revision.")
182 (defun org-mac-paste-applescript-links (as-link-list)
183 "Paste in a list of links from an applescript handler. The
184 links are of the form <link>::split::<name>"
185 (let* ((link-list
186 (mapcar
187 (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x) (setq x (match-string 1 x))) x)
188 (split-string as-link-list "[\r\n]+")))
189 split-link URL description orglink orglink-insert rtn orglink-list)
190 (while link-list
191 (setq split-link (split-string (pop link-list) "::split::"))
192 (setq URL (car split-link))
193 (setq description (cadr split-link))
194 (when (not (string= URL ""))
195 (setq orglink (org-make-link-string URL description))
196 (push orglink orglink-list)))
197 (setq rtn (mapconcat 'identity orglink-list "\n"))
198 (kill-new rtn)
199 rtn))
203 ;; Handle links from Firefox.app
205 ;; This code allows you to grab the current active url from the main
206 ;; Firefox.app window, and insert it as a link into an org-mode
207 ;; document. Unfortunately, firefox does not expose an applescript
208 ;; dictionary, so this is necessarily introduces some limitations.
210 ;; The applescript to grab the url from Firefox.app uses the System
211 ;; Events application to give focus to the firefox application, select
212 ;; the contents of the url bar, and copy it. It then uses the title of
213 ;; the window as the text of the link. There is no way to grab links
214 ;; from other open tabs, and further, if there is more than one window
215 ;; open, it is not clear which one will be used (though emperically it
216 ;; seems that it is always the last active window).
218 (defun as-mac-firefox-get-frontmost-url ()
219 (let ((result (do-applescript
220 (concat
221 "set oldClipboard to the clipboard\n"
222 "set frontmostApplication to path to frontmost application\n"
223 "tell application \"Firefox\"\n"
224 " activate\n"
225 " delay 0.15\n"
226 " tell application \"System Events\"\n"
227 " keystroke \"l\" using {command down}\n"
228 " keystroke \"a\" using {command down}\n"
229 " keystroke \"c\" using {command down}\n"
230 " end tell\n"
231 " delay 0.15\n"
232 " set theUrl to the clipboard\n"
233 " set the clipboard to oldClipboard\n"
234 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
235 "end tell\n"
236 "activate application (frontmostApplication as text)\n"
237 "set links to {}\n"
238 "copy theResult to the end of links\n"
239 "return links as string\n"))))
240 (car (split-string result "[\r\n]+" t))))
242 (defun org-mac-firefox-get-frontmost-url ()
243 (interactive)
244 (message "Applescript: Getting Firefox url...")
245 (let* ((url-and-title (as-mac-firefox-get-frontmost-url))
246 (split-link (split-string url-and-title "::split::"))
247 (URL (car split-link))
248 (description (cadr split-link))
249 (org-link))
250 (when (not (string= URL ""))
251 (setq org-link (org-make-link-string URL description)))
252 (kill-new org-link)
253 org-link))
255 (defun org-mac-firefox-insert-frontmost-url ()
256 (interactive)
257 (insert (org-mac-firefox-get-frontmost-url)))
260 ;; Handle links from Google Firefox.app running the Vimperator extension
261 ;; Grab the frontmost url from Firefox+Vimperator. Same limitations are
262 ;; Firefox
264 (defun as-mac-vimperator-get-frontmost-url ()
265 (let ((result (do-applescript
266 (concat
267 "set oldClipboard to the clipboard\n"
268 "set frontmostApplication to path to frontmost application\n"
269 "tell application \"Firefox\"\n"
270 " activate\n"
271 " delay 0.15\n"
272 " tell application \"System Events\"\n"
273 " keystroke \"y\"\n"
274 " end tell\n"
275 " delay 0.15\n"
276 " set theUrl to the clipboard\n"
277 " set the clipboard to oldClipboard\n"
278 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
279 "end tell\n"
280 "activate application (frontmostApplication as text)\n"
281 "set links to {}\n"
282 "copy theResult to the end of links\n"
283 "return links as string\n"))))
284 (replace-regexp-in-string "\s+-\s+Vimperator" "" (car (split-string result "[\r\n]+" t)))))
287 (defun org-mac-vimperator-get-frontmost-url ()
288 (interactive)
289 (message "Applescript: Getting Vimperator url...")
290 (let* ((url-and-title (as-mac-vimperator-get-frontmost-url))
291 (split-link (split-string url-and-title "::split::"))
292 (URL (car split-link))
293 (description (cadr split-link))
294 (org-link))
295 (when (not (string= URL ""))
296 (setq org-link (org-make-link-string URL description)))
297 (kill-new org-link)
298 org-link))
300 (defun org-mac-vimperator-insert-frontmost-url ()
301 (interactive)
302 (insert (org-mac-vimperator-get-frontmost-url)))
305 ;; Handle links from Google Chrome.app
306 ;; Grab the frontmost url from Google Chrome. Same limitations are
307 ;; Firefox because Chrome doesn't publish an Applescript dictionary
309 (defun as-mac-chrome-get-frontmost-url ()
310 (let ((result (do-applescript
311 (concat
312 "set oldClipboard to the clipboard\n"
313 "set frontmostApplication to path to frontmost application\n"
314 "tell application \"Google Chrome\"\n"
315 " activate\n"
316 " delay 0.15\n"
317 " tell application \"System Events\"\n"
318 " keystroke \"l\" using command down\n"
319 " keystroke \"c\" using command down\n"
320 " end tell\n"
321 " delay 0.15\n"
322 " set theUrl to the clipboard\n"
323 " set the clipboard to oldClipboard\n"
324 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
325 "end tell\n"
326 "activate application (frontmostApplication as text)\n"
327 "set links to {}\n"
328 "copy theResult to the end of links\n"
329 "return links as string\n"))))
330 (car (split-string result "[\r\n]+" t))))
332 (defun org-mac-chrome-get-frontmost-url ()
333 (interactive)
334 (message "Applescript: Getting Chrome url...")
335 (let* ((url-and-title (as-mac-chrome-get-frontmost-url))
336 (split-link (split-string url-and-title "::split::"))
337 (URL (car split-link))
338 (description (cadr split-link))
339 (org-link))
340 (when (not (string= URL ""))
341 (setq org-link (org-make-link-string URL description)))
342 (kill-new org-link)
343 org-link))
345 (defun org-mac-chrome-insert-frontmost-url ()
346 (interactive)
347 (insert (org-mac-chrome-get-frontmost-url)))
350 ;; Handle links from Safari.app
351 ;; Grab the frontmost url from Safari.
353 (defun as-mac-safari-get-frontmost-url ()
354 (let ((result (do-applescript
355 (concat
356 "tell application \"Safari\"\n"
357 " set theUrl to URL of document 1\n"
358 " set theName to the name of the document 1\n"
359 " return theUrl & \"::split::\" & theName & \"\n\"\n"
360 "end tell\n"))))
361 (car (split-string result "[\r\n]+" t))))
363 (defun org-mac-safari-get-frontmost-url ()
364 (interactive)
365 (message "Applescript: Getting Safari url...")
366 (let* ((url-and-title (as-mac-safari-get-frontmost-url))
367 (split-link (split-string url-and-title "::split::"))
368 (URL (car split-link))
369 (description (cadr split-link))
370 (org-link))
371 (when (not (string= URL ""))
372 (setq org-link (org-make-link-string URL description)))
373 (kill-new org-link)
374 org-link))
376 (defun org-mac-safari-insert-frontmost-url ()
377 (interactive)
378 (insert (org-mac-safari-get-frontmost-url)))
383 ;; Handle links from together.app
387 (org-add-link-type "x-together-item" 'org-mac-together-item-open)
389 (defun org-mac-together-item-open (uid)
390 "Open the given uid, which is a reference to an item in Together"
391 (shell-command (concat "open -a Together \"x-together-item:" uid "\"")))
393 (defun as-get-selected-together-items ()
394 (do-applescript
395 (concat
396 "tell application \"Together\"\n"
397 " set theLinkList to {}\n"
398 " set theSelection to selected items\n"
399 " repeat with theItem in theSelection\n"
400 " set theLink to (get item link of theItem) & \"::split::\" & (get name of theItem) & \"\n\"\n"
401 " copy theLink to end of theLinkList\n"
402 " end repeat\n"
403 " return theLinkList as string\n"
404 "end tell")))
406 (defun org-mac-together-get-selected ()
407 (interactive)
408 (message "Applescript: Getting Togther items...")
409 (org-mac-paste-applescript-links (as-get-selected-together-items)))
411 (defun org-mac-together-insert-selected ()
412 (interactive)
413 (insert (org-mac-together-get-selected)))
418 ;; Handle links from Finder.app
422 (defun as-get-selected-finder-items ()
423 (do-applescript
424 (concat
425 "tell application \"Finder\"\n"
426 " set theSelection to the selection\n"
427 " set links to {}\n"
428 " repeat with theItem in theSelection\n"
429 " set theLink to \"file://\" & (POSIX path of (theItem as string)) & \"::split::\" & (get the name of theItem) & \"\n\"\n"
430 " copy theLink to the end of links\n"
431 " end repeat\n"
432 " return links as string\n"
433 "end tell\n")))
435 (defun org-mac-finder-item-get-selected ()
436 (interactive)
437 (message "Applescript: Getting Finder items...")
438 (org-mac-paste-applescript-links (as-get-selected-finder-items)))
440 (defun org-mac-finder-insert-selected ()
441 (interactive)
442 (insert (org-mac-finder-item-get-selected)))
447 ;; Handle links from AddressBook.app
451 (org-add-link-type "addressbook" 'org-mac-addressbook-item-open)
453 (defun org-mac-addressbook-item-open (uid)
454 "Open the given uid, which is a reference to an item in Together"
455 (shell-command (concat "open \"addressbook:" uid "\"")))
457 (defun as-get-selected-addressbook-items ()
458 (do-applescript
459 (concat
460 "tell application \"Address Book\"\n"
461 " set theSelection to the selection\n"
462 " set links to {}\n"
463 " repeat with theItem in theSelection\n"
464 " set theLink to \"addressbook://\" & (the id of theItem) & \"::split::\" & (the name of theItem) & \"\n\"\n"
465 " copy theLink to the end of links\n"
466 " end repeat\n"
467 " return links as string\n"
468 "end tell\n")))
470 (defun org-mac-addressbook-item-get-selected ()
471 (interactive)
472 (message "Applescript: Getting Address Book items...")
473 (org-mac-paste-applescript-links (as-get-selected-addressbook-items)))
475 (defun org-mac-addressbook-insert-selected ()
476 (interactive)
477 (insert (org-mac-addressbook-item-get-selected)))
481 ;; Handle links from Skim.app
483 ;; Original code & idea by Christopher Suckling (org-mac-protocol)
485 (org-add-link-type "skim" 'org-mac-skim-open)
487 (defun org-mac-skim-open (uri)
488 "Visit page of pdf in Skim"
489 (let* ((page (when (string-match "::\\(.+\\)\\'" uri)
490 (match-string 1 uri)))
491 (document (substring uri 0 (match-beginning 0))))
492 (do-applescript
493 (concat
494 "tell application \"Skim\"\n"
495 "activate\n"
496 "set theDoc to \"" document "\"\n"
497 "set thePage to " page "\n"
498 "open theDoc\n"
499 "go document 1 to page thePage of document 1\n"
500 "end tell"))))
503 (defun as-get-skim-page-link ()
504 (do-applescript
505 (concat
506 "tell application \"Skim\"\n"
507 "set theDoc to front document\n"
508 "set theTitle to (name of theDoc)\n"
509 "set thePath to (path of theDoc)\n"
510 "set thePage to (get index for current page of theDoc)\n"
511 "set theSelection to selection of theDoc\n"
512 "set theContent to contents of (get text for theSelection)\n"
513 "if theContent is missing value then\n"
514 " set theContent to theTitle & \", p. \" & thePage\n"
515 (when org-mac-Skim-highlight-selection-p
516 (concat
517 "else\n"
518 " tell theDoc\n"
519 " set theNote to make note with properties {type:highlight note, selection:theSelection}\n"
520 " set text of theNote to (get text for theSelection)\n"
521 " end tell\n"))
522 "end if\n"
523 "set theLink to \"skim://\" & thePath & \"::\" & thePage & "
524 "\"::split::\" & theContent\n"
525 "end tell\n"
526 "return theLink as string\n")))
528 (defun org-mac-skim-get-page ()
529 (interactive)
530 (message "Applescript: Getting Skim page link...")
531 (let* ((link-and-descr (as-get-skim-page-link))
532 (split-link (split-string link-and-descr "::split::"))
533 (link (car split-link))
534 (description (cadr split-link))
535 (org-link))
536 (when (not (string= link ""))
537 (setq org-link (org-make-link-string link description)))
538 (kill-new org-link)
539 org-link))
541 (defun org-mac-skim-insert-page ()
542 (interactive)
543 (insert (org-mac-skim-get-page)))
547 (provide 'org-mac-link-grabber)
549 ;;; org-mac-link-grabber.el ends here