Ubuntu – Keep calculator from opening a new window every time I press the “Calculator” button on the keyboard

11.10calculator

Currently what happens:

I press Calculator button on my keyboard, and calculator pops up. No problem there!
Next, after my calculation, I click back to the window where I'm inputting the calculation, and when I press the calculator button again, a NEW window opens in the calculator program.

What I want to happen:

When I click the calculator button, it switches back to the calculator program if it's already open, else, open calculator program. I don't want it to open a new window every time I try to change back to my recent calculation.

It's not a big deal, obviously, just a slight annoyance to me so far. Is there a setting, or some terminal command I can run, or anything I can do to change it?

Thanks!

Best Answer

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: SystemPreferencesKeyboard 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
Related Question