1

I created a keyboard shortcut, Ctrl+Alt+C, for gnome-calculator. This works fine, but if another application is currently running, the Calculator gets opened behind the other application.

How to make calculator active upon start with this shortcut?

  • @JacobVlijm that's a job for you :) – Sergiy Kolodyazhnyy Sep 2 '15 at 5:26
  • @Serg Aha, great, working on it :) – Jacob Vlijm Sep 2 '15 at 5:42
1

Running the script below will startup gnome-calculator (or any other application, see "Notes"), wait for its window to appear and raise: it.

The script

#!/usr/bin/env python3
import subprocess
import time

app = "gnome-calculator"
subprocess.Popen([app])

t = 0
while t < 20:
    try:
        # wait for the application's pid
        pid = subprocess.check_output(["pgrep", "-f", app]).decode("utf-8").strip()
    except subprocess.CalledProcessError:
        pass
    else:
        try:
            # read the window list
            w_data = subprocess.check_output(["wmctrl", "-lp"]).decode("utf-8").splitlines()
            # find the window of the found pid
            window = [w.split()[0] for w in w_data if pid in w][0]
            # raise it and terminate the script
            subprocess.Popen(["wmctrl", "-ia", window])
            break
        except IndexError:
            pass
    time.sleep(0.5)
    t = t + 1

How to use

  1. The script needs wmctrl to read the window list and raise the window:

    sudo apt-get install wmctrl
    
  2. Copy the script into an empty file, save it as raise_window.py
  3. Test- run it by the command:

    python3 /path/to/raise_window.py
    
  4. If all works fine, add the command to a keyboard shortcut: choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command:

    python3 /path/to/raise_window.py
    

Notes

  • To prevent an endless loop (e.g. in case the application doesn't run for some reason), the script will wait for ten seconds, then it terminates anyway. If you want to increase the time, change the value in the line:

    while t < 20
    

    (20 = 20 * 0.5 seconds)

  • As mentioned, you can use the script to "fix" the behaviour of any application that does not raise by itself, by changing the line:

    app = "gnome-calculator"
    

    to run another application (app = "<application>"), or:

    alternatively, to use the script for various applications:

    replace the line:

    app = "gnome-calculator"
    

    by:

    app = sys.argv[1]
    

    and add in the head section of the script (directly below the shebang):

    import sys
    

    Then run the script by the command:

    python3 raise_window.py <application>
    

Explanation

To raise a window, you can use the command:

wmctrl -ia <window_id> 

or

wmctrl -a <window_name>

(assuming wmctrl is installed)

Since the window name is possibly not unique, The id is preferable to use. The trick is then to wait for the pid of the application to appear, testing with the command:

pgrep -f <application>

If the command has an output, it means the application has a pid and runs. The next thing is to see if the application has a window (already), by using the command:

wmctrl -lp

which will output a list of windows, their window-id and the corresponding (application's) pid.

If there is a match of the found pid and the windows in the list, we have our window that we need to raise. We can parse the window id from the window's line in the list, which we will subsequently use to raise the window with the command:

wmctrl -ia <window_id>
  • Hi Jacob, it worked successfully. Sorry for late reply as I did not see this till today. Thanks a lot – Ramana Reddy Sep 7 '15 at 5:53
  • @RamanaReddy Perfect, glad it works :) – Jacob Vlijm Sep 7 '15 at 6:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.