org-contacts: added org-contacts-show-map (requires google-maps-el)
[org-mode.git] / contrib / lisp / org-mac-link-grabber.el
blob8ec428b2d59c0cb65716a1e6c3cdd270af524c4b
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 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 program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 3, or (at your option)
13 ;; any later version.
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program; if not, write to the Free Software
22 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 ;;; Commentary:
26 ;; This code allows you to grab either the current selected items, or
27 ;; the frontmost url in various mac appliations, and insert them as
28 ;; hyperlinks into the current org-mode document at point.
30 ;; This code is heavily based on, and indeed requires,
31 ;; org-mac-message.el written by John Weigley and Christopher
32 ;; Suckling.
34 ;; Detailed comments for each application interface are inlined with
35 ;; the code. Here is a brief overview of how the code interacts with
36 ;; each application:
38 ;; Finder.app - grab links to the selected files in the frontmost window
39 ;; Mail.app - grab links to the selected messages in the message list
40 ;; AddressBook.app - Grab links to the selected addressbook Cards
41 ;; Firefox.app - Grab the url of the frontmost tab in the frontmost window
42 ;; Vimperator/Firefox.app - Grab the url of the frontmost tab in the frontmost window
43 ;; Safari.app - Grab the url of the frontmost tab in the frontmost window
44 ;; Google Chrome.app - Grab the url of the frontmost tab in the frontmost window
45 ;; Together.app - Grab links to the selected items in the library list
48 ;; Installation:
50 ;; add (require 'org-mac-link-grabber) to your .emacs, and optionally
51 ;; bind a key to activate the link grabber menu, like this:
53 ;; (add-hook 'org-mode-hook (lambda ()
54 ;; (define-key org-mode-map (kbd "C-c g") 'omlg-grab-link)))
57 ;; Usage:
59 ;; Type C-c g (or whatever key you defined, as above), or type M-x
60 ;; omlg-grab-link RET to activate the link grabber. This will present
61 ;; you with a menu to choose an application from which to grab a link
62 ;; to insert at point. You may also type C-g to abort.
64 ;; Customizing:
66 ;; You may customize which applications appear in the grab menu by
67 ;; customizing the group org-mac-link-grabber. Changes take effect
68 ;; immediately.
71 ;;; Code:
73 (require 'org)
74 (require 'org-mac-message)
76 (defgroup org-mac-link-grabber nil
77 "Options concerning grabbing links from external Mac
78 applications and inserting them in org documents"
79 :tag "Org Mac link grabber"
80 :group 'org-link)
82 (defcustom org-mac-grab-Finder-app-p t
83 "Enable menu option [F]inder to grab links from the Finder"
84 :tag "Grab Finder.app links"
85 :group 'org-mac-link-grabber
86 :type 'boolean)
88 (defcustom org-mac-grab-Mail-app-p t
89 "Enable menu option [m]ail to grab links from Mail.app"
90 :tag "Grab Mail.app links"
91 :group 'org-mac-link-grabber
92 :type 'boolean)
94 (defcustom org-mac-grab-Addressbook-app-p t
95 "Enable menu option [a]ddressbook to grab links from AddressBook.app"
96 :tag "Grab AddressBook.app links"
97 :group 'org-mac-link-grabber
98 :type 'boolean)
100 (defcustom org-mac-grab-Safari-app-p t
101 "Enable menu option [s]afari to grab links from Safari.app"
102 :tag "Grab Safari.app links"
103 :group 'org-mac-link-grabber
104 :type 'boolean)
106 (defcustom org-mac-grab-Firefox-app-p t
107 "Enable menu option [f]irefox to grab links from Firefox.app"
108 :tag "Grab Firefox.app links"
109 :group 'org-mac-link-grabber
110 :type 'boolean)
112 (defcustom org-mac-grab-Firefox+Vimperator-p nil
113 "Enable menu option [v]imperator to grab links from Firefox.app running the Vimperator plugin"
114 :tag "Grab Vimperator/Firefox.app links"
115 :group 'org-mac-link-grabber
116 :type 'boolean)
118 (defcustom org-mac-grab-Chrome-app-p t
119 "Enable menu option [f]irefox to grab links from Google Chrome.app"
120 :tag "Grab Google Chrome.app links"
121 :group 'org-mac-link-grabber
122 :type 'boolean)
124 (defcustom org-mac-grab-Together-app-p nil
125 "Enable menu option [t]ogether to grab links from Together.app"
126 :tag "Grab Together.app links"
127 :group 'org-mac-link-grabber
128 :type 'boolean)
131 (defun omlg-grab-link ()
132 "Prompt the user for an application to grab a link from, then go grab the link, and insert it at point"
133 (interactive)
134 (let* ((descriptors `(("F" "inder" org-mac-finder-insert-selected ,org-mac-grab-Finder-app-p)
135 ("m" "ail" org-mac-message-insert-selected ,org-mac-grab-Mail-app-p)
136 ("a" "ddressbook" org-mac-addressbook-insert-selected ,org-mac-grab-Addressbook-app-p)
137 ("s" "afari" org-mac-safari-insert-frontmost-url ,org-mac-grab-Safari-app-p)
138 ("f" "irefox" org-mac-firefox-insert-frontmost-url ,org-mac-grab-Firefox-app-p)
139 ("v" "imperator" org-mac-vimperator-insert-frontmost-url ,org-mac-grab-Firefox+Vimperator-p)
140 ("c" "hrome" org-mac-chrome-insert-frontmost-url ,org-mac-grab-Chrome-app-p)
141 ("t" "ogether" org-mac-together-insert-selected ,org-mac-grab-Together-app-p)))
142 (menu-string (make-string 0 ?x))
143 input)
145 ;; Create the menu string for the keymap
146 (mapc '(lambda (descriptor)
147 (when (elt descriptor 3)
148 (setf menu-string (concat menu-string "[" (elt descriptor 0) "]" (elt descriptor 1) " "))))
149 descriptors)
150 (setf (elt menu-string (- (length menu-string) 1)) ?:)
152 ;; Prompt the user, and grab the link
153 (message menu-string)
154 (setq input (read-char-exclusive))
155 (mapc '(lambda (descriptor)
156 (let ((key (elt (elt descriptor 0) 0))
157 (active (elt descriptor 3))
158 (grab-function (elt descriptor 2)))
159 (when (and active (eq input key))
160 (call-interactively grab-function))))
161 descriptors)))
163 (defalias 'omgl-grab-link 'omlg-grab-link
164 "Renamed, and this alias will be obsolete next revision.")
166 (defun org-mac-paste-applescript-links (as-link-list)
167 "Paste in a list of links from an applescript handler. The
168 links are of the form <link>::split::<name>"
169 (let* ((link-list
170 (mapcar
171 (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x) (setq x (match-string 1 x))) x)
172 (split-string as-link-list "[\r\n]+")))
173 split-link URL description orglink orglink-insert rtn orglink-list)
174 (while link-list
175 (setq split-link (split-string (pop link-list) "::split::"))
176 (setq URL (car split-link))
177 (setq description (cadr split-link))
178 (when (not (string= URL ""))
179 (setq orglink (org-make-link-string URL description))
180 (push orglink orglink-list)))
181 (setq rtn (mapconcat 'identity orglink-list "\n"))
182 (kill-new rtn)
183 rtn))
187 ;; Handle links from Firefox.app
189 ;; This code allows you to grab the current active url from the main
190 ;; Firefox.app window, and insert it as a link into an org-mode
191 ;; document. Unfortunately, firefox does not expose an applescript
192 ;; dictionary, so this is necessarily introduces some limitations.
194 ;; The applescript to grab the url from Firefox.app uses the System
195 ;; Events application to give focus to the firefox application, select
196 ;; the contents of the url bar, and copy it. It then uses the title of
197 ;; the window as the text of the link. There is no way to grab links
198 ;; from other open tabs, and further, if there is more than one window
199 ;; open, it is not clear which one will be used (though emperically it
200 ;; seems that it is always the last active window).
202 (defun as-mac-firefox-get-frontmost-url ()
203 (let ((result (do-applescript
204 (concat
205 "set oldClipboard to the clipboard\n"
206 "set frontmostApplication to path to frontmost application\n"
207 "tell application \"Firefox\"\n"
208 " activate\n"
209 " delay 0.15\n"
210 " tell application \"System Events\"\n"
211 " keystroke \"l\" using command down\n"
212 " keystroke \"c\" using command down\n"
213 " end tell\n"
214 " delay 0.15\n"
215 " set theUrl to the clipboard\n"
216 " set the clipboard to oldClipboard\n"
217 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
218 "end tell\n"
219 "activate application (frontmostApplication as text)\n"
220 "set links to {}\n"
221 "copy theResult to the end of links\n"
222 "return links as string\n"))))
223 (car (split-string result "[\r\n]+" t))))
225 (defun org-mac-firefox-get-frontmost-url ()
226 (interactive)
227 (message "Applescript: Getting Firefox url...")
228 (let* ((url-and-title (as-mac-firefox-get-frontmost-url))
229 (split-link (split-string url-and-title "::split::"))
230 (URL (car split-link))
231 (description (cadr split-link))
232 (org-link))
233 (when (not (string= URL ""))
234 (setq org-link (org-make-link-string URL description)))
235 (kill-new org-link)
236 org-link))
238 (defun org-mac-firefox-insert-frontmost-url ()
239 (interactive)
240 (insert (org-mac-firefox-get-frontmost-url)))
243 ;; Handle links from Google Firefox.app running the Vimperator extension
244 ;; Grab the frontmost url from Firefox+Vimperator. Same limitations are
245 ;; Firefox
247 (defun as-mac-vimperator-get-frontmost-url ()
248 (let ((result (do-applescript
249 (concat
250 "set oldClipboard to the clipboard\n"
251 "set frontmostApplication to path to frontmost application\n"
252 "tell application \"Firefox\"\n"
253 " activate\n"
254 " delay 0.15\n"
255 " tell application \"System Events\"\n"
256 " keystroke \"y\"\n"
257 " end tell\n"
258 " delay 0.15\n"
259 " set theUrl to the clipboard\n"
260 " set the clipboard to oldClipboard\n"
261 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
262 "end tell\n"
263 "activate application (frontmostApplication as text)\n"
264 "set links to {}\n"
265 "copy theResult to the end of links\n"
266 "return links as string\n"))))
267 (replace-regexp-in-string "\s+-\s+Vimperator" "" (car (split-string result "[\r\n]+" t)))))
270 (defun org-mac-vimperator-get-frontmost-url ()
271 (interactive)
272 (message "Applescript: Getting Vimperator url...")
273 (let* ((url-and-title (as-mac-vimperator-get-frontmost-url))
274 (split-link (split-string url-and-title "::split::"))
275 (URL (car split-link))
276 (description (cadr split-link))
277 (org-link))
278 (when (not (string= URL ""))
279 (setq org-link (org-make-link-string URL description)))
280 (kill-new org-link)
281 org-link))
283 (defun org-mac-vimperator-insert-frontmost-url ()
284 (interactive)
285 (insert (org-mac-vimperator-get-frontmost-url)))
288 ;; Handle links from Google Chrome.app
289 ;; Grab the frontmost url from Google Chrome. Same limitations are
290 ;; Firefox because Chrome doesn't publish an Applescript dictionary
292 (defun as-mac-chrome-get-frontmost-url ()
293 (let ((result (do-applescript
294 (concat
295 "set oldClipboard to the clipboard\n"
296 "set frontmostApplication to path to frontmost application\n"
297 "tell application \"Google Chrome\"\n"
298 " activate\n"
299 " delay 0.15\n"
300 " tell application \"System Events\"\n"
301 " keystroke \"l\" using command down\n"
302 " keystroke \"c\" using command down\n"
303 " end tell\n"
304 " delay 0.15\n"
305 " set theUrl to the clipboard\n"
306 " set the clipboard to oldClipboard\n"
307 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
308 "end tell\n"
309 "activate application (frontmostApplication as text)\n"
310 "set links to {}\n"
311 "copy theResult to the end of links\n"
312 "return links as string\n"))))
313 (car (split-string result "[\r\n]+" t))))
315 (defun org-mac-chrome-get-frontmost-url ()
316 (interactive)
317 (message "Applescript: Getting Chrome url...")
318 (let* ((url-and-title (as-mac-chrome-get-frontmost-url))
319 (split-link (split-string url-and-title "::split::"))
320 (URL (car split-link))
321 (description (cadr split-link))
322 (org-link))
323 (when (not (string= URL ""))
324 (setq org-link (org-make-link-string URL description)))
325 (kill-new org-link)
326 org-link))
328 (defun org-mac-chrome-insert-frontmost-url ()
329 (interactive)
330 (insert (org-mac-chrome-get-frontmost-url)))
333 ;; Handle links from Safari.app
334 ;; Grab the frontmost url from Safari.
336 (defun as-mac-safari-get-frontmost-url ()
337 (let ((result (do-applescript
338 (concat
339 "tell application \"Safari\"\n"
340 " set theUrl to URL of document 1\n"
341 " set theName to the name of the document 1\n"
342 " return theUrl & \"::split::\" & theName & \"\n\"\n"
343 "end tell\n"))))
344 (car (split-string result "[\r\n]+" t))))
346 (defun org-mac-safari-get-frontmost-url ()
347 (interactive)
348 (message "Applescript: Getting Safari url...")
349 (let* ((url-and-title (as-mac-safari-get-frontmost-url))
350 (split-link (split-string url-and-title "::split::"))
351 (URL (car split-link))
352 (description (cadr split-link))
353 (org-link))
354 (when (not (string= URL ""))
355 (setq org-link (org-make-link-string URL description)))
356 (kill-new org-link)
357 org-link))
359 (defun org-mac-safari-insert-frontmost-url ()
360 (interactive)
361 (insert (org-mac-safari-get-frontmost-url)))
366 ;; Handle links from together.app
370 (org-add-link-type "x-together-item" 'org-mac-together-item-open)
372 (defun org-mac-together-item-open (uid)
373 "Open the given uid, which is a reference to an item in Together"
374 (shell-command (concat "open -a Together \"x-together-item:" uid "\"")))
376 (defun as-get-selected-together-items ()
377 (do-applescript
378 (concat
379 "tell application \"Together\"\n"
380 " set theLinkList to {}\n"
381 " set theSelection to selected items\n"
382 " repeat with theItem in theSelection\n"
383 " set theLink to (get item link of theItem) & \"::split::\" & (get name of theItem) & \"\n\"\n"
384 " copy theLink to end of theLinkList\n"
385 " end repeat\n"
386 " return theLinkList as string\n"
387 "end tell")))
389 (defun org-mac-together-get-selected ()
390 (interactive)
391 (message "Applescript: Getting Togther items...")
392 (org-mac-paste-applescript-links (as-get-selected-together-items)))
394 (defun org-mac-together-insert-selected ()
395 (interactive)
396 (insert (org-mac-together-get-selected)))
401 ;; Handle links from Finder.app
405 (defun as-get-selected-finder-items ()
406 (do-applescript
407 (concat
408 "tell application \"Finder\"\n"
409 " set theSelection to the selection\n"
410 " set links to {}\n"
411 " repeat with theItem in theSelection\n"
412 " set theLink to \"file://\" & (POSIX path of (theItem as string)) & \"::split::\" & (get the name of theItem) & \"\n\"\n"
413 " copy theLink to the end of links\n"
414 " end repeat\n"
415 " return links as string\n"
416 "end tell\n")))
418 (defun org-mac-finder-item-get-selected ()
419 (interactive)
420 (message "Applescript: Getting Finder items...")
421 (org-mac-paste-applescript-links (as-get-selected-finder-items)))
423 (defun org-mac-finder-insert-selected ()
424 (interactive)
425 (insert (org-mac-finder-item-get-selected)))
430 ;; Handle links from AddressBook.app
434 (org-add-link-type "addressbook" 'org-mac-addressbook-item-open)
436 (defun org-mac-addressbook-item-open (uid)
437 "Open the given uid, which is a reference to an item in Together"
438 (shell-command (concat "open \"addressbook:" uid "\"")))
440 (defun as-get-selected-addressbook-items ()
441 (do-applescript
442 (concat
443 "tell application \"Address Book\"\n"
444 " set theSelection to the selection\n"
445 " set links to {}\n"
446 " repeat with theItem in theSelection\n"
447 " set theLink to \"addressbook://\" & (the id of theItem) & \"::split::\" & (the name of theItem) & \"\n\"\n"
448 " copy theLink to the end of links\n"
449 " end repeat\n"
450 " return links as string\n"
451 "end tell\n")))
453 (defun org-mac-addressbook-item-get-selected ()
454 (interactive)
455 (message "Applescript: Getting Address Book items...")
456 (org-mac-paste-applescript-links (as-get-selected-addressbook-items)))
458 (defun org-mac-addressbook-insert-selected ()
459 (interactive)
460 (insert (org-mac-addressbook-item-get-selected)))
463 (provide 'org-mac-link-grabber)
465 ;;; org-mac-link-grabber.el ends here