Caffeinated Bitstream

Bits, bytes, and words.

Opening links in the correct Firefox instance

I'm often running multiple instances of Firefox on my desktop. Not just multiple windows, but multiple instances, with each associated with a different Firefox profile. I do this so I can have one instance running with the default profile for normal web use, and another completely separate instance running in an alternate profile loaded with development tools such as Firebug. This way, I can use different settings for certain development work, and if the development tools go haywire and interfere with the browser, they don't affect the Firefox instance I use for "normal" web browsing. I selected a different theme for the development instance, so I can visually tell them apart on the desktop.

One problem, though, is that when I click links in other applications (mail reader, feed reader, etc.) the link is sometimes opened in the development instance instead of my default instance. This is because the application uses the firefox executable to open the link, which sends the URL to the first instance it happens to see.

To fix this problem, I wrote a small python script which is smart enough to only send the link to the Firefox instance using the default profile. If no instance is using the default profile, a new instance is launched. This script uses Xlib to scan windows in search of the Firefox instance, so it will only work on X11-based platforms (Linux, Solaris, etc.).

To use this script for opening links in non-browser applications, you'll need to install the script as the default program for opening URLs in your desktop environment. In my GNOME environment, I accomplished this by using gconf-editor to set these gconf values:

Key Value
/desktop/gnome/applications/browser/exec /home/simmons/bin/ffremote.py
/desktop/gnome/url-handlers/http/command /home/simmons/bin/ffremote.py %s
/desktop/gnome/url-handlers/https/command /home/simmons/bin/ffremote.py %s

Here is the script, be sure to note the comments at the top:

  • ffremote.py - a python script to open links only in the Firefox instance running the desired profile.