org.texi: Update Beamer documentation
[org-mode.git] / contrib / lisp / org-mac-link-grabber.el
blob05986172b8c18dfff3b26a556ed591d50e4f8c2a
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)
132 (defun omlg-grab-link ()
133 "Prompt the user for an application to grab a link from, then go grab the link, and insert it at point"
134 (interactive)
135 (let* ((descriptors `(("F" "inder" org-mac-finder-insert-selected ,org-mac-grab-Finder-app-p)
136 ("m" "ail" org-mac-message-insert-selected ,org-mac-grab-Mail-app-p)
137 ("a" "ddressbook" org-mac-addressbook-insert-selected ,org-mac-grab-Addressbook-app-p)
138 ("s" "afari" org-mac-safari-insert-frontmost-url ,org-mac-grab-Safari-app-p)
139 ("f" "irefox" org-mac-firefox-insert-frontmost-url ,org-mac-grab-Firefox-app-p)
140 ("v" "imperator" org-mac-vimperator-insert-frontmost-url ,org-mac-grab-Firefox+Vimperator-p)
141 ("c" "hrome" org-mac-chrome-insert-frontmost-url ,org-mac-grab-Chrome-app-p)
142 ("t" "ogether" org-mac-together-insert-selected ,org-mac-grab-Together-app-p)))
143 (menu-string (make-string 0 ?x))
144 input)
146 ;; Create the menu string for the keymap
147 (mapc '(lambda (descriptor)
148 (when (elt descriptor 3)
149 (setf menu-string (concat menu-string "[" (elt descriptor 0) "]" (elt descriptor 1) " "))))
150 descriptors)
151 (setf (elt menu-string (- (length menu-string) 1)) ?:)
153 ;; Prompt the user, and grab the link
154 (message menu-string)
155 (setq input (read-char-exclusive))
156 (mapc '(lambda (descriptor)
157 (let ((key (elt (elt descriptor 0) 0))
158 (active (elt descriptor 3))
159 (grab-function (elt descriptor 2)))
160 (when (and active (eq input key))
161 (call-interactively grab-function))))
162 descriptors)))
164 (defalias 'omgl-grab-link 'omlg-grab-link
165 "Renamed, and this alias will be obsolete next revision.")
167 (defun org-mac-paste-applescript-links (as-link-list)
168 "Paste in a list of links from an applescript handler. The
169 links are of the form <link>::split::<name>"
170 (let* ((link-list
171 (mapcar
172 (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x) (setq x (match-string 1 x))) x)
173 (split-string as-link-list "[\r\n]+")))
174 split-link URL description orglink orglink-insert rtn orglink-list)
175 (while link-list
176 (setq split-link (split-string (pop link-list) "::split::"))
177 (setq URL (car split-link))
178 (setq description (cadr split-link))
179 (when (not (string= URL ""))
180 (setq orglink (org-make-link-string URL description))
181 (push orglink orglink-list)))
182 (setq rtn (mapconcat 'identity orglink-list "\n"))
183 (kill-new rtn)
184 rtn))
188 ;; Handle links from Firefox.app
190 ;; This code allows you to grab the current active url from the main
191 ;; Firefox.app window, and insert it as a link into an org-mode
192 ;; document. Unfortunately, firefox does not expose an applescript
193 ;; dictionary, so this is necessarily introduces some limitations.
195 ;; The applescript to grab the url from Firefox.app uses the System
196 ;; Events application to give focus to the firefox application, select
197 ;; the contents of the url bar, and copy it. It then uses the title of
198 ;; the window as the text of the link. There is no way to grab links
199 ;; from other open tabs, and further, if there is more than one window
200 ;; open, it is not clear which one will be used (though emperically it
201 ;; seems that it is always the last active window).
203 (defun as-mac-firefox-get-frontmost-url ()
204 (let ((result (do-applescript
205 (concat
206 "set oldClipboard to the clipboard\n"
207 "set frontmostApplication to path to frontmost application\n"
208 "tell application \"Firefox\"\n"
209 " activate\n"
210 " delay 0.15\n"
211 " tell application \"System Events\"\n"
212 " keystroke \"l\" using command down\n"
213 " keystroke \"c\" using command down\n"
214 " end tell\n"
215 " delay 0.15\n"
216 " set theUrl to the clipboard\n"
217 " set the clipboard to oldClipboard\n"
218 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
219 "end tell\n"
220 "activate application (frontmostApplication as text)\n"
221 "set links to {}\n"
222 "copy theResult to the end of links\n"
223 "return links as string\n"))))
224 (car (split-string result "[\r\n]+" t))))
226 (defun org-mac-firefox-get-frontmost-url ()
227 (interactive)
228 (message "Applescript: Getting Firefox url...")
229 (let* ((url-and-title (as-mac-firefox-get-frontmost-url))
230 (split-link (split-string url-and-title "::split::"))
231 (URL (car split-link))
232 (description (cadr split-link))
233 (org-link))
234 (when (not (string= URL ""))
235 (setq org-link (org-make-link-string URL description)))
236 (kill-new org-link)
237 org-link))
239 (defun org-mac-firefox-insert-frontmost-url ()
240 (interactive)
241 (insert (org-mac-firefox-get-frontmost-url)))
244 ;; Handle links from Google Firefox.app running the Vimperator extension
245 ;; Grab the frontmost url from Firefox+Vimperator. Same limitations are
246 ;; Firefox
248 (defun as-mac-vimperator-get-frontmost-url ()
249 (let ((result (do-applescript
250 (concat
251 "set oldClipboard to the clipboard\n"
252 "set frontmostApplication to path to frontmost application\n"
253 "tell application \"Firefox\"\n"
254 " activate\n"
255 " delay 0.15\n"
256 " tell application \"System Events\"\n"
257 " keystroke \"y\"\n"
258 " end tell\n"
259 " delay 0.15\n"
260 " set theUrl to the clipboard\n"
261 " set the clipboard to oldClipboard\n"
262 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
263 "end tell\n"
264 "activate application (frontmostApplication as text)\n"
265 "set links to {}\n"
266 "copy theResult to the end of links\n"
267 "return links as string\n"))))
268 (replace-regexp-in-string "\s+-\s+Vimperator" "" (car (split-string result "[\r\n]+" t)))))
271 (defun org-mac-vimperator-get-frontmost-url ()
272 (interactive)
273 (message "Applescript: Getting Vimperator url...")
274 (let* ((url-and-title (as-mac-vimperator-get-frontmost-url))
275 (split-link (split-string url-and-title "::split::"))
276 (URL (car split-link))
277 (description (cadr split-link))
278 (org-link))
279 (when (not (string= URL ""))
280 (setq org-link (org-make-link-string URL description)))
281 (kill-new org-link)
282 org-link))
284 (defun org-mac-vimperator-insert-frontmost-url ()
285 (interactive)
286 (insert (org-mac-vimperator-get-frontmost-url)))
289 ;; Handle links from Google Chrome.app
290 ;; Grab the frontmost url from Google Chrome. Same limitations are
291 ;; Firefox because Chrome doesn't publish an Applescript dictionary
293 (defun as-mac-chrome-get-frontmost-url ()
294 (let ((result (do-applescript
295 (concat
296 "set oldClipboard to the clipboard\n"
297 "set frontmostApplication to path to frontmost application\n"
298 "tell application \"Google Chrome\"\n"
299 " activate\n"
300 " delay 0.15\n"
301 " tell application \"System Events\"\n"
302 " keystroke \"l\" using command down\n"
303 " keystroke \"c\" using command down\n"
304 " end tell\n"
305 " delay 0.15\n"
306 " set theUrl to the clipboard\n"
307 " set the clipboard to oldClipboard\n"
308 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
309 "end tell\n"
310 "activate application (frontmostApplication as text)\n"
311 "set links to {}\n"
312 "copy theResult to the end of links\n"
313 "return links as string\n"))))
314 (car (split-string result "[\r\n]+" t))))
316 (defun org-mac-chrome-get-frontmost-url ()
317 (interactive)
318 (message "Applescript: Getting Chrome url...")
319 (let* ((url-and-title (as-mac-chrome-get-frontmost-url))
320 (split-link (split-string url-and-title "::split::"))
321 (URL (car split-link))
322 (description (cadr split-link))
323 (org-link))
324 (when (not (string= URL ""))
325 (setq org-link (org-make-link-string URL description)))
326 (kill-new org-link)
327 org-link))
329 (defun org-mac-chrome-insert-frontmost-url ()
330 (interactive)
331 (insert (org-mac-chrome-get-frontmost-url)))
334 ;; Handle links from Safari.app
335 ;; Grab the frontmost url from Safari.
337 (defun as-mac-safari-get-frontmost-url ()
338 (let ((result (do-applescript
339 (concat
340 "tell application \"Safari\"\n"
341 " set theUrl to URL of document 1\n"
342 " set theName to the name of the document 1\n"
343 " return theUrl & \"::split::\" & theName & \"\n\"\n"
344 "end tell\n"))))
345 (car (split-string result "[\r\n]+" t))))
347 (defun org-mac-safari-get-frontmost-url ()
348 (interactive)
349 (message "Applescript: Getting Safari url...")
350 (let* ((url-and-title (as-mac-safari-get-frontmost-url))
351 (split-link (split-string url-and-title "::split::"))
352 (URL (car split-link))
353 (description (cadr split-link))
354 (org-link))
355 (when (not (string= URL ""))
356 (setq org-link (org-make-link-string URL description)))
357 (kill-new org-link)
358 org-link))
360 (defun org-mac-safari-insert-frontmost-url ()
361 (interactive)
362 (insert (org-mac-safari-get-frontmost-url)))
367 ;; Handle links from together.app
371 (org-add-link-type "x-together-item" 'org-mac-together-item-open)
373 (defun org-mac-together-item-open (uid)
374 "Open the given uid, which is a reference to an item in Together"
375 (shell-command (concat "open -a Together \"x-together-item:" uid "\"")))
377 (defun as-get-selected-together-items ()
378 (do-applescript
379 (concat
380 "tell application \"Together\"\n"
381 " set theLinkList to {}\n"
382 " set theSelection to selected items\n"
383 " repeat with theItem in theSelection\n"
384 " set theLink to (get item link of theItem) & \"::split::\" & (get name of theItem) & \"\n\"\n"
385 " copy theLink to end of theLinkList\n"
386 " end repeat\n"
387 " return theLinkList as string\n"
388 "end tell")))
390 (defun org-mac-together-get-selected ()
391 (interactive)
392 (message "Applescript: Getting Togther items...")
393 (org-mac-paste-applescript-links (as-get-selected-together-items)))
395 (defun org-mac-together-insert-selected ()
396 (interactive)
397 (insert (org-mac-together-get-selected)))
402 ;; Handle links from Finder.app
406 (defun as-get-selected-finder-items ()
407 (do-applescript
408 (concat
409 "tell application \"Finder\"\n"
410 " set theSelection to the selection\n"
411 " set links to {}\n"
412 " repeat with theItem in theSelection\n"
413 " set theLink to \"file://\" & (POSIX path of (theItem as string)) & \"::split::\" & (get the name of theItem) & \"\n\"\n"
414 " copy theLink to the end of links\n"
415 " end repeat\n"
416 " return links as string\n"
417 "end tell\n")))
419 (defun org-mac-finder-item-get-selected ()
420 (interactive)
421 (message "Applescript: Getting Finder items...")
422 (org-mac-paste-applescript-links (as-get-selected-finder-items)))
424 (defun org-mac-finder-insert-selected ()
425 (interactive)
426 (insert (org-mac-finder-item-get-selected)))
431 ;; Handle links from AddressBook.app
435 (org-add-link-type "addressbook" 'org-mac-addressbook-item-open)
437 (defun org-mac-addressbook-item-open (uid)
438 "Open the given uid, which is a reference to an item in Together"
439 (shell-command (concat "open \"addressbook:" uid "\"")))
441 (defun as-get-selected-addressbook-items ()
442 (do-applescript
443 (concat
444 "tell application \"Address Book\"\n"
445 " set theSelection to the selection\n"
446 " set links to {}\n"
447 " repeat with theItem in theSelection\n"
448 " set theLink to \"addressbook://\" & (the id of theItem) & \"::split::\" & (the name of theItem) & \"\n\"\n"
449 " copy theLink to the end of links\n"
450 " end repeat\n"
451 " return links as string\n"
452 "end tell\n")))
454 (defun org-mac-addressbook-item-get-selected ()
455 (interactive)
456 (message "Applescript: Getting Address Book items...")
457 (org-mac-paste-applescript-links (as-get-selected-addressbook-items)))
459 (defun org-mac-addressbook-insert-selected ()
460 (interactive)
461 (insert (org-mac-addressbook-item-get-selected)))
464 (provide 'org-mac-link-grabber)
466 ;;; org-mac-link-grabber.el ends here