Keeping track of time…

People who follow me on Twitter will be used to seeing messages of the general form:

and people who I work with are used to seeing my desktop with the time-spent diagnostic at the bottom…

Screen Shot 2013-12-02 at 20.08.05

…and I’m semi-regularly asked a) who cares? (which is not the subject of this post) and b) how do I track this?

Let’s talk about how I track it…

Many people swear by RescueTime for this sort of tracking – but I much prefer to get my hands dirty (plus it lets me match some things together that wouldn’t otherwise be possible).

So I have a couple of little bits. First of all I have a tiny bit of AppleScript (I’m mac-user) that looks like this:

tell application "System Events" to tell (process 1 where frontmost is true)
 set o to name
 try
 set o to o & ":" & name of window 1
 end try
 o
end tell

(I’m largely clueless about AppleScript, this script is the result of questions I asked here and here – (I do like StackExchange!))

When run from the terminal, this produces output of the form:

Google Chrome:Add New Post ‹ Joe Reddington — WordPress

(Yes, that is what I’m doing right now…)

I’ve then got a bit of java code that calls this every minute, adds a timestamp and writes to file (java is a poor choice for this, the reason it’s java is because much of the other tracking code I have is in java and this was orginally an offshoot of that)

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class ActiveWindowCapture extends CaptureParent {

int minsBetweenCaptures = 1;
String outputFilenameOrPath = "../output/results";

public static void main(String args[]) {
CaptureParent temp = new ActiveWindowCapture();
temp.runForever();
}

public void runForever() {
while (true) {
String response = call("osascript ../activewindow");
System.out.println(response);
try {
toFile(outputFilenameOrPath, getDateTime() + response);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
TimeUnit.MINUTES.sleep(minsBetweenCaptures);
} catch (Exception e) {
e.printStackTrace();
}
}
}

private static void toFile(String filename, String text) throws IOException {
FileWriter fstream = new FileWriter(filename + ".txt", true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(text);
out.close();
}

}

This java thingy produces a file that looks like this:

2013-12-02_20:08:36Google Chrome:putting code in wordpress - Google Search
2013-12-02_20:08:36Google Chrome:putting code in wordpress - Google Search
2013-12-02_20:09:18Google Chrome:Writing Code in Your Posts « WordPress Codex
2013-12-02_20:09:33Google Chrome:wordpress java code with syntax highlighting - Google Search
2013-12-02_20:09:37Google Chrome:wordpress java code with syntax highlighting - Google Search
2013-12-02_20:09:37Google Chrome:wordpress java code with syntax highlighting - Google Search
2013-12-02_20:10:19Google Chrome:Add New Post ‹ Joe Reddington — WordPress
2013-12-02_20:10:35Google Chrome:Add New Post ‹ Joe Reddington — WordPress
2013-12-02_20:10:38Google Chrome:Add New Post ‹ Joe Reddington — WordPress
2013-12-02_20:10:38Google Chrome:Add New Post ‹ Joe Reddington — WordPress
2013-12-02_20:11:20Google Chrome:Add New Post ‹ Joe Reddington — WordPress
2013-12-02_20:11:36Google Chrome:Add New Post ‹ Joe Reddington — WordPress

But tracking is only half the problem, now we have to talk about retrieving the data.

For this I use python (yes, this is our third different programming langauge, at some point this will become a consistent thing)

The relevent python code looks like:

#!/usr/bin/python
from __future__ import division
import datetime
def processFile(filename, datestring):
file = open(filename)
line = file.readline()
countTwitter=0
countGmail=0
countFacebook=0
countAll=0
countYouTube=0
countTropes=0
countVLC=0
while line:
if datestring in line:
countAll=countAll+1
if 'ouTube' in line:
countYouTube=countYouTube+1
if 'VLC:' in line:
countVLC=countVLC+1
if ' - Messages' in line:
countFacebook=countFacebook+1
if 'acebook' in line:
countFacebook=countFacebook+1
if 'ustUnfo' in line:
countTwitter=countTwitter+1
if 'witter' in line:
countTwitter=countTwitter+1
if 'Google Chrome:Inbox -' in line:
countGmail=countGmail+1
if 'Mail:' in line:
countGmail=countGmail+1

line=file.readline()
print "{:4d} ".format(countAll), "{:4d} ({:4.1f}%)".format(countTwitter,float(countTwitter )/countAll * 100), "{:4d} ({:4.1f}%)".format(countFacebook,float(countFacebook )/countAll * 100), "{:4d} ({:4.1f}%)".format(countVLC,float(countVLC )/countAll * 100), "{:4d} ({:4.1f}%)".format(countYouTube,float(countYouTube )/countAll * 100), "{:4d} ({:4.1f}%)".format(countGmail,float(countGmail )/countAll * 100)

import datetime
import time
start_date = datetime.date.today()

day_count=9
print "Total, Twiter, Facebook, VLC, YouTube Inbox"
for single_date in (start_date - datetime.timedelta(days=n) for n in range(day_count)):

processFile("results.txt",time.strftime("%Y-%m-%d", single_date.timetuple()))

..and the output of this python script is embedded into my desktop (and make slightly blue) by geektool.

Hence we have:

Screen Shot 2013-12-02 at 20.08.05

 

 

It's been running for months now and I find it astonishingly useful in terms of reminding me what it is I'm spending my time on - and I'd like to thank all the people at stackoverflow who patiently answered my questions while I set it up.

 

Leave a Reply