#!/usr/bin/python3

import pyatspi
import time

def listener(e):
    if e.host_application.name != "gnome-shell" or not e.detail1:
        return

    labelledBy = None
    relations = e.source.getRelationSet()
    for relation in relations:
        if relation.getRelationType() == pyatspi.RELATION_LABELLED_BY:
            labelledBy = relation.getTarget(0)
            break

    outfile = open('/tmp/pyatspi-listener.out', 'a')
    outfile.writelines("%s - %s label: %s\n" % (time.strftime("%D %H:%M:%S"), e.source, labelledBy))
    outfile.close()

pyatspi.Registry.registerEventListener(listener, "object:state-changed:focused")
pyatspi.Registry.start()
