Friday, March 23, 2018

Opening Windows' Google Chrome for links from WSL Ubuntu

Aside from Chrome, MS Office, and some work chat apps, most of my time is spent in WSL (using VcXsrv for X11 with WGL enabled).

One thing I was missing was how to open links in the Windows' version of Chrome (I've yet to get sound working in WSL...).

For most cases, exporting BROWSER to the /mnt/c path of Google Chrome is sufficient, but I also use Markdown heavily in Vim and have a macro to generate HTML from it.

Below is a script to map the WSL paths to Windows, and to adjust some of my known DrvFS mounts to proper Windows mounts.  If I'm browsing a path local to WSL, it instead opens my WSL version of Chrome.

I find it best to open a command prompt and type "dir /x c:\" to get the 8.3 short-name of "C:\Program Files (x86)" since WSL seems to choke on spaces in paths.

I then renamed /usr/local/google-chrome to /usr/local/google-chrome_main and symlink'd /usr/local/google-chrome to this


#!/bin/bash

# Purpose:
# 1. to open local file paths from /mnt/DRV in Windows' Google Chrome
# 2. to translate known mount points (in this example, /home/aaron/addc_g is a DrvFS mount to my ExpanDrive H: for Google Drive)

# Assumes:
# 1. Google chrome is installed in windows
# 2. Google chrome is installed in WSL
# 3. the output of "dir /x c:\" shows the "Program Files (x86)" as "PROGRA~2"

WIN_GOOGLE="/mnt/c/PROGRA~2/Google/Chrome/Application/chrome.exe"
LIN_GOOGLE="google-chrome_main"

if [[ $1 == /mnt/* ]]
then
    #echo "windows"
    url=${1:5}
    url=$(echo "${url}" | sed 's/^\///' | sed 's/\//\\\\/g' | sed 's/^./\0:/')
    eval ${WIN_GOOGLE} "${url}"
elif [[ $1 == /home/aaron/addc_g/* ]]
then
    #echo "windows g-drive"
    url=${1:19}
    url="/h/${url}"
    url=$(echo "${url}" | sed 's/^\///' | sed 's/\//\\\\/g' | sed 's/^./\0:/')
    eval ${WIN_GOOGLE} "${url}"
elif [[ $1 == http* ]]
then
    #echo "windows url"
    eval ${WIN_GOOGLE} "$1"
else
    #echo "linux"
    url=$1
    eval ${LIN_GOOGLE} ${url}
fi
#echo $1
#echo ${url}

View the GIST

For general Bash usage, my .bashrc has: export BROWSER="/mnt/c/PROGRA~2/Google/Chrome/Application/chrome.exe"

No comments:

Post a Comment