home
lars.st0ne.at
Linux And Related Stuff
[BLOG] |ARCHIVE| |TAG MAP| |OTHER STUFF| |ME|
  • switch between windows within the same application


    #bash #ewmh #openbox #wmctrl #xprop

    I use the bash script below, on my Openbox and old Ubuntu ( Gnome 2.30.2 ) desktop, to switch between windows in the same "window class" ( =same app ).

    In newer Ubuntu versions ( post unity ), you can switch between windows in the same application by using the shortcut TAB-^

    On my Openbox desktop, the script is triggered by the keyboard shortcut SUPER+TAB, which is configured in the file "rc.xml".

    / prerequisites:
    The following packages have to be installed:
    - wmctrl
    - xprop

    Important: The script only works with a EWMH compliant window manager.


    rc.xml:

    ...
    <keyboard>
    ...
      <keybind key="W-Tab">
        <action name="execute">
          <execute>~/bin/switch_window_by_app.sh</execute>
        </action>
      </keybind>
    ...
    </keyboard>
    ...
    

    ... either specify the full path to the script, or put the script somewhere within the $PATH variable.


    switch_window_by_app.sh:

    #!/bin/bash
    
    # get id of the focused window
    active_win_id=$(xprop -root | grep '^_NET_ACTIVE_W' | awk -F'# 0x' '{print $2}')
    if [ "$active_win_id" = "0" ]; then
        active_win_id=""
    fi
    
    # get window manager class of current window
    win_class=$(wmctrl -x -l | grep $active_win_id | awk '{print $3}' )
    
    # get list of all windows matching with the class above
    win_list=$(wmctrl -x -l | grep  $win_class | awk '{print $1}' )
    
    # get next window to focus on
    switch_to=$(echo $win_list | sed s/.*$active_win_id// | awk '{print $1}')
    
    # if the current window is the last in the list ... take the first one
    if [ "$switch_to" = '' ];then
    switch_to=$(echo $win_list | awk '{print $1}')
    fi
    
    
    # switch to window
    wmctrl -i -a $switch_to
    
    Aug 02 2012 15:47
    by st0ne
    ? hits
    • comment @Twitter
    • share on Twitter
©2012 Robert Steininger aka st0ne
CC-BY-SA