Tuesday, 31 May 2011

Query the Gnome screensaver using DBUS and Ruby

At my current work place, I need to send a mail every week with the amount of hours I have clocked in that week. Since I am a fairly lazy person, this can be somewhat problematic to remember (and results in annoying monthly mails from my manager). To help this, I have created a script to monitor my hours.

It is a simple script that works on the assumption that the first and last thing I do at work is something involving moving my mouse at the desk (the assumption is fairly correct). The script is a quick thing done in Ruby, with this essential part:


require 'dbus'

bus = DBus::SessionBus.instance
ss_serv = bus.service("org.gnome.ScreenSaver")
ss_obj = ss_serv.object("/org/gnome/ScreenSaver")
ss_obj.introspect
ss_obj.default_iface = "org.gnome.ScreenSaver"
while true:
  if not ss_obj.GetActive then
    on_work
  end
  sleep 10
end


This code basically polls the screensaver every 10 seconds and calls a function to say that I am currently at work. I have not included the on_work - function. This is also a fairly simple thing that uses a PStore to persistently store the first and last time the screen saver is not in use - code not included because of risk of embarrassment. 

1 less angry manager and learning more Ruby in the progress, that is what I call a win-win situation!

No comments:

Post a Comment