I do most of my work in two applications: my web browser and my editor. I switch back and forth between them frequently with Alt-Tab. I also have an IM client (Hipchat) open at all times, but I interact with it only rarely compared to the other two apps.
A recurring annoyance is that after I interact with the Hipchat window and Alt-Tab back to (say) my editor, my muscle memory is tuned to expect another Alt-Tab to focus on my browser, but I end up on Hipchat again.
Is there any way to cause Hipchat to be sent to the bottom of the stack or recency list or whatever it is, after it loses focus by any means?
What you ask is actually to allow a specific application's window to only appear either on first- or last position, z-wise.
When the gedit window (in this example) loses focus, it is sent to the last poition (z-wise, below the semi-transparent terminal window) instead of descending only one position:
Z- position of a window
While it can be done, we still have to overcome some serious complications; When the window is sent to the very last position, you will want to keep the z-order of all other windows. However, currently, there are no tools that can give us this z-order of windows. Both
xdotool
andwmctrl
give us no information on this whatsoever.What we can do however is to keep track of the focus history of (all) windows. Since a window descends one position if another window gets focus, we can conclude the z-order of windows if we run a background script to watch the focus history of windows.
The solution two small background scripts
The solution below exists of two small background scripts, to run simultaneously.
focus_history.py
set_z.py
Script 1
focus-history.py
Script 2
How to set up
The script uses both
wmctrl
andxdotool
Copy script 1 into an empty file, save it (exactly!) as
focus_history.py
Copy script 2 into an empty file, save it as
set_z.py
in the exact same directory as script 1.In the head section of the script, in the line:
replace
"gedit"
by the process name of your application (between quotes)Test- run the script: Before opening any (additional) windows, start script 1 by the command:
[The script will recognize windows that were focussed at least once. That will be the case if the script is run on log in]
As mentioned, the scripts should be in one and the same directory, on the same level.
Now start opening windows and see how it behaves. Your application should move to the (very) background if it loses focus.
If all works fine, add it to Startup Applications: Dash > Startup Applications > Add. Add the command:
Notes
Alternatively
Alternatively, you could set a shortcut key to raise a specific applications's window if it exists, as explained here.
That would require however to have another shortcut to go back to the first application's window,
Unless...,
You would setup one shortcut to toggle between two applications. That would however be out of the scope of this question...