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
The pre-installed (GNOME) Calculator application on Ubuntu 18.04 is a snap application, it doesn't have .desktop
launcher(s) for the application at traditional place(s), e.g. /usr/share/applications/
. Check the /var/lib/snapd/desktop/applications/
directory instead.
Alternatively, you may consider creating a custom .desktop
launcher for the application by hand. To do so, follow the steps below.
Create a new file, say calculator.desktop
in your ~/.local/share/applications/
directory. You can do that by running the following in Terminal
touch ~/.local/share/applications/calculator.desktop
Open this file with a text-editor, for example by running
gedit ~/.local/share/applications/calculator.desktop
Add the following lines to this file and save the file.
[Desktop Entry]
Name=Calculator
Comment=Perform arithmetic, scientific or financial calculations
Exec=/snap/bin/gnome-calculator
Type=Application
Icon=accessories-calculator
Terminal=false
NoDisplay=false
You may also remove the snap version by running
snap remove gnome-calculator
and install the traditional apt versions by running
sudo apt install gnome-calculator
Best Answer
Open a new terminal, then execute the following command
gnome-calculator