LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General > LinuxQuestions.org Member Success Stories
User Name
Password
LinuxQuestions.org 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.

Notices


Reply
  Search this Thread
Old 04-10-2015, 12:24 AM   #1
eureka_man
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:

Code:
#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)")
    else:
        system.exec_command("wmctrl -x -a nautilus.Nautilus")
        
else:
    system.exec_command("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
 
  


Reply



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 10:23 PM
Toggle between Linux & windows ppkamma Linux - Newbie 4 05-31-2006 02:23 PM
linux hotkey ust Linux - Software 3 11-05-2004 05:03 AM
Hotkey for application exit? xanas3712 Linux - Newbie 1 10-16-2004 09:16 PM
x server kill application keyboard shortcut/hotkey hypermegachi Linux - Newbie 2 07-25-2004 02:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General > LinuxQuestions.org Member Success Stories

All times are GMT -5. The time now is 09:35 AM.

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