org-mac-link-grabber.el 16 KB

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