I decided to learn Python two days ago. Partly because I wanted a quick solution to a problem (and writing C always takes me so long), but also because I’ve read a lot of positive comments about it.
The problem
What I wanted was to get rid of the old gnome-osd stuff and beautify the visual output of some shortcut-scripts I use (more about that later). I found out I needed a script that can display notifications (using Ubuntu’s notify-osd) and provide that functionality for other scripts with D-Bus.
“D-Bus?”, you might ask. Well, I also wanted to be able to update notifications or append new body-text to them. This turned out to only be possible for a single script, but my shortcut-scripts (like “toggle-touchpad”) run only once and then exit. So I needed inter-process-communication, hence D-Bus.
The solution
Python is indeed great, because there are bindings for python-dbus and pynotify. Just what I needed to start hacking.
Download: Notification Server, 0.0.1
Usage
Here’s an example (toggle-touchpad), of how to use “send-notification.py”:
#!/bin/bash TEST=`synclient -l | grep "TouchpadOff = 0"` if [ "$TEST" != "" ]; then synclient TouchpadOff=1 send-notification.py --name="toggle-touchpad" \ --icon="input-mouse" "Input" "Touchpad disabled" else synclient TouchpadOff=0 send-notification.py --name="toggle-touchpad" \ --icon="input-mouse" "Input" "Touchpad enabled" fi
Both Python-scripts come with “--help
“, by the way.
#!/bin/bash
TEST=`synclient -l | grep “TouchpadOff = 0″`
I had to motdify the shellscript a bit to get it running. I had to tell the system to use python for the .py scrips.
thus i ended with this:
if [ “$TEST” != “” ]; then
synclient TouchpadOff=1
python send-notification.py –name=”toggle-touchpad” \
–icon=”input-mouse” “Input” “Touchpad disabled”
else
synclient TouchpadOff=0
python send-notification.py –name=”toggle-touchpad” \
–icon=”input-mouse” “Input” “Touchpad enabled”
fi
I had to modify the shellscript a bit to get it running. I had to tell the system to use python for the .py scrips. Thus i ended with this:
#!/bin/bash
TEST=`synclient -l | grep “TouchpadOff = 0″`
I had to motdify the shellscript a bit to get it running. I had to tell the system to use python for the .py scrips.
thus i ended with this:
if [ “$TEST” != “” ]; then
synclient TouchpadOff=1
python send-notification.py –name=”toggle-touchpad” \
–icon=”input-mouse” “Input” “Touchpad disabled”
else
synclient TouchpadOff=0
python send-notification.py –name=”toggle-touchpad” \
–icon=”input-mouse” “Input” “Touchpad enabled”
fi