Shell script for launching Firefox

What follows is a small shell script which will launch Firefox in a sensible manner: if it’s open, make use of it; only if not, launch a new session.

This should get rid of those pesky messages telling you that Firefox is already running, and would you like to use another profile?

And if you use this script, which I’ve proudly dubbed browser-new, in your other applications, such as email program, it will launch links in the nice way you’ve always wanted.

The script will also focus the Firefox window if wmctrl is available. This is a one-liner and can be disabled by commenting that line (the last one, add a # in front of it). But I think it’s useful if you happen to iconify Firefox or use a preload manager like AllTray or KDocker.

The script takes one parameter, which is the URL you want to open. If none is given, it uses about:blank, which means a blank page.

Oh, and if you’d rather open new pages in new windows rather than new tabs, replace new-tab with new-window.

mozilla-xremote-client -a firefox "ping()" && \
mozilla-xremote-client -a firefox "openurl(${1:-about:blank},new-tab)" || \
firefox "${1:-about:blank}"

which wmctrl &>/dev/null && wmctrl -x -R Firefox