Switch to or launch application

How to do things, solutions, recipes, tutorials
Post Reply
Message
Author
User avatar
Uten
Posts: 129
Joined: Tue 29 Jan 2008, 11:00

Switch to or launch application

#1 Post by Uten »

I have played with openbox (fatdog64-721) tonight.
Turns out there are lots of keybinding options that makes sense.

While playing I came across a question from someone that wanted an application to be brought to the foreground if it was running or launched if it was not. Apparently Ubuntus unity has this. Suggestions and solutions are to be found on net, but they seemed to fail quite often on fatdog. Anyway I ended up with this script which works for me. Also visit the RESOURCE links in the script if you want to know more about keybindings.

OBS: The script requires wmctrl witch you install from gslapt or compile yourself.

Save as xwinfocus.sh somewhere in PATH and chmod +x xwinfocus.sh

Code: Select all

#!/bin/bash
# Set focus to the first window containing the window title string
#      ( and for good measure has a reasonable size )
# If the window is not found we start the application/cmd
#
# REQUIRES: wmctrl (not included in fatdog64-721, install from gslapt)
# 
# USAGE: "xwinfocus.sh <window title string> <cmd to start>"
# 
#
#
# Another usage is by binding it to a key combination in openbox (or 
#      any window/keyboard manager)
# In this case I added these lines to: ~/.config/openbox/rc.xml
#
#    <!-- bind application to W-[0-9] W is the windows key-->
#    <!-- apply changes by: openbox --reconfigure         --> 
#    <keybind key="W-1">
#      <action name="Execute">
#        <command>xwinfocus.sh  "Mozilla Firefox" "firefox --private-window"</command>
#      </action>    
#     </keybind>
#    <keybind key="W-2">
#      <action name="Execute">
#        <command>sh -c 'xwinfocus.sh "- Geany" geany'</command>
#      </action>
#    </keybind>
#    <keybind key="W-3">
#      <action name="Execute">
#        <command>sh -c 'xwinfocus.sh Osmo osmo'</command>
#      </action>      
#    </keybind>
#    <keybind key="W-9">
#      <action name="Execute">
#        <command>xwinfocus.sh "mc \[" "urxvt -e mc"</command>
#      </action>      
#    </keybind>    
#    <keybind key="W-0">
#      <action name="Execute">
#        <command>xwinfocus.sh htop "xterm -fg yellow -bg black -title htop -e htop"</command>
#      </action>            
#    </keybind>          
# 
# RESOURCE: http://blog.johnnovak.net/2016/11/13/a-minimalist-openbox-desktop-for-vim-freaks/
# RESOURCE: http://melp.nl/2011/01/10-must-have-key-and-mouse-binding-configs-in-openbox/
# RESOURCE: 
export app_title="$1"
shift
app_exec="$*"
echo "[$app_title], [$app_exec]"

#wmctrl only lists windows managed (started by) the window manager?
#app_win_id=`wmctrl -lx|grep -i $app|cut -d ' ' -f 1`
# xwininfo has been more reliable on my system (fatdog64)
app_win_id=$(xwininfo -tree -root|awk '$0 ~ /[[:xdigit:]]{3,}x[[:xdigit:]]{3,}/&& ! /(has no name)/ && $0 ~ ENVIRON["app_title"] {$0=$0; print$1; exit}')

if [ -z $app_win_id ]; then
	echo "RUN: $app_exec"
    $app_exec & # app not started, so start it
else
	echo "PROCESS: $app_win_id"
    active_win_id=`wmctrl -r :ACTIVE: -e 0,-1,-1,-1,-1 -v 2>&1|grep U|cut -d ' ' -f 3`
    if [ $app_win_id == $active_win_id ]; then
        wmctrl -r :ACTIVE: -b toggle,hidden    # hide app when active
    else
        wmctrl -i -a $app_win_id    #switch to app
    fi;
fi;

Post Reply