Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - General > Member Success Stories
User Name
Password Member Success Stories Just spent four hours configuring your favorite program? Just figured out a Linux problem that has been stumping you for months?
Post your Linux Success Stories here.


  Search this Thread
Old 04-10-2015, 01:24 AM   #1
LQ Newbie
Registered: Apr 2015
Posts: 1

Rep: Reputation: Disabled
How to toggle ANY application in Linux with a hotkey!

I worked out an awesome little tip/trick and wanted to share it somewhere! It should work on a variety of linux distros, probably most common ones out there.

This started out with the GNOME extension Toggle Nautilus. Its Super+E toggle isn't working in Debian Jessie/GNOME 3.14, and in Debian's Wheezy I was used to this super quick and easy toggle for Nautilus rather than setting some custom hotkey in GNOME Keyboard preferences that merely starts a NEW Nautilus window (not bring up the existing one) and so accumulating 20 Nautilus windows in the process if I'm not careful.

I had already used AutoKey for other cool remappings (like mapping my macbook's power key to <delete>!), and had a bit of experience using xdotool for scripting up window navigation and the like, so with a bit of determination and 45 mins of googling and trial and error, little non-programmer me cracked it (ah the power of code)!!! A full toggler that opens a new window of the app if there isn't currently one, brings up the existing window if there is, and minimizes ('hides') that window if you're currently IN it - and it seems to work flawlessly.

So: apt-get install autokey-gtk xdotool wmctrl (or for its 'KDE version' the repo says autokey-qt, I haven't tried it in KDE yet, or otherwise in general build from source), and make a new 'Script' with this code and map it to whatever hotkey you want for it:

#AutoKey script to toggle any windowed application, Nautilus as the example. Requires xdotool and wmctrl.
import subprocess
command = 'wmctrl -lx'
output = system.exec_command(command, getOutput=True)

if 'nautilus.Nautilus' in output:
    winClass = window.get_active_class()
    if winClass == 'nautilus.Nautilus':
        system.exec_command("xdotool windowminimize $(xdotool getactivewindow)")
        system.exec_command("wmctrl -x -a nautilus.Nautilus")
#end script
That's basically it! If anyone has any improvements/ideas it'd be great but in my testing it works lightning fast and flawlessly so far! BTW, KDE/other desktops may have their OWN equally powerful way to 'toggle the window of a given application (KDE tends to blow me away in how feature-rich it is with window management!), but either way, I think this is one great way to do it in KDE, GNOME and I assume more! AutoKey rocks!

I successfully tested this with:
- Nautilus (I now map it to Super+E)
- gedit (I now map it to Super+G)
- Terminal (I now map it to Super+T)
- AutoKey itself
- Debian's Iceweasel
- Recoll (I now map it to Super+F!)

The only application I've tried that is only partially working (bringing it up works but the xdotool minimize command doesn't), is a java program. But I'll try to achieve it another way, I'm sure with a minor modification it can also minimize via the hotkey just fine.

And another detail: if the app has multiple windows, it seems to work fine by just opening the 'first' one without worrying about the others, and if you're currently in a second, third, fourth etc window of that given app (to toggle it 'off'), it either just minimizes that single window if that's all that was 'unminimized', or if others are still 'unminimized' it actually brings them up one by one after the other and minimizes them all as you go! Which to me is desirable behavior!

I suppose you could easily modify it to minimize ('hide') ALL of the given app's (wm_class) windows instead of just the current one, too.

long live coding!
Peace out


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hotkey to raise/lower specific application/window mike11 Linux - Newbie 6 09-05-2010 11:23 PM
Toggle between Linux & windows ppkamma Linux - Newbie 4 05-31-2006 03:23 PM
linux hotkey ust Linux - Software 3 11-05-2004 06:03 AM
Hotkey for application exit? xanas3712 Linux - Newbie 1 10-16-2004 10:16 PM
x server kill application keyboard shortcut/hotkey hypermegachi Linux - Newbie 2 07-25-2004 03:37 PM > Forums > Linux Forums > Linux - General > Member Success Stories

All times are GMT -5. The time now is 11:19 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration