Assigning keyboard shortcuts to applications
8th of October 2008I was surprised how annoying this was to do, all I wanted was to have some keyboard shortcut that focused a particular application, or started it if it wasnt already running. Ctrl+F1 would activate firefox, Ctrl+F2 would bring up emacs, etc etc.
I found a good little program called wmctrl that allows you to activate and query desktop / window information, The following is what I did in ubuntu, I dont imagine other distros would be much different.
Install wmctrl
sudo apt-get install wmctrl
Then I wrote a little helper script, this does a match against the title bar of each window and activates the first one that matches. If it doesnt match anything, attempts to run the command :
#!/bin/bash
# given a application name, focuses the first window
# with its name in the title, or start it
NAME=$(echo $1 | tr "[:upper:]" "[:lower:]")
ID=$(wmctrl -l -x | awk -v win="$NAME" 'tolower($0) ~ win {print $1; exit}')
if [ -z $ID ]; then
echo "Not Found"
else
wmctrl -i -a "$ID"
fi
Store this anywhere you want, I called it focus, you can then run /home/name/focus Navigator.Firefox to activate/focus firefox or emacs etc.
As for assigning the shortcuts, its pretty simple, I just ran gconf-editor browsed to apps->metacity then set the following values
| keybinding_commands | global_keybindings |
|---|---|
command_1 |
run_command_1 |
command_2 |
run_command_2 |
command_3 |
run_command_3 |
You can run wmctrl -l -x to view open windows. The third column gives you the classname of the window which is best to search on
If you have a better method / workaround, just post it in the comments.
Comments
dh
Oct 18, 08kostix
Oct 19, 08Post a Comment