Skip to content

Instantly share code, notes, and snippets.

@agzam
Last active October 7, 2022 15:21
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agzam/e90123c0820991ae39fbe61fed5a57df to your computer and use it in GitHub Desktop.
Save agzam/e90123c0820991ae39fbe61fed5a57df to your computer and use it in GitHub Desktop.
Send browser URLs to Emacs EWW (Mac)

Instructions and the script for https://www.youtube.com/watch?v=05wghiNzIj0

EWW - Emacs’ built-in browser is great for many things. Simple use cases include:

  • Opening any file on GitHub/GitLab without having to download it
  • Log investigation e.g., for GitHub Actions
  • Reading PDF - link to a pdf doc opens it in pdf-tools (if installed)
  • Bypassing a paywall

This script is about sending URLs from browser to Emacs. With little effort and scripting you can even do some other awesome things (they don’t have to be EWW-related), a few ideas come to mind:

  • Display the diff of changes (magit-diff) for a Pull Request URL
  • Jump to email (from webmail interface) and open it in mu4e, notmuch, etc.
  • Fetch the transcript for a YouTube video and show it in a buffer

And if you think about it, there’s not only URL, you can collect more context and send it over to Emacs to deal with it.

So how do you send current URL from browser to Emacs? Two ways come to mind: org-protocol, and on a Mac, you can use a QuickAction. This doc describes the latter. Share your ideas for the similar thing that works in Linux or Windows.

Prerequisite

You need to be running Emacs in daemon mode, so you can use emacsclient. See Emacs manual to learn more about using Emacs as a server.

How to create a Quick Action?

  • Open Automator app
  • New -> Quick Action -> Select Run Applescript from the menu on the left
  • use the following applescript code:

Note that I use Brave Browser, you can either modify the if statement to include other browsers, or change it to the main browser of your choice.

on run {input, parameters}
  tell application "System Events" to set frontApp to name of first process whose frontmost is true

  if (frontApp = "Brave Browser") then           -- <- change this to your browser, e.g. Firefox
    using terms from application "Google Chrome" -- <- and this 
      tell application frontApp to set currentTabUrl to URL of active tab of front window
    end using terms from
    set argsString to "'(funcall-interactively (quote eww) \"" & currentTabUrl & "\" :new-buffer)'"
  end if

  set EC to "/usr/local/bin/emacsclient --no-wait -e " -- check emacsclient location on disk

  -- uncomment next line to troubleshoot, it will display exact command to run from shell (if something goes wrong)
  -- display dialog EC & argsString  
  do shell script EC & argsString
  tell application "Emacs" to activate

  return input
end run
  • When you safe the workflow it should appear in services menu. Help -> Search -> Services
  • You can bind the key to it in System Preferences -> Keyboard -> Shortcuts
@macownersclub
Copy link

Here a simplified version for Safari:

tell application "Safari"
	set theUrl to URL of document 1
	set theName to the name of the document 1
end tell


do shell script "/opt/homebrew/bin/emacsclient --no-wait -c -e '(eww-browse-url \"" & theUrl & "\")'"

tell application "Emacs" to activate

Salut

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment