The Cube - Assimilating Technology

The Cube

Fluxbox & wmctrl

For Humans only


Print E-mail
Tuesday, 11 August 2009 23:12

Fluxbox promotes minimalistic usage, and to keep in-line with this approach, you can use wmctrl to prevent more than one instance of an application to be open.

Check if you have wmctrl installed

which wmctrl

If it is installed, you will be presented with the path, if not, you will be told. If not please install using your distrobution's package install method.

Save the script below as 'find_app.sh' (anywhere in your filesystem)

#!/bin/bash
# Find_app
# Author: Lucas van Staden (lvs at dedmeet.com / www.dedmeet.com)
# This little script will try and find the application attempting to start
# in the running processes, and if found, focus the application
# if not found, a new instance will start
# usage:
# find_app.sh <application with full path>
# params
# 1 - application to start (full path)
# helper applications
WMCTRL=`which wmctrl`;
GREP=`which grep`;
APPLICATION=$1;
BASENAME=`basename $APPLICATION`;
BASENAME=`echo $BASENAME | tr "[:upper:]" "[:lower:]"`
FOUND=0;
function findwindow {
# 1 = BASENAME
# 2 = WMCTRL
# 3 = GREP
IFS=$'\n';
for RUNNING in `$2 -l -x`
do
if [ `echo $RUNNING | tr "[:upper:]" "[:lower:]" | $3 -c $1` -gt 0 ]
then
HOSTNAME=`hostname`
WINDOW=${RUNNING#*${HOSTNAME} }
$2 -a $WINDOW
FOUND=1;
fi;
done
}
findwindow $BASENAME $WMCTRL $GREP;
if [ $FOUND -eq 0 ]
then
$APPLICATION &
sleep 2;
# Attempt to find the window, in case it is on another desktop
findwindow $BASENAME $WMCTRL $GREP;
if [ $FOUND -eq 0 ]
then
# Still not found, sleep a bit, and try again
sleep 3;
findwindow $BASENAME $WMCTRL $GREP;
fi
fi

Make sure the script is executable

chmod +x find_app.sh

Now in your keys file, in place of calling the application directly, use the wrapper script above, for example the following line in the keys file

Control Mod1 f :ExecCommand ~/.fluxbox/scripts/find_app.sh /usr/bin/firefox

will try and start firefox, but if firefox is already running, then it will be set as the active, top-most application, and you will be moved to the desktop where it is running. If firefox is not running, it will be started as usual, and after a short delay, you will be moved to the desktop where it started up.

You can now use the keypress, not just to start an application, but to find an open instance.

I recently discovered a discussion about my script on an Archlinux forum (http://bbs.archlinux.org/viewtopic.php?pid=624950#p624950), from this post I was introduced to an alternative tool, called 'xdotool', which can do the job at hand as well.

Member 'hbekel' created an alternative script using that tool.

 

Last Updated on Tuesday, 29 September 2009 20:01
 
Share on facebook