Penguin
Note: You are viewing an old revision of this page. View the current version.

A number of people have problem with Firefox not launching things properly. Here's a script that might help you clear some of that up:

#!/bin/bash
# Description: script for firefox use on KDE, GNOME, graphical console...
# Version:     1.0b
# last version at http://www.iespana.es/infogeneral_es/contrib/mozz
# Author:      MarC, suggestions at marc_contrib@ramonvinyes.es
# Comment:     I'd like to include it in the "firefox" official script... Anyone knows how to contact the development team?

#the following options are applied when mozilla is already running...
NEW="new-window"
#to open the requested content in a new window (default)
#NEW="new-tab"
#to open the requested content in a new tab (uncomment previous line to make it active)

#IMPORTANT: below you must set the path of firefox in your system
FIREFOX_PATH="/usr/bin"

if [ "$1" != "" ]
then
 if [ "`expr substr "$1" 1 7`" = "http://" ] || [ "`expr substr "$1" 1 6`" = "about:" ] || \
    [ "`expr substr "$1" 1 6`" = "ftp://" ]  || [ "`expr substr "$1" 1 8`" = "https://" ]  || \
    [ "`expr substr "$1" 1 7`" = "file://" ]
 then
  FILE_PATH="$1"
 else
  if [ "`expr substr "$1" 1 3`" = "www" ]
  then
   FILE_PATH="http://$1"
  else
   if [ "`expr substr "$1" 1 1`" = "/" ]
   then
    FILE_PATH="file://$1"
   else
    if [ -f "$1" ]
    then
     FILE_PATH="file://`pwd`/$1"
    else
     FILE_PATH="http://$1"
    fi
   fi
  fi
 fi
else
 FILE_PATH=""
fi

#to fix space characters problem
FILE_PATH=${FILE_PATH//" "/"%20"}
if [ "${FILE_PATH}" != "" ]
then
echo "Opening file: '${FILE_PATH}'..."
fi

if ps ux | grep "${FIREFOX_PATH}/firefox-bin" | grep -v "grep"; then
#firefox is already running in another window, open in a new tab/new window
${FIREFOX_PATH}/mozilla-xremote-client -a firefox "openURL($FILE_PATH,$NEW)"&
# Note that there should be no space between the comma
# and the command after the url or you get a  "Error: Failed to send command: 509 internal error".

#uncomment the following line and comment the previous uncommented line if you have firefox <= 0.8
#${FIREFOX_PATH}/firefox -remote "openURL($FILE_PATH, $NEW)"&

else
#start firefox
${FIREFOX_PATH}/firefox $FILE_PATH&
fi

We also have a copy of DanielLawson's old Firefox script, hacked up to support post-0.8 versions.


EDIT (25-06-2005)


If you have problems with this script, try this part in the last part of it (the script seemed to have problems with the the variable in the grep command), instead of the the long if..... ;then line

if ps ux | grep "firefox-bin" | grep -v "grep" &> /dev/null
then
#firefox is already running in another window, open in a new tab/new window
${FIREFOX_PATH}/mozilla-xremote-client -a firefox "openURL($FILE_PATH,$NEW)"&

Also, I made the NEW-variable parameter dependent, that gives more flexibility for hotkey configuration. Just replace NEW=new-tab/window with the next few lines. Default remains new-window

#apply tab or window on commandline as second argument, otherwise a new window is opened
if [ "$2" != "" ]
then
NEW="new-$2"
else
NEW="new-window"
fi

End of edit


And here's another, which some kind soul can refactor for me please if it's not as good as the first:

Set this script as your URL handler to make it open in a new tab if Firefox is running, or start it if it isn't. Change the BROWSER variable to point to your Firefox binary. DEFAULT_TO_HOMEPAGE specifies whether new tabs or windows should start with the homepage you've set in your browser -- 0 = no, 1 = yes (only works in browser versions 0.9 and newer). OPEN_IN_NEW_TAB specifies how you want URLs to open -- 0 = in a new window, 1 = in a new tab.

#!/bin/bash
exec &>/dev/null

BROWSER=/path/to/firefox/binary
DEFAULT_TO_HOMEPAGE=1
OPEN_IN_NEW_TAB=1

if $BROWSER -v | egrep -cq '\<0\.9' ; then
        EXTRA_OPTIONS="-a firefox"

        if [ "$DEFAULT_TO_HOMEPAGE" -ne 0 -a -z "$*" ] ; then
                PROFILEPATH="$( grep ^Path= ~/.mozilla/firefox/profiles.ini | cut -f2 -d= )"
                set -- "$( fgrep '"browser.startup.homepage"' "~/.mozilla/firefox/$PROFILEPATH/prefs.js" | cut -f4 -d\" )"
        fi
else
        EXTRA_OPTIONS=
fi

if [ "$OPEN_IN_NEW_TAB" -ne 0 ] ; then INTO=new-tab ; else INTO=new-window ; fi

$BROWSER $EXTRA_OPTIONS -remote "ping()" || exec $BROWSER "$@"
$BROWSER $EXTRA_OPTIONS -remote "openurl($1,$INTO)"