Fix typo
[org-mode/org-tableheadings.git] / contrib / lisp / org-mac-link-grabber.el
blob46a95656f5010d63eb571819969dfe267b3d63cd
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
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 ;; Safari.app - Grab the url of the frontmost tab in the frontmost window
43 ;; Google Chrome.app - Grab the url of the frontmost tab in the frontmost window
44 ;; Together.app - Grab links to the selected items in the library list
47 ;; Installation:
49 ;; add (require 'org-mac-link-grabber) to your .emacs, and optionally
50 ;; bind a key to activate the link grabber menu, like this:
52 ;; (add-hook 'org-mode-hook (lambda ()
53 ;; (define-key org-mode-map (kbd "C-c g") 'omlg-grab-link)))
56 ;; Usage:
58 ;; Type C-c g (or whatever key you defined, as above), or type M-x
59 ;; omlg-grab-link RET to activate the link grabber. This will present
60 ;; you with a menu to choose an application from which to grab a link
61 ;; to insert at point. You may also type C-g to abort.
63 ;; Customizing:
65 ;; You may customize which applications appear in the grab menu by
66 ;; customizing the group org-mac-link-grabber. Changes take effect
67 ;; immediately.
70 ;;; Code:
72 (require 'org)
73 (require 'org-mac-message)
75 (defgroup org-mac-link-grabber nil
76 "Options concerning grabbing links from external Mac
77 applications and inserting them in org documents"
78 :tag "Org Mac link grabber"
79 :group 'org-link)
81 (defcustom org-mac-grab-Finder-app-p t
82 "Enable menu option [F]inder to grab links from the Finder"
83 :tag "Grab Finder.app links"
84 :group 'org-mac-link-grabber
85 :type 'boolean)
87 (defcustom org-mac-grab-Mail-app-p t
88 "Enable menu option [m]ail to grab links from Mail.app"
89 :tag "Grab Mail.app links"
90 :group 'org-mac-link-grabber
91 :type 'boolean)
93 (defcustom org-mac-grab-Addressbook-app-p t
94 "Enable menu option [a]ddressbook to grab links from AddressBook.app"
95 :tag "Grab AddressBook.app links"
96 :group 'org-mac-link-grabber
97 :type 'boolean)
99 (defcustom org-mac-grab-Safari-app-p t
100 "Enable menu option [s]afari to grab links from Safari.app"
101 :tag "Grab Safari.app links"
102 :group 'org-mac-link-grabber
103 :type 'boolean)
105 (defcustom org-mac-grab-Firefox-app-p t
106 "Enable menu option [f]irefox to grab links from Firefox.app"
107 :tag "Grab Firefox.app links"
108 :group 'org-mac-link-grabber
109 :type 'boolean)
111 (defcustom org-mac-grab-Chrome-app-p t
112 "Enable menu option [f]irefox to grab links from Google Chrome.app"
113 :tag "Grab Google Chrome.app links"
114 :group 'org-mac-link-grabber
115 :type 'boolean)
117 (defcustom org-mac-grab-Together-app-p nil
118 "Enable menu option [t]ogether to grab links from Together.app"
119 :tag "Grab Together.app links"
120 :group 'org-mac-link-grabber
121 :type 'boolean)
124 (defun omlg-grab-link ()
125 "Prompt the user for an application to grab a link from, then go grab the link, and insert it at point"
126 (interactive)
127 (let* ((descriptors `(("F" "inder" org-mac-finder-insert-selected ,org-mac-grab-Finder-app-p)
128 ("m" "ail" org-mac-message-insert-selected ,org-mac-grab-Mail-app-p)
129 ("a" "ddressbook" org-mac-addressbook-insert-selected ,org-mac-grab-Addressbook-app-p)
130 ("s" "afari" org-mac-safari-insert-frontmost-url ,org-mac-grab-Safari-app-p)
131 ("f" "irefox" org-mac-firefox-insert-frontmost-url ,org-mac-grab-Firefox-app-p)
132 ("c" "hrome" org-mac-chrome-insert-frontmost-url ,org-mac-grab-Chrome-app-p)
133 ("t" "ogether" org-mac-together-insert-selected ,org-mac-grab-Together-app-p)))
134 (menu-string (make-string 0 ?x))
135 input)
137 ;; Create the menu string for the keymap
138 (mapc '(lambda (descriptor)
139 (when (elt descriptor 3)
140 (setf menu-string (concat menu-string "[" (elt descriptor 0) "]" (elt descriptor 1) " "))))
141 descriptors)
142 (setf (elt menu-string (- (length menu-string) 1)) ?:)
144 ;; Prompt the user, and grab the link
145 (message menu-string)
146 (setq input (read-char-exclusive))
147 (mapc '(lambda (descriptor)
148 (let ((key (elt (elt descriptor 0) 0))
149 (active (elt descriptor 3))
150 (grab-function (elt descriptor 2)))
151 (when (and active (eq input key))
152 (call-interactively grab-function))))
153 descriptors)))
155 (defalias 'omgl-grab-link 'omlg-grab-link
156 "Renamed, and this alias will be obsolete next revision.")
158 (defun org-mac-paste-applescript-links (as-link-list)
159 "Paste in a list of links from an applescript handler. The
160 links are of the form <link>::split::<name>"
161 (let* ((link-list
162 (mapcar
163 (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x) (setq x (match-string 1 x))) x)
164 (split-string as-link-list "[\r\n]+")))
165 split-link URL description orglink orglink-insert rtn orglink-list)
166 (while link-list
167 (setq split-link (split-string (pop link-list) "::split::"))
168 (setq URL (car split-link))
169 (setq description (cadr split-link))
170 (when (not (string= URL ""))
171 (setq orglink (org-make-link-string URL description))
172 (push orglink orglink-list)))
173 (setq rtn (mapconcat 'identity orglink-list "\n"))
174 (kill-new rtn)
175 rtn))
179 ;; Handle links from Firefox.app
181 ;; This code allows you to grab the current active url from the main
182 ;; Firefox.app window, and insert it as a link into an org-mode
183 ;; document. Unfortunately, firefox does not expose an applescript
184 ;; dictionary, so this is necessarily introduces some limitations.
186 ;; The applescript to grab the url from Firefox.app uses the System
187 ;; Events application to give focus to the firefox application, select
188 ;; the contents of the url bar, and copy it. It then uses the title of
189 ;; the window as the text of the link. There is no way to grab links
190 ;; from other open tabs, and further, if there is more than one window
191 ;; open, it is not clear which one will be used (though emperically it
192 ;; seems that it is always the last active window).
194 (defun as-mac-firefox-get-frontmost-url ()
195 (let ((result (do-applescript
196 (concat
197 "set oldClipboard to the clipboard\n"
198 "set frontmostApplication to path to frontmost application\n"
199 "tell application \"Firefox\"\n"
200 " activate\n"
201 " delay 0.15\n"
202 " tell application \"System Events\"\n"
203 " keystroke \"l\" using command down\n"
204 " keystroke \"c\" using command down\n"
205 " end tell\n"
206 " delay 0.15\n"
207 " set theUrl to the clipboard\n"
208 " set the clipboard to oldClipboard\n"
209 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
210 "end tell\n"
211 "activate application (frontmostApplication as text)\n"
212 "set links to {}\n"
213 "copy theResult to the end of links\n"
214 "return links as string\n"))))
215 (car (split-string result "[\r\n]+" t))))
217 (defun org-mac-firefox-get-frontmost-url ()
218 (interactive)
219 (message "Applescript: Getting Firefox url...")
220 (let* ((url-and-title (as-mac-firefox-get-frontmost-url))
221 (split-link (split-string url-and-title "::split::"))
222 (URL (car split-link))
223 (description (cadr split-link))
224 (org-link))
225 (when (not (string= URL ""))
226 (setq org-link (org-make-link-string URL description)))
227 (kill-new org-link)
228 org-link))
230 (defun org-mac-firefox-insert-frontmost-url ()
231 (interactive)
232 (insert (org-mac-firefox-get-frontmost-url)))
235 ;; Handle links from Google Chrome.app
236 ;; Grab the frontmost url from Google Chrome. Same limitations are
237 ;; Firefox because Chrome doesn't publish an Applescript dictionary
239 (defun as-mac-chrome-get-frontmost-url ()
240 (let ((result (do-applescript
241 (concat
242 "set oldClipboard to the clipboard\n"
243 "set frontmostApplication to path to frontmost application\n"
244 "tell application \"Google Chrome\"\n"
245 " activate\n"
246 " delay 0.15\n"
247 " tell application \"System Events\"\n"
248 " keystroke \"l\" using command down\n"
249 " keystroke \"c\" using command down\n"
250 " end tell\n"
251 " delay 0.15\n"
252 " set theUrl to the clipboard\n"
253 " set the clipboard to oldClipboard\n"
254 " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
255 "end tell\n"
256 "activate application (frontmostApplication as text)\n"
257 "set links to {}\n"
258 "copy theResult to the end of links\n"
259 "return links as string\n"))))
260 (car (split-string result "[\r\n]+" t))))
262 (defun org-mac-chrome-get-frontmost-url ()
263 (interactive)
264 (message "Applescript: Getting Chrome url...")
265 (let* ((url-and-title (as-mac-chrome-get-frontmost-url))
266 (split-link (split-string url-and-title "::split::"))
267 (URL (car split-link))
268 (description (cadr split-link))
269 (org-link))
270 (when (not (string= URL ""))
271 (setq org-link (org-make-link-string URL description)))
272 (kill-new org-link)
273 org-link))
275 (defun org-mac-chrome-insert-frontmost-url ()
276 (interactive)
277 (insert (org-mac-chrome-get-frontmost-url)))
280 ;; Handle links from Safari.app
281 ;; Grab the frontmost url from Safari.
283 (defun as-mac-safari-get-frontmost-url ()
284 (let ((result (do-applescript
285 (concat
286 "tell application \"Safari\"\n"
287 " set theUrl to URL of document 1\n"
288 " set theName to the name of the document 1\n"
289 " return theUrl & \"::split::\" & theName & \"\n\"\n"
290 "end tell\n"))))
291 (car (split-string result "[\r\n]+" t))))
293 (defun org-mac-safari-get-frontmost-url ()
294 (interactive)
295 (message "Applescript: Getting Safari url...")
296 (let* ((url-and-title (as-mac-safari-get-frontmost-url))
297 (split-link (split-string url-and-title "::split::"))
298 (URL (car split-link))
299 (description (cadr split-link))
300 (org-link))
301 (when (not (string= URL ""))
302 (setq org-link (org-make-link-string URL description)))
303 (kill-new org-link)
304 org-link))
306 (defun org-mac-safari-insert-frontmost-url ()
307 (interactive)
308 (insert (org-mac-safari-get-frontmost-url)))
313 ;; Handle links from together.app
317 (org-add-link-type "x-together-item" 'org-mac-together-item-open)
319 (defun org-mac-together-item-open (uid)
320 "Open the given uid, which is a reference to an item in Together"
321 (shell-command (concat "open \"x-together-item:" uid "\"")))
323 (defun as-get-selected-together-items ()
324 (do-applescript
325 (concat
326 "tell application \"Together\"\n"
327 " set theLinkList to {}\n"
328 " set theSelection to selected items\n"
329 " repeat with theItem in theSelection\n"
330 " set theLink to (get item link of theItem) & \"::split::\" & (get name of theItem) & \"\n\"\n"
331 " copy theLink to end of theLinkList\n"
332 " end repeat\n"
333 " return theLinkList as string\n"
334 "end tell")))
336 (defun org-mac-together-get-selected ()
337 (interactive)
338 (message "Applescript: Getting Togther items...")
339 (org-mac-paste-applescript-links (as-get-selected-together-items)))
341 (defun org-mac-together-insert-selected ()
342 (interactive)
343 (insert (org-mac-together-get-selected)))
348 ;; Handle links from Finder.app
352 (defun as-get-selected-finder-items ()
353 (do-applescript
354 (concat
355 "tell application \"Finder\"\n"
356 " set theSelection to the selection\n"
357 " set links to {}\n"
358 " repeat with theItem in theSelection\n"
359 " set theLink to \"file://\" & (POSIX path of (theItem as string)) & \"::split::\" & (get the name of theItem) & \"\n\"\n"
360 " copy theLink to the end of links\n"
361 " end repeat\n"
362 " return links as string\n"
363 "end tell\n")))
365 (defun org-mac-finder-item-get-selected ()
366 (interactive)
367 (message "Applescript: Getting Finder items...")
368 (org-mac-paste-applescript-links (as-get-selected-finder-items)))
370 (defun org-mac-finder-insert-selected ()
371 (interactive)
372 (insert (org-mac-finder-item-get-selected)))
377 ;; Handle links from AddressBook.app
381 (org-add-link-type "addressbook" 'org-mac-together-item-open)
383 (defun org-mac-together-item-open (uid)
384 "Open the given uid, which is a reference to an item in Together"
385 (shell-command (concat "open \"addressbook:" uid "\"")))
387 (defun as-get-selected-addressbook-items ()
388 (do-applescript
389 (concat
390 "tell application \"Address Book\"\n"
391 " set theSelection to the selection\n"
392 " set links to {}\n"
393 " repeat with theItem in theSelection\n"
394 " set theLink to \"addressbook://\" & (the id of theItem) & \"::split::\" & (the name of theItem) & \"\n\"\n"
395 " copy theLink to the end of links\n"
396 " end repeat\n"
397 " return links as string\n"
398 "end tell\n")))
400 (defun org-mac-addressbook-item-get-selected ()
401 (interactive)
402 (message "Applescript: Getting Address Book items...")
403 (org-mac-paste-applescript-links (as-get-selected-addressbook-items)))
405 (defun org-mac-addressbook-insert-selected ()
406 (interactive)
407 (insert (org-mac-addressbook-item-get-selected)))
410 (provide 'org-mac-link-grabber)
412 ;;; org-mac-link-grabber.el ends here