Ubuntu – Display the current date & time in the window’s title

scriptswindowwindow-managerxdotool

Is it currently/technically possible to display the current date & time (ie: digital clock numbers) to the currently active window? Like appending some text from a script?

Best Answer

Showing date & time in the active window's title

Running the script below (an edited version of this one) in the background, the front most window will show the current date & time:

enter image description here

If the window loses focus, the time will not be updated; only on the front most window, the time will be updated.
The script also shows the date & time per tab on any application using tabs, like firefox or gnome-terminal

The script

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

cmd = "xdotool", "getwindowfocus"
get_name = "xdotool", "getactivewindow", "getwindowname"

currtime_1 = time.strftime("%d-%m-%Y  %H:%M"); wid_1 = subprocess.check_output(cmd).decode("utf-8").strip()
wname_1 = subprocess.check_output(get_name).decode("utf-8"); wname_1 = wname_1[:wname_1.rfind(" |  ")]

while True:
    time.sleep(2)
    currtime_2 = time.strftime("%d-%m-%Y  %H:%M")
    try:
        wid_2 = subprocess.check_output(cmd).decode("utf-8").strip()
        wname_2 = subprocess.check_output(get_name).decode("utf-8"); wname_2 = wname_2[:wname_2.rfind(" |  ")]
        if any([wid_2 != wid_1, currtime_2 != currtime_1, wname_2 != wname_1]):
            cmd2 = ["xdotool", "set_window", "--name", wname_2+" |  "+str(currtime_2), wid_2]
            subprocess.Popen(cmd2)
        currtime_1 = currtime_2; wid_1 = wid_2
    except subprocess.CalledProcessError:
        pass

How to use

  1. The script uses xdotool

    sudo apt-get install xdotool
    
  2. Copy the script below into an empty file, save it as show_datetime.py

  3. Test-run it by the command:

    python3 /path/to/show_datetime.py
    

    Open a new window or give an existing one focus. The date & time should appear in the window's title within 1-2 seconds. Wait a minute to see if the time is updated.

  4. If all works fine, add it to your startup applications: Dash > Startup Applications > Add, add the command:

    python3 /path/to/show_datetime.py
    
  5. If you are having difficulties running it from start up, use the command (in startup applications):

    /bin/bash -c "sleep 20&&python3 /path/to/show_datetime.py"
    

Explanation

The script keeps an eye on three things:

  • The frontmost window's id
  • The frontmost window's name
  • The current time (minute)

If there is a change in either one, the date/time is appended or updated to the currently frontmost window.

Why use the name -and- the id of the window?

  • The window- id is used to set the window's title, to prevent setting the wrong window (terminal windows e.g. can be named similarly).
  • The window- name is to include tabbed windows, e.g. Firefox. The window name will change when another tab is chosen or opened, while the window- id won't change.
  • The conditional if any([]) is to edit the window's title only if there is a reason to.