3

When editing code or org-mode, I often need the URL of the page I'm viewing in Google Chrome. Is there a way to do this from Emacs? Or with an external helper script if necessary?

An example use case would be editing code:

# See <insert current link to StackOverflow on bash variables>
if [[ -z "${param// }" ]]; then foo; fi
Joe
  • 1,312
  • 1
  • 8
  • 19
  • 1
    What operating system are you using? How to get data from another application will depend on the platform. – glucas Nov 11 '16 at 18:36
  • partial answer might be in http://unix.stackexchange.com/questions/241658/is-there-a-way-to-get-the-url-from-current-tab-in-google-chrome – icarus Nov 12 '16 at 00:47
  • Or https://github.com/stuartsierra/org-mode/blob/master/contrib/lisp/org-mac-link.el – icarus Nov 12 '16 at 00:49
  • I'm on Linux and Mac, but it'd be nice to have a way for all OS's to get it and wrap it in an elisp function. – Joe Nov 12 '16 at 07:46
  • I use `alt d` to quickly highlight the omnibox in Chrome, and the same shortcut works fine in most browsers. It's a pretty quick way to copy and paste a link to Emacs. –  Nov 12 '16 at 13:26

1 Answers1

1

Mac OS

You can use org-mac-link.el, it produces an Org link since it can know not only URL but also TITLE by using AppleScript. You can install it via

  • Install org-plus-contrib from Org ELPA
  • or install org-mac-link from MELPA
  • or contrib/lisp/ under Org source code (get it via Git repo or tarball)

by the way, even you install via ELPA, you still need to setup autoloads for commands you need or simply require the feature since org-mac-link.el doesn't setup its autoload cookies.

GNU/Linux

As far as I know, X11 doesn't provide tool like AppleScript. So you need to do it manually or some automation tool. For example, to get URL:

  1. Switch to Google Chrome
  2. Move cursor to Address Bar and select (or copy) URL
  3. Switch to Emacs (or Terminal if you are running Emacs in it)
  4. Yank from PRIMARY selection (or clipboard)

You can use tools such as xdotool(1) to simulate the above steps, I tried this way a few days ago in grab-x-link.el, it supports Firefox and Chromium but not Chrome, it works for me in most cases.


There is also org-protocol.el. Basically, you register a URL scheme / protocol for emacsclient, then you find some way (such as Bookmarklet in web browser) to activate URL in that scheme.

xuchunyang
  • 14,302
  • 1
  • 18
  • 39