org-mac-link-grabber.el 16 KB

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