org-mac-link-grabber.el 16 KB

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