1 ;;; org-mac-link-grabber.el --- Grab links and url from various mac
2 ;;; application and insert them as links into org-mode documents
4 ;; Copyright (c) 2010 Free Software Foundation, Inc.
6 ;; Author: Anthony Lander <anthony.lander@gmail.com>
8 ;; Keywords: org, mac, hyperlink
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)
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.
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
34 ;; Detailed comments for each application interface are inlined with
35 ;; the code. Here is a brief overview of how the code interacts with
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
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)))
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.
66 ;; You may customize which applications appear in the grab menu by
67 ;; customizing the group org-mac-link-grabber. Changes take effect
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"
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
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
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
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
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
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
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
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
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"
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
))
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) " "))))
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
))))
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>"
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
)
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"))
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
205 "set oldClipboard to the clipboard\n"
206 "set frontmostApplication to path to frontmost application\n"
207 "tell application \"Firefox\"\n"
210 " tell application \"System Events\"\n"
211 " keystroke \"l\" using command down\n"
212 " keystroke \"c\" using command down\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"
219 "activate application (frontmostApplication as text)\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 ()
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
))
233 (when (not (string= URL
""))
234 (setq org-link
(org-make-link-string URL description
)))
238 (defun org-mac-firefox-insert-frontmost-url ()
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
247 (defun as-mac-vimperator-get-frontmost-url ()
248 (let ((result (do-applescript
250 "set oldClipboard to the clipboard\n"
251 "set frontmostApplication to path to frontmost application\n"
252 "tell application \"Firefox\"\n"
255 " tell application \"System Events\"\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"
263 "activate application (frontmostApplication as text)\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 ()
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
))
278 (when (not (string= URL
""))
279 (setq org-link
(org-make-link-string URL description
)))
283 (defun org-mac-vimperator-insert-frontmost-url ()
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
295 "set oldClipboard to the clipboard\n"
296 "set frontmostApplication to path to frontmost application\n"
297 "tell application \"Google Chrome\"\n"
300 " tell application \"System Events\"\n"
301 " keystroke \"l\" using command down\n"
302 " keystroke \"c\" using command down\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"
309 "activate application (frontmostApplication as text)\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 ()
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
))
323 (when (not (string= URL
""))
324 (setq org-link
(org-make-link-string URL description
)))
328 (defun org-mac-chrome-insert-frontmost-url ()
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
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"
344 (car (split-string result
"[\r\n]+" t
))))
346 (defun org-mac-safari-get-frontmost-url ()
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
))
354 (when (not (string= URL
""))
355 (setq org-link
(org-make-link-string URL description
)))
359 (defun org-mac-safari-insert-frontmost-url ()
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 ()
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"
386 " return theLinkList as string\n"
389 (defun org-mac-together-get-selected ()
391 (message "Applescript: Getting Togther items...")
392 (org-mac-paste-applescript-links (as-get-selected-together-items)))
394 (defun org-mac-together-insert-selected ()
396 (insert (org-mac-together-get-selected)))
401 ;; Handle links from Finder.app
405 (defun as-get-selected-finder-items ()
408 "tell application \"Finder\"\n"
409 " set theSelection to the selection\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"
415 " return links as string\n"
418 (defun org-mac-finder-item-get-selected ()
420 (message "Applescript: Getting Finder items...")
421 (org-mac-paste-applescript-links (as-get-selected-finder-items)))
423 (defun org-mac-finder-insert-selected ()
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 ()
443 "tell application \"Address Book\"\n"
444 " set theSelection to the selection\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"
450 " return links as string\n"
453 (defun org-mac-addressbook-item-get-selected ()
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 ()
460 (insert (org-mac-addressbook-item-get-selected)))
463 (provide 'org-mac-link-grabber
)
465 ;;; org-mac-link-grabber.el ends here