I use emacs’ html-mode fairly often. And on OS X, it’s always been a problem that I couldn’t use Chrome to preview HTML pages straight from emacs. I found some help on StackOveflow, but not much in the way that it solved my issue. 

I went ahead and made a python script that gets called from emacs every time I save an html file in html-mode. 

This is two step process: 

1. We edit our .emacs file located within your home directory (/~)

The below code should be copy pasted or entered in your .emacs located at ~/

1
2
3
4
5
6

;;set default browser for browse-url-of
(setq browse-url-browser-function 
    'browse-url-generic)
(setq browse-url-generic-program 
    "google-chrome-open-url")

Line 3: calls the browse-url-generic function which in runs a “generic program” called google-chrome-open-url - this is our python script(see below). 

2. We create a new python executable with the following code

1
2
3
4
5
#!/usr/bin/env python
# Name this file “google-chrome-open-url”
from subprocess import call
import sys
call(['/usr/bin/open', '-a', "/Applications/Google Chrome.app", sys.argv[1]])

The above is a straight forward python script. That uses the call method from the subprocess module to execute a terminal based command to open the app. The thing to note here is that the script takes an argument from the command line. This argument is supplied by emacs lisp lines above while making a call to this python script (“google-chrome-open-url”). This is where we use sys.

Make sure that this file is executable and the path to this is located in your PATH var. Instead of keeping this file around in a seperate directory and then specifying the path, I just moved it to my /usr/bin.

This was possible because you can use OS X’s CLI / Terminal.app to open/execute apps via the /usr/bin/open command.

Hope this helps someone out there.