The tiny script below will do what you want. You need to install wmctrl
for it to work:
sudo apt-get install wmctrl
After you've installed wmctrl
, paste the text below into a file and call it whatever you want. I called mine calc-activate.sh.
#!/bin/bash
#This script switches focus to gnome-calculator if it is running.
#If it is not running, it will run gnome-calculator.
if [ "$(wmctrl -l | grep Calculator)" != "" ]; then
wmctrl -a "Calculator"
else
gnome-calculator &
fi
After saving, the text file, make it executable either by right clicking and going to the permissions tab and checking off the Execute boxes, or do it from the terminal with the chmod
command).
Now you can set your multimedia keys:
- For Ubuntu 11.10 and earlier, go to: System → Preferences → Keyboard Shortcuts
- For 12.04 and later, hit the Super key, type "shortcut" then hit Enter.
- Now find the "Launch calculator" shortcut, click on it and hit backspace to disable it.
- Now you can add a new custom shortcut. Point the command to the full path of the script you just saved. Click on the new command and press the calculator button to assign it. Voila!
Please note: Since wmctrl
looks for a program titled "Calculator" the above script snippet might not work with other languages. To fix, change Calculator so it matches the window title of gnome-calculator
.
Danish example:
if [ "$(wmctrl -l | grep Lommeregner)" != "" ]; then
wmctrl -a "Lommeregner"
else
gnome-calculator &
fi