1

Using Alt +Tab normally does what I want; it groups windows from the same application. If I want to switch between windows of one and the same application, I use Alt +`

However, if I have multiple terminal windows open (for example), and I use Alt +Tab to switch to another application, and then switch back to the terminal, another terminal window is in front than the last one.

How do I force it to always return to the same application window, like gnome 3 does?

Ubuntu 14.04 unity.

5
  • Is one of these options a solution to you? askubuntu.com/a/569481/72216 – Jacob Vlijm Jan 20 '15 at 18:07
  • No not really, unless I customise that script to do what I want ... in which case how would I know the last switched application that's not the current one in python? – texasflood Jan 21 '15 at 8:24
  • @JacobVlijm That's not quite what I want, it brings forward all the windows of an application, I just want to bring forwards the most recent window of an application – texasflood Jan 21 '15 at 10:43
  • @JacobVlijm I've tried the script in Option 2, it says File "./a.py", line 37, in <module> arg = apps[0] IndexError: list index out of range – texasflood Jan 21 '15 at 17:03
  • I saved it as a.py and ran it from the command line and from a keyboard shortcut (which did nothing). I may have made a mistake in implementing it I'm not sure. I am on 14.04 – texasflood Jan 21 '15 at 17:17
1

Below a script that switches through application windows. It remembers the last used (= frontmost) window of all running applications, bypassing other windows of the same application.

The script is based on the same principle as the scripts here, but since the script only runs when a key combination is pressed, the last used window (per application) needs to be stored and read outside the script(-'s memory).

How to use

  • The script uses wmctrl

      sudo apt-get install wmctrl
    
  • Copy the script below into an empty file, same it as alternative_switcher.py

  • Add it to a shortcut key combination: Choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command:

      python3 /path/to/alternative_switcher.py
    
  • Use it as an alternative switcher with your key combination.

Note

The script switches among "normal" application windows, as defined as: "_NET_WM_WINDOW_TYPE_NORMAL" in the output of the xprop -id <window_id> command. That means that dialogue windows will be excluded from the window list, as well as Idle windows for example, which have pid 0.

The script

#!/usr/bin/env python3
import subprocess
import os
import getpass

home = os.environ["HOME"]
lastdir = home+"/.config/alternative_switcher"; wlist = lastdir+"/"+"wlist.txt"

def get(command):
    return subprocess.check_output(["/bin/bash", "-c", command]).decode("utf-8")

def get_frontmost():
    cmd = "xprop -root"
    frontmost = [l for l in get(cmd).splitlines() if\
                 "ACTIVE_WINDOW(WINDOW)" in l][0].split()[-1]
    return frontmost[:2]+"0"+frontmost[2:]
# read last used windowlist
if not os.path.exists(lastdir):
    os.makedirs(lastdir)
try:
    with open(wlist) as src:
        windowlist = eval(src.read())
except (FileNotFoundError, SyntaxError):
    windowlist = []
# creating window list: id, application name
w_data = [l.split()[0:7] for l in get("wmctrl -lpG").splitlines()]
[windowlist.remove(w) for w in windowlist if not w[1] in [data[0] for data in w_data]]
windows = [[get("ps -u "+getpass.getuser()+" | grep "+w[2]).split()[-1], w[0]]
           for w in w_data if "_NET_WM_WINDOW_TYPE_NORMAL" in get("xprop -id "+w[0])]
# get frontmost window + application
frontmost = [data for data in windows if data[1] == get_frontmost()][0]
[windowlist.remove(item) for item in windowlist if item[0] == frontmost[0]]
# add frontmost to  windowlist of last-used application windows
windowlist.insert(0, frontmost)
current_app = frontmost[0]
# determine next application
apps = sorted(set([w[0] for w in windows]))
next_app_i = apps.index(current_app)+1
if next_app_i == len(apps):
    next_app_i = 0
next_app = apps[next_app_i]
# find matching window to raise
try:
    next_w = [w[1] for w in windowlist if w[0] == next_app][0]
except IndexError:
    next_w = [w[1] for w in windows  if w[0] == next_app][0]
# write last- window list
with open(wlist, "wt") as out:
    out.write(str(windowlist))
# raise next window
cmd = "wmctrl -ia "+next_w
subprocess.Popen(["/bin/bash", "-c", cmd])
3
  • Thanks! This is nearly correct... except it doesn't cycle through the most recently opened applications first like Alt + Tab does - though there is a problem with that in that it would forever cycle between two applications... unless more functionality was added so it stays up while you hold the alt tab for example. – texasflood Jan 21 '15 at 20:36
  • @texasflood It would be a bad idea to do in a script like this, since only switching between the last two applications would limit the functionality quite. With Alt+Tab it is no problem, since you have the visual overview and can escape from the cycle. I cannot change the functionality of Alt+Tab, only offer a nearby solution. – Jacob Vlijm Jan 21 '15 at 20:48
  • @texasflood fixed a small bug on resuming after closing windows. – Jacob Vlijm Jan 21 '15 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.