#!/bin/bash # CVS: $Id: mozer,v 1.6 2005/06/17 11:22:05 becki Exp $ # Author: Stefan Beckert http://becki.skiclub-mitwitz.de # License: Public Domain # Description: # Use firefox, mozilla or netscape from the command line to display html # pages, text files or images (like 'less' for text files). # Each invocation of this script creates a new tab in your browser # (tabbed browsing). # If your browser is not running yet, it will be started. # Examples: # mozer becki.skiclub-mitwitz.de # opens webpage in new tab # mozer -s becki.skiclub-mitwitz.de # opens webpage in same tab # mozer ~/.profile # opens local file in new tab # mozer ftp://url # opens ftp-url in new tab # mozer -s javascript:home() # opens your homepage in same tab #### CONFIGURATION (optional) ################################################# # # customize path to browser startup script here if necessary: # This script expects the path to your default browser in the environment # variable $BROWSER # You can set it with e.g.: "export BROWSER=firefox" in ~/.profile # If you don't want that, set the path to your browser manually here # Examples: #browser=firefox #browser=/usr/bin/firefox #browser=/user/bin/mozilla #browser=/opt/netscape/netscape # ############################################################################### # default values: usage="Usage: \"$(basename $0) [-s] url\" to open url in new [or same] tab of your browser" debug='false' # do nothing instead of echo same=0 # use new tab, not the same window # for debugging only: (create a symlink called debugmozer -> mozer and call this # script with 'debugmozer') if [ $(basename $0) == debugmozer ]; then debug='echo' fi # get path to browser if not configured above: if [ ! "$browser" ]; then if [ "$BROWSER" ]; then $debug "using environment variable \$BROWSER=$BROWSER" >&2 browser=$BROWSER # use environment variable for browser locaton fi else $debug "user has set \$browser=$browser" >&2 fi # nothing set: try to detect browser: if [ ! "$browser" ]; then if which firefox 1>/dev/null 2>/dev/null; then browser=firefox elif which mozilla 1>/dev/null 2>/dev/null; then browser=mozilla elif which netscape 1>/dev/null 2>/dev/null; then browser=netscape fi if [ "$browser" ]; then echo "detected $browser, set \$BROWSER environment variable if you want to change this" >&2 else echo "ERROR: Can't find your browser, set \$BROWSER environment variable!" >&2 echo -e $usage >&2 exit 1 fi fi # get options: while getopts ":s?" Option do case $Option in s ) same=1 ;; # user doesn't want a new tab ? ) echo -e $usage >&2; exit 0 ;; esac done shift $(($OPTIND - 1)) # Move argument pointer to next. # Test if instace of browser already exists: $browser -remote "ping()" 2> /dev/null off=$? # Test number of remaining command line arguments: if [ $# -ne 1 ]; then echo "ERROR: Wrong number of arguments!" >&2 echo -e $usage >&2 exit 1 fi url=$1 # set default value of url, may be overwritten later # Test if argument is a local file & exists: if [ -e $1 ]; then $debug "$1 is a local file" >&2 # Try to cd into specified directory: # Mozilla etc. require the absolte pathname of the file. # To cd into the specified directory is a workaround, because I don't know # how to determine the absolute pathname directly. Any suggestions? dirn=$(dirname $1) cd $dirn &> /dev/null if [ $? -eq 0 ]; then url="file://$PWD/$(basename $1)" else echo "ERROR: Can't change into $dirn" >&2 exit 2 fi # no local file => Test if argument is a fully qualified url: elif [ $(expr substr $1 1 7) == 'http://' ]; then # 1st char has index 1 not 0! $debug "$1 is a http-url" >&2 elif [ $(expr substr $1 1 8) == 'https://' ]; then $debug "$1 is a https-url" >&2 elif [ $(expr substr $1 1 6) == 'ftp://' ]; then $debug "$1 is a ftp-url" >&2 elif [ $(expr substr $1 1 11) == 'javascript:' ]; then $debug "$1 is a javascript-url" >&2 # no file, no url: fallback to hypertext: else url="http://$1" echo -e "$1 is neither a local file nor a fully qualified url\n\ -> assuming $url" >&2 fi #$debug "url: $url" if [ $off -eq 0 ]; then # open url in running window: # We use remote control of unix mozilla here, see # http://www.mozilla.org/unix/remote.html if [ $same -eq 0 ]; then $debug "loading $url in new tab" >&2 $browser -remote "openURL($url, new-tab)" else $debug "loading $url in same window" >&2 $browser -remote "openURL($url)" fi else # open mozilla with url: # If browser is not running yet, start browser. $debug "starting $(basename $browser) with $url" >&2 $browser $url & fi