Categories
Technology

Fun with X – hacks in Ubuntu

I have always wanted to have more control over my GUI windows so that I can control them and monitor them via commands. So when I looked around for tools to help me do that I found a couple in Ubuntu that help me do some pretty nifty stuff.

So here are a couple of quick hacks:

  • Monitoring what you do on a daily basis:
    Productivity geeks will love this! This is a script which can help you know what the active window title is and print it into a file. Just cron this script to run once a minute and you will have a pretty good picture of what you do on a daily basis. Want to know how much time you spend on Facebook? Try this script!


    printf "`date "+%D %T"`: " >> active-window-log
    active=`xprop -root | grep -P "^_NET_ACTIVE_WINDOW" | awk '{print $NF}'`
    title=`xwininfo -id $active | grep -P "^xwin"` >> active-window-log

    Try this script for a day and you will be amazed by the insights you get by looking at the output log.

  • Monitoring a terminal for changes: How many times has it so happened that you had a terminal which was running a script and you were waiting for it to complete, but then you didn’t know how long it would take before it printed the next line of output? Wouldn’t it be great if you could monitor it?

    So here is the command that you can use to monitor changes to a specific terminal:

    window_id=`xwininfo | grep "Window id" | sed -e 's/.*Window id: //g' -e 's/ .*//g'`;
    xwd -id "$window_id" > /tmp/initial;
    while true;
    do xwd -id "$window_id" > /tmp/final;
    if [ -z "`diff /tmp/initial /tmp/final`" ];
    then echo "No diff";
    else echo "Windows differ";
    rm /tmp/final /tmp/initial;
    break;
    fi;
    sleep 3;
    done

    When you run this script, your mouse pointer changes to a cross-hair. Just point and click the terminal that you want to monitor and then allow it to continue with its work. As soon as there are changes, it will print “Windows differ”. Now you can as well make it send you a mail or start playing a song!

    The cool thing about this script is, it shows how flexible Unix tooling is. The way the script works is by taking a ‘screenshot’ of the terminal every 3 seconds and comparing it with the original screenshot. If there is a change, then diff outputs something and hence becomes non-zero.

The scripts have been tested in Ubuntu. If you don’t have any of the commands, Ubuntu should prompt you to apt-get install them. Although not tested, it should work in any X based system.