org-mac-link-grabber.el 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. (defcustom org-mac-grab-Skim-app-p t
  121. "Enable menu option [S]kim to grab page links from Skim.app"
  122. :tag "Grab Skim.app page links"
  123. :group 'org-mac-link-grabber
  124. :type 'boolean)
  125. (defun omlg-grab-link ()
  126. "Prompt the user for an application to grab a link from, then go grab the link, and insert it at point"
  127. (interactive)
  128. (let* ((descriptors `(("F" "inder" org-mac-finder-insert-selected ,org-mac-grab-Finder-app-p)
  129. ("m" "ail" org-mac-message-insert-selected ,org-mac-grab-Mail-app-p)
  130. ("a" "ddressbook" org-mac-addressbook-insert-selected ,org-mac-grab-Addressbook-app-p)
  131. ("s" "afari" org-mac-safari-insert-frontmost-url ,org-mac-grab-Safari-app-p)
  132. ("f" "irefox" org-mac-firefox-insert-frontmost-url ,org-mac-grab-Firefox-app-p)
  133. ("v" "imperator" org-mac-vimperator-insert-frontmost-url ,org-mac-grab-Firefox+Vimperator-p)
  134. ("c" "hrome" org-mac-chrome-insert-frontmost-url ,org-mac-grab-Chrome-app-p)
  135. ("t" "ogether" org-mac-together-insert-selected ,org-mac-grab-Together-app-p)
  136. ("S" "kim" org-mac-skim-insert-page ,org-mac-grab-Skim-app-p)))
  137. (menu-string (make-string 0 ?x))
  138. input)
  139. ;; Create the menu string for the keymap
  140. (mapc '(lambda (descriptor)
  141. (when (elt descriptor 3)
  142. (setf menu-string (concat menu-string "[" (elt descriptor 0) "]" (elt descriptor 1) " "))))
  143. descriptors)
  144. (setf (elt menu-string (- (length menu-string) 1)) ?:)
  145. ;; Prompt the user, and grab the link
  146. (message menu-string)
  147. (setq input (read-char-exclusive))
  148. (mapc '(lambda (descriptor)
  149. (let ((key (elt (elt descriptor 0) 0))
  150. (active (elt descriptor 3))
  151. (grab-function (elt descriptor 2)))
  152. (when (and active (eq input key))
  153. (call-interactively grab-function))))
  154. descriptors)))
  155. (defalias 'omgl-grab-link 'omlg-grab-link
  156. "Renamed, and this alias will be obsolete next revision.")
  157. (defun org-mac-paste-applescript-links (as-link-list)
  158. "Paste in a list of links from an applescript handler. The
  159. links are of the form <link>::split::<name>"
  160. (let* ((link-list
  161. (mapcar
  162. (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x) (setq x (match-string 1 x))) x)
  163. (split-string as-link-list "[\r\n]+")))
  164. split-link URL description orglink orglink-insert rtn orglink-list)
  165. (while link-list
  166. (setq split-link (split-string (pop link-list) "::split::"))
  167. (setq URL (car split-link))
  168. (setq description (cadr split-link))
  169. (when (not (string= URL ""))
  170. (setq orglink (org-make-link-string URL description))
  171. (push orglink orglink-list)))
  172. (setq rtn (mapconcat 'identity orglink-list "\n"))
  173. (kill-new rtn)
  174. rtn))
  175. ;; Handle links from Firefox.app
  176. ;;
  177. ;; This code allows you to grab the current active url from the main
  178. ;; Firefox.app window, and insert it as a link into an org-mode
  179. ;; document. Unfortunately, firefox does not expose an applescript
  180. ;; dictionary, so this is necessarily introduces some limitations.
  181. ;;
  182. ;; The applescript to grab the url from Firefox.app uses the System
  183. ;; Events application to give focus to the firefox application, select
  184. ;; the contents of the url bar, and copy it. It then uses the title of
  185. ;; the window as the text of the link. There is no way to grab links
  186. ;; from other open tabs, and further, if there is more than one window
  187. ;; open, it is not clear which one will be used (though emperically it
  188. ;; seems that it is always the last active window).
  189. (defun as-mac-firefox-get-frontmost-url ()
  190. (let ((result (do-applescript
  191. (concat
  192. "set oldClipboard to the clipboard\n"
  193. "set frontmostApplication to path to frontmost application\n"
  194. "tell application \"Firefox\"\n"
  195. " activate\n"
  196. " delay 0.15\n"
  197. " tell application \"System Events\"\n"
  198. " keystroke \"l\" using {command down}\n"
  199. " keystroke \"a\" using {command down}\n"
  200. " keystroke \"c\" using {command down}\n"
  201. " end tell\n"
  202. " delay 0.15\n"
  203. " set theUrl to the clipboard\n"
  204. " set the clipboard to oldClipboard\n"
  205. " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
  206. "end tell\n"
  207. "activate application (frontmostApplication as text)\n"
  208. "set links to {}\n"
  209. "copy theResult to the end of links\n"
  210. "return links as string\n"))))
  211. (car (split-string result "[\r\n]+" t))))
  212. (defun org-mac-firefox-get-frontmost-url ()
  213. (interactive)
  214. (message "Applescript: Getting Firefox url...")
  215. (let* ((url-and-title (as-mac-firefox-get-frontmost-url))
  216. (split-link (split-string url-and-title "::split::"))
  217. (URL (car split-link))
  218. (description (cadr split-link))
  219. (org-link))
  220. (when (not (string= URL ""))
  221. (setq org-link (org-make-link-string URL description)))
  222. (kill-new org-link)
  223. org-link))
  224. (defun org-mac-firefox-insert-frontmost-url ()
  225. (interactive)
  226. (insert (org-mac-firefox-get-frontmost-url)))
  227. ;; Handle links from Google Firefox.app running the Vimperator extension
  228. ;; Grab the frontmost url from Firefox+Vimperator. Same limitations are
  229. ;; Firefox
  230. (defun as-mac-vimperator-get-frontmost-url ()
  231. (let ((result (do-applescript
  232. (concat
  233. "set oldClipboard to the clipboard\n"
  234. "set frontmostApplication to path to frontmost application\n"
  235. "tell application \"Firefox\"\n"
  236. " activate\n"
  237. " delay 0.15\n"
  238. " tell application \"System Events\"\n"
  239. " keystroke \"y\"\n"
  240. " end tell\n"
  241. " delay 0.15\n"
  242. " set theUrl to the clipboard\n"
  243. " set the clipboard to oldClipboard\n"
  244. " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
  245. "end tell\n"
  246. "activate application (frontmostApplication as text)\n"
  247. "set links to {}\n"
  248. "copy theResult to the end of links\n"
  249. "return links as string\n"))))
  250. (replace-regexp-in-string "\s+-\s+Vimperator" "" (car (split-string result "[\r\n]+" t)))))
  251. (defun org-mac-vimperator-get-frontmost-url ()
  252. (interactive)
  253. (message "Applescript: Getting Vimperator url...")
  254. (let* ((url-and-title (as-mac-vimperator-get-frontmost-url))
  255. (split-link (split-string url-and-title "::split::"))
  256. (URL (car split-link))
  257. (description (cadr split-link))
  258. (org-link))
  259. (when (not (string= URL ""))
  260. (setq org-link (org-make-link-string URL description)))
  261. (kill-new org-link)
  262. org-link))
  263. (defun org-mac-vimperator-insert-frontmost-url ()
  264. (interactive)
  265. (insert (org-mac-vimperator-get-frontmost-url)))
  266. ;; Handle links from Google Chrome.app
  267. ;; Grab the frontmost url from Google Chrome. Same limitations are
  268. ;; Firefox because Chrome doesn't publish an Applescript dictionary
  269. (defun as-mac-chrome-get-frontmost-url ()
  270. (let ((result (do-applescript
  271. (concat
  272. "set oldClipboard to the clipboard\n"
  273. "set frontmostApplication to path to frontmost application\n"
  274. "tell application \"Google Chrome\"\n"
  275. " activate\n"
  276. " delay 0.15\n"
  277. " tell application \"System Events\"\n"
  278. " keystroke \"l\" using command down\n"
  279. " keystroke \"c\" using command down\n"
  280. " end tell\n"
  281. " delay 0.15\n"
  282. " set theUrl to the clipboard\n"
  283. " set the clipboard to oldClipboard\n"
  284. " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
  285. "end tell\n"
  286. "activate application (frontmostApplication as text)\n"
  287. "set links to {}\n"
  288. "copy theResult to the end of links\n"
  289. "return links as string\n"))))
  290. (car (split-string result "[\r\n]+" t))))
  291. (defun org-mac-chrome-get-frontmost-url ()
  292. (interactive)
  293. (message "Applescript: Getting Chrome url...")
  294. (let* ((url-and-title (as-mac-chrome-get-frontmost-url))
  295. (split-link (split-string url-and-title "::split::"))
  296. (URL (car split-link))
  297. (description (cadr split-link))
  298. (org-link))
  299. (when (not (string= URL ""))
  300. (setq org-link (org-make-link-string URL description)))
  301. (kill-new org-link)
  302. org-link))
  303. (defun org-mac-chrome-insert-frontmost-url ()
  304. (interactive)
  305. (insert (org-mac-chrome-get-frontmost-url)))
  306. ;; Handle links from Safari.app
  307. ;; Grab the frontmost url from Safari.
  308. (defun as-mac-safari-get-frontmost-url ()
  309. (let ((result (do-applescript
  310. (concat
  311. "tell application \"Safari\"\n"
  312. " set theUrl to URL of document 1\n"
  313. " set theName to the name of the document 1\n"
  314. " return theUrl & \"::split::\" & theName & \"\n\"\n"
  315. "end tell\n"))))
  316. (car (split-string result "[\r\n]+" t))))
  317. (defun org-mac-safari-get-frontmost-url ()
  318. (interactive)
  319. (message "Applescript: Getting Safari url...")
  320. (let* ((url-and-title (as-mac-safari-get-frontmost-url))
  321. (split-link (split-string url-and-title "::split::"))
  322. (URL (car split-link))
  323. (description (cadr split-link))
  324. (org-link))
  325. (when (not (string= URL ""))
  326. (setq org-link (org-make-link-string URL description)))
  327. (kill-new org-link)
  328. org-link))
  329. (defun org-mac-safari-insert-frontmost-url ()
  330. (interactive)
  331. (insert (org-mac-safari-get-frontmost-url)))
  332. ;;
  333. ;;
  334. ;; Handle links from together.app
  335. ;;
  336. ;;
  337. (org-add-link-type "x-together-item" 'org-mac-together-item-open)
  338. (defun org-mac-together-item-open (uid)
  339. "Open the given uid, which is a reference to an item in Together"
  340. (shell-command (concat "open -a Together \"x-together-item:" uid "\"")))
  341. (defun as-get-selected-together-items ()
  342. (do-applescript
  343. (concat
  344. "tell application \"Together\"\n"
  345. " set theLinkList to {}\n"
  346. " set theSelection to selected items\n"
  347. " repeat with theItem in theSelection\n"
  348. " set theLink to (get item link of theItem) & \"::split::\" & (get name of theItem) & \"\n\"\n"
  349. " copy theLink to end of theLinkList\n"
  350. " end repeat\n"
  351. " return theLinkList as string\n"
  352. "end tell")))
  353. (defun org-mac-together-get-selected ()
  354. (interactive)
  355. (message "Applescript: Getting Togther items...")
  356. (org-mac-paste-applescript-links (as-get-selected-together-items)))
  357. (defun org-mac-together-insert-selected ()
  358. (interactive)
  359. (insert (org-mac-together-get-selected)))
  360. ;;
  361. ;;
  362. ;; Handle links from Finder.app
  363. ;;
  364. ;;
  365. (defun as-get-selected-finder-items ()
  366. (do-applescript
  367. (concat
  368. "tell application \"Finder\"\n"
  369. " set theSelection to the selection\n"
  370. " set links to {}\n"
  371. " repeat with theItem in theSelection\n"
  372. " set theLink to \"file://\" & (POSIX path of (theItem as string)) & \"::split::\" & (get the name of theItem) & \"\n\"\n"
  373. " copy theLink to the end of links\n"
  374. " end repeat\n"
  375. " return links as string\n"
  376. "end tell\n")))
  377. (defun org-mac-finder-item-get-selected ()
  378. (interactive)
  379. (message "Applescript: Getting Finder items...")
  380. (org-mac-paste-applescript-links (as-get-selected-finder-items)))
  381. (defun org-mac-finder-insert-selected ()
  382. (interactive)
  383. (insert (org-mac-finder-item-get-selected)))
  384. ;;
  385. ;;
  386. ;; Handle links from AddressBook.app
  387. ;;
  388. ;;
  389. (org-add-link-type "addressbook" 'org-mac-addressbook-item-open)
  390. (defun org-mac-addressbook-item-open (uid)
  391. "Open the given uid, which is a reference to an item in Together"
  392. (shell-command (concat "open \"addressbook:" uid "\"")))
  393. (defun as-get-selected-addressbook-items ()
  394. (do-applescript
  395. (concat
  396. "tell application \"Address Book\"\n"
  397. " set theSelection to the selection\n"
  398. " set links to {}\n"
  399. " repeat with theItem in theSelection\n"
  400. " set theLink to \"addressbook://\" & (the id of theItem) & \"::split::\" & (the name of theItem) & \"\n\"\n"
  401. " copy theLink to the end of links\n"
  402. " end repeat\n"
  403. " return links as string\n"
  404. "end tell\n")))
  405. (defun org-mac-addressbook-item-get-selected ()
  406. (interactive)
  407. (message "Applescript: Getting Address Book items...")
  408. (org-mac-paste-applescript-links (as-get-selected-addressbook-items)))
  409. (defun org-mac-addressbook-insert-selected ()
  410. (interactive)
  411. (insert (org-mac-addressbook-item-get-selected)))
  412. ;;
  413. ;;
  414. ;; Handle links from Skim.app
  415. ;;
  416. ;; A rewriting of some code originally by Christopher Suckling from org-mac-protocol
  417. (org-add-link-type "skim" 'org-mac-skim-open)
  418. (defun org-mac-skim-open (uri)
  419. "Visit page of pdf in Skim"
  420. (let* ((page (when (string-match "::\\(.+\\)\\'" uri)
  421. (match-string 1 uri)))
  422. (document (substring uri 0 (match-beginning 0))))
  423. (do-applescript
  424. (concat
  425. "tell application \"Skim\"\n"
  426. "activate\n"
  427. "set theDoc to \"" document "\"\n"
  428. "set thePage to " page "\n"
  429. "open theDoc\n"
  430. "go document 1 to page thePage of document 1\n"
  431. "end tell"))))
  432. (defun as-get-skim-page-link ()
  433. (do-applescript
  434. (concat
  435. "tell application \"Skim\"\n"
  436. "set theDoc to front document\n"
  437. "set theTitle to (name of theDoc)\n"
  438. "set thePath to (path of theDoc)\n"
  439. "set thePage to (get index for current page of theDoc)\n"
  440. "set theSelection to selection of theDoc\n"
  441. "set theContent to contents of (get text for theSelection)\n"
  442. "if theContent is missing value then\n"
  443. "set theContent to theTitle & \", p. \" & thePage\n"
  444. "end if\n"
  445. "set theLink to \"skim://\" & thePath & \"::\" & thePage & "
  446. "\"::split::\" & theContent\n"
  447. "end tell\n"
  448. "return theLink as string\n")))
  449. (defun org-mac-skim-get-page ()
  450. (interactive)
  451. (message "Applescript: Getting Skim page link...")
  452. (let* ((url-and-title (as-get-skim-page-link))
  453. (split-link (split-string url-and-title "::split::"))
  454. (URL (car split-link))
  455. (description (cadr split-link))
  456. (org-link))
  457. (when (not (string= URL ""))
  458. (setq org-link (org-make-link-string URL description)))
  459. (kill-new org-link)
  460. org-link))
  461. (defun org-mac-skim-insert-page ()
  462. (interactive)
  463. (insert (org-mac-skim-get-page)))
  464. (provide 'org-mac-link-grabber)
  465. ;;; org-mac-link-grabber.el ends here