Display the current date & time in the window's title

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?


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.

It's a simple shell script:


Requirements

  • xdotool

    sudo apt-get install xdotool
    
  • wmctrl

    sudo apt-get install wmctrl
    

Main part

wmctrl -r :ACTIVE: -N "$(awk -F' \\|\\|' '{print $1}' <<< $(xdotool getwindowfocus getwindowname)) || $(date "+%Y-%m-%d %H:%M:%S")"

The || is the separator between the window title an the date/time part. If you need another one, change it, eg:

#

wmctrl -r :ACTIVE: -N "$(awk -F' #' '{print $1}' <<< $(xdotool getwindowfocus getwindowname)) # $(date "+%Y-%m-%d %H:%M:%S")"

The script

#!/bin/bash
while true
do
    wmctrl -r :ACTIVE: -N "$(awk -F' \\|\\|' '{print $1}' <<< $(xdotool getwindowfocus getwindowname)) || $(date "+%Y-%m-%d %H:%M:%S")"
    sleep 5
done

Here's a script that utilizes bash, xprop, and wmctrl (does not come with ubuntu). This script can be added as an autostart entry or placed into .desktop file. Basic idea is to get current active window's id, store its current title, and use output of date command and title to form a new title. The if statement prevents infinite loop of appending date. You can get wmctrl with sudo apt-get install wmctrl . Otherwise this script is very simplistic but does the job well enough.

#!/bin/bash
# Date : June 9 2015
# Author: Serg Kolo
# Description: script to append time to window titles
while sleep 1; do
        ACTIVEID=$(xprop -root | awk '/^_NET_ACTIVE_WINDOW/ {gsub("0x","0x0");print $5}')
        MATCHED_TITLE="$( wmctrl -l | awk -v searchterm=$ACTIVEID   '$0~searchterm {for(i=4;i<=NF;i++) printf $i" "}')"
        date --date="$(echo $MATCHED_TITLE | awk '{print $1" "$2}')"  > /dev/null  2>&1

        if [ "$?" -eq 0 ]; then
                MATCHED_TITLE="$(echo $MATCHED_TITLE | awk '{for(i=3;i<=NF;i++) printf $i" "}')"
        fi      
        sleep 1
        wmctrl -i -r $ACTIVEID -T "$(date +"%D %H:%M" 2>/dev/null) $MATCHED_TITLE "

done