Friday, 29 November 2019

linux - Custom hotkey/shortcut to open/bring to front an app


I don't imagine this is built into the system, but is it possible to do it without too much hassle?


Say I open a specific program with a hotkey, and when I press that hotkey again, the program window is brought to the front.


I want to do this on Ubuntu 9.04.


Maybe with D-Bus? Any experts?


Update: Here's what I ended up with in case it's of help to somebody:


#!/bin/bash
if [ -f "/tmp/myterm.pid" ]; then
WID=`cat /tmp/myterm.pid`
xdotool windowactivate $WID
if [ "$?" != "0" ]; then
WID=""
fi
else
WID=`xdotool search --title "UNIQUE TITLE" | head -1`
fi

if [ "$WID" == "" ]; then
/usr/bin/gnome-terminal --window-with-profile=MYPROFILE "$@"
WID=`xdotool search --title "UNIQUE TITLE" | head -1`
echo $WID > /tmp/myterm.pid
else
xdotool windowactivate $WID
fi

Surely it can be simplified, but I'm no bash wiz. Also, for my example to work, I created a custom profile in Terminal that applies a unique title to the window so it can be found later. The possibilities are endless!



Answer



The wmctrl program is just what you're looking for (sudo apt-get install wmctrl). You can use the wmctrl -a "AppTitle" command to bring the app to the front. wmctrl -l will list all available windows, so it should be easy to write a shell script that checks if your program is running and either launches it or brings it to the front. Then you can just bind that to a keyboard shortcut.


First save the following script somewhere, I'll use /home/jtb/code/bringToFront. It takes two arguments, the first is what you would type at the terminal to launch the program, the second is a substring of the program window's title. If there is no constant unique string in the title then you'll need to do a bit more work to find the program's window.


#!/bin/bash
if [ `wmctrl -l | grep -c "$2"` != 0 ]
then
wmctrl -a "$2"
else
$1 &
fi



  1. With the script in your current directory, run chmod +x bringToFront to make the script executable. Then make sure it works; to launch/focus firefox you could run ./bringToFront firefox "Mozilla Firefox".




  2. Now we need to bind a shortcut key. Run gconf-editor and navigate the folder structure to the left to /apps/metacity/keybinding_commands.




  3. Double click on the first command with a blank value, probably command_1. Type the full path to the script and provide the two parameters, e.g. /home/jtb/code/bringToFront firefox Firefox.




  4. From the panel on the left, select global_keybindings, the next folder up. Find the run entry matching the command you just defined, probably run_command_1. Double click it and type the keyboard shortcut you want to use. Put the modifiers in angle brackets, e.g. F.




Now Control + Alt + F will bring your firefox window to the front, or launch it if it's not already running.


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...