org-mac-link-grabber.el 19 KB

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