Saturday, October 31, 2009

Bring window to the front instead of launching new instance [Archive] - Ubuntu Forums

Bring window to the front instead of launching new instance [Archive] - Ubuntu Forums: "I want a launcher on my panel for Thunderbird (for example). But I want this launcer to check if an instance of Thunderbird is already open on the desktop: if it is open, then I just want to bring that window to the front; otherwise, I want to just launch Thunderbird. These days (with dapper), I think that t-bird just behaves this way without needing to mess around like this. But I'll illustrate with t-bird.

To do this, I first installed wmctrl from the universe repostirories. This is a command-line utility that can list open windows, bring them to the front etc. Then I wrote a script, using wmctrl. Then I created a launcher on the panel, which just runs the script. For thunderbird, the script looked like this (excluding the annotations in blue):

#! /bin/bash

WINTITLE='Mail/News' # Main Thunderbird window has this in titlebar
PROGNAME='mozilla-thunderbird' # This is the name of the binary for t-bird

# Use wmctrl to list all windows, count how many contain WINTITLE,
# and test if that count is non-zero:

if [ `wmctrl -l | grep -c '$WINTITLE'` != 0 ]
then
wmctrl -a '$WINTITLE' # If it exists, bring t-bird window to front
else
$PROGNAME & # Otherwise, just launch t-bird
fi
exit 0


Save this script as 'my-tbird-script' and make it executable. Click on panel, and create a 'Custom Application Launcher ... '. Make the command point to your script. Give it the t-bird icon. Change WINTITLE and PROGNAME to adapt your script to any other application.

As I said, for t-bird this is redundant now, I believe. For gnome-terminal, it is a bit tricky, because the title-bar doesn't have a 'signature' that makes it easily recognizable in the list of windows. You can work around that by changing your default profile in terminal so that the title bar always contains 'Terminal'."

No comments:

Post a Comment