Focus an application instead of re-launching it

May 5, 2008

Do you often end up with a cluttered desktop where you have several instances of the same application running? You need quick access to a particular application (often a file manager or terminal in my case) and instead of looking for the current open instance you launch a new one. After a few hours, you notice you have 4 terminals running and three Thunar windows, where one instance of each would suffice. If you often end up in a similar situation, despair no more!

Vaughn Dickson posted a very handy script on the Openbox mailing list and his wiki that either launches an application or gives it focus if it is already running.

I have modified the script a little and created a second one for Thunar. In my version, it moves the open instance of Thunar or the terminal to the current desktop (rather than moving me to the desktop where that window currently is running). You can find my Thunar script here, and my Terminal script here. If you would like to open a new Terminal/Thunar window if there is none on the current desktop, instead of moving the existing window to the current desktop, replace the last line of the script with $terminal_exec & or $thunar_exec &. Note that you need to have wmctrl installed to use this script (it should be in the repositories of most distributions).

Make the script executable (chmod +x path to the script) and assign a keybinding to it. I bound these two scripts to the keys I normally use to launch the xfce4-terminal and Thunar (Mod4+F3 and Mod4+F4). It speeds everything up enormously, since I no longer have to wait for the application to launch (or go looking for it among my open windows).

Thank you very much, Vaughn! This is a great tool😀

7 Responses to “Focus an application instead of re-launching it”

  1. Mads said

    Hey, he stole my script!😀

  2. Navi said

    Would be more useful if you could make it modular. I.e change it so that it only needs one script that does every program, the variables can be set in a config file somewhere… This + wbar = poor man’s OSX knockoff. Then again, maybe I should just use simdock…

  3. Animesh said

    Navi’s comment got me thinkering with your/Vaughn’s scripts and with very little modifications this is what I came up with :
    ======================================================
    #!/bin/sh

    app_exec=$1
    app_wm_class=$2

    # no app started, so start one
    if [ -z “`wmctrl -lx | grep $app_wm_class`” ]; then
    $app_exec &
    else
    # search for existing apps on current desktop
    current_desk=`wmctrl -d | grep ‘*’ | cut -d ‘ ‘ -f 1`
    app_on_this_desk=`wmctrl -lx | grep “$current_desk[ ]*$app_exec” | cut -d ‘ ‘ -f 1`
    if [ -n “$app_on_this_desk” ]; then
    wmctrl -i -a $app_on_this_desk
    else
    # no apps on current desktop, so just open the first one
    wmctrl -x -a $app_wm_class
    fi;
    fi;
    ======================================================
    I called it : app_focus.sh

    for example you can launch/ focus

    gnome-terminal with :
    —————————————————–
    app_focus.sh gnome-terminal Gnome-terminal
    —————————————————–

    nautilus with :
    —————————————————
    app_focus.sh “nautilus –no-desktop” Nautilus
    —————————————————

    And since I use Arch linux, Gran Paradiso i.e. some form of “free firefox” with :
    ——————————————————
    app_focus.sh firefox “”Gran Paradiso””
    ——————————————————

    And like Navi said I assigned them to wbar …….🙂

    BUT it is not a taskbar replacement😦 applications not in the bar initially would still need a proper taskbar that is only if you use one🙂🙂

    wmctrl + devilspie is a great combination in my opinion, hey I even made xterm into a drop down terminal like tilda using those two with animation even🙂

  4. Animesh said

    I forgot to mention; I keep my scripts in
    —————————————————-
    /home/username/bin/
    —————————————————-

    so I don’t need to mention full path for my scripts🙂

  5. Ikem said

    The script from “Animesh” spill out some errors. Has to do with the type of quotation marks he used. I fixed it. And I formatted the code for better readability. ^^

    http://pastebin.org/391309

  6. […] a very long time I’ve used a script that either raises a running application or, if the application has not been launched yet, launches […]

  7. […] – create a focusing shortcut Focus an application instead of re-launching it […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s