What are you doing anyway the whole day staring at your screen?

Here is a handy script which logs the current timestamp and the title of the currently focused window in an SQLite3 database.

It does not insert a row if the current window title is the same than on the previous run.

You can run it by cron every minute, and will help you to find out where you spent most time 🙂

#!/bin/bash

# sqlite3 ~.windowlog.db
# CREATE TABLE windowlog(moment integer not null primary key, window);
# echo "select * from windowlog order by moment desc;" | sqlite3 ~.windowlog.db

: ${DISPLAY:=:0}
export DISPLAY

WIN_ID=`xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)"| cut -d ' ' -f 5`
WIN_NAME=`xwininfo -id $WIN_ID |awk 'BEGIN {FS="\""}/xwininfo: Window id/{print $2}' | sed 's/-[^-]*$//g'`
OLD_WIN_NAME=`echo "select window from windowlog order by moment desc limit 1;" | sqlite3 ~.windowlog.db`

if [ "$WIN_NAME" != "$OLD_WIN_NAME" ]; then
        #echo "$WIN_NAME ----- $OLD_WIN_NAME"
        timestamp=`date +%s`
        #echo `date +%s`,$WIN_NAME >> ~.windowlog
        sqlite3 ~.windowlog.db "insert into windowlog (moment,window) values ('$timestamp','$WIN_NAME');"
fi

Before running it you need to create the database and the table:

$ sqlite3 ~.windowlog.db
sqlite> CREATE TABLE windowlog(moment integer not null primary key, window);

Edit: This code is now living in github, and being greatly improved. Thanks to flazzarini!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s