9

Being a compulsive computer user, I am in front of it all day. I sometimes lose track of time when I am working on my comp. I need a notification service to alert me of the current time, either by a popup notification or a sound being played or both.

For the popup, I found the Free Desktop notification standard which uses a DBus API.

I was able to create a notification using DFeet, a graphical DBUS explorer. I used the following arguments:

"wakeup", 1234, "", "The time is", "9PM", [], [], 1

It works fine so far, but how can I take it further from here?

  • How do I invoke this from the command-line?
  • How do I automate this command? Is cron still the recommended way of automating time based actions?
  • How do I play sounds along with the popup? Either via the FreeDesktop API or via a media player?

A complete solution would be appreciated and perhaps useful to others too.

3
  • 1
    Regarding points 1, 2, dbus-send could be the app to use, and yes cron is the recommended way.
    – enzotib
    Aug 16, 2011 at 16:14
  • dbus-send is not working because it doesn't support some parameters required by this API. I am looking into pynotify.
    – HRJ
    Aug 16, 2011 at 20:09
  • +1 for needing you computer to notify you about current time :) (but seriously - that's a good question) Aug 16, 2011 at 20:10

3 Answers 3

7

Since I couldn't use dbus-send I wrote a python script instead. The pynotify module internally uses the dbus API. For extra kicks I added a fortune cookie in the message. Works like a charm:

#!/usr/bin/env python
"""python 2.7 script that creates a notification using pynotify. It shows the current time and a small fortune cookie"""
try:
  import pynotify
  import time
  import subprocess
  if pynotify.init("Wakeup service"):
    subprocess.Popen(["paplay", "/usr/share/sounds/ubuntu/stereo/message.ogg"])

    # You can get more stock icons from here: http://stackoverflow.com/questions/3894763/what-icons-are-available-to-use-when-displaying-a-notification-with-libnotify
    timeStr = time.strftime("%I:%M %p %d %b")
    cookie = subprocess.check_output(["/usr/games/fortune", "-s"])
    n = pynotify.Notification(timeStr, cookie, "/usr/share/app-install/icons/ktimer.png")
    n.set_timeout(1)
    n.show()
  else:
    print "problem initializing the pynotify module"
except Exception as exc:
  print "Exception", exc

I then scheduled this using cron. The crontab entry looks like:

0,30 * * * * DISPLAY=:0 ./local/bin/notify_new.py

Update:Added a method to play a sound using pulse audio

4

You can use a simple Python script, like this:

#!/usr/bin/python
import dbus
import sys

bus = dbus.SessionBus()

notify = bus.get_object('org.freedesktop.Notifications', '/org/freedesktop/Notifications')
method = notify.get_dbus_method('Notify', 'org.freedesktop.Notifications')

method("wakeup", 1234, "", "The time is", "9PM", [], [], 1)
3

You can send messages using the dbus-send command. See man:dbus-send for more details about that.

1
  • 3
    Thanks for the pointer to dbus-send. Unfortunately, it doesn't work in this case because dbus-send can't create a dictionary with variants which the API requires
    – HRJ
    Aug 16, 2011 at 19:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .