Desktop Command Execution Widget scripts

Desktop Command Execution widget is one of the most useful widgets on your Maemo desktop. It can be used to show certain information (for example battery level in percentage) or as a button which can be used for example to disconnect active internet connection (you need to tap 3 times and also wait for menus to appear without this widget). Therefore it can replace many other applications/widgets/applets and you can also make something new. Here you'll find a collection of scripts that can be added to the widget. The discussion about the widget is on the forum.

Scripts are also compatible with Queen BeeCon Widget which has extended graphic and cosmetic functionality, but is far more complicated.


[edit] Making your own scripts

[edit] Scripts without output

When there's no output (for example if you're using widget as a button and you use D-Bus call) the widget displays "Invalid Command". This can be most easily avoided if you pipe echo "" at the end of the command. This is also usable if your script produces unwanted output (D-Bus reply for example).

dbus-send -options -moreoptions | echo ""

Collection of D-Bus calls can be found on Phone control wiki page. The basic principle for making a script for DCEW is the same as above (D-Bus command and piping an echo).

[edit] Scripts with long output

Some scripts may create multiple lines which are too long to be displayed on a single line. The widget will not wrap these. In order to wrap them you can use the fold command:

command-that-produces-long-lines | fold -s -w 100

The 80 in that instance is the maximum length of the line, which you can change. The -s option makes fold word wrap with spaces. More information is available from the fold man page.

[edit] Scripts to display information

[edit] Battery (via Nokia's BME)

All battery scripts are collected here. Pick the one which suits your needs. Examples of the output values are under each one.

There are 2 values for full battery capacity available. First one is design charge in mAh, which is always the same (1273 mAh). The second one is the one used in these scripts and it is the full charge from last charging. With displaying this one you can also monitor battery wear level.

Battery percentage level is calculated using first value and is therefore less accurate, that's why you cannot achieve 100% full battery, but only about 95%. After some time the full percentage will be even lower.

But last full charge value has one disadvantage. This is that after a reboot the phone forgets this value and the value returned is 0. It shows the proper value after next charging.

[edit] Percentage, current and last full charge

hal-device bme | awk '/l.p/ {perc = $3}; /g.c/ {curr = $3}; / {last = $3}; /s_c/ {isch = $3} END if (isch == "false") {print perc" % ("curr"/"last" mAh)"} else {print "Charging"}'

Output example: 83 % (1000/1200 mAh), when charging Charging

[edit] Percentage and current charge

hal-device bme | awk '/l.p/ {perc = $3}; /g.c/ {curr = $3}; /s_c/ {isch = $3} END if (isch == "false") {print perc" % ("curr" mAh)"} else {print "Charging"}'

Output example: 83 % (1000 mAh), when charging Charging

[edit] Percentage

hal-device bme | awk '/l.p/ {perc = $3}; /s_c/ {isch = $3} END if (isch == "false") {print perc" %"} else {print "Chrg"}'

Output example: 83 %, when charging Chrg

[edit] Current and last full charge

hal-device bme | awk '/g.c/ {curr = $3}; / {last = $3} END {print curr"/"last" mAh"}'

Output example: 1000/1200 mAh

[edit] Current charge

hal-device bme | awk '/g.c/ {print $3" mAh"}'

Output example: 1000 mAh

[edit] Last full charge

hal-device bme | awk '/ {print $3" mAh"}'

Output example: 1200 mAh

[edit] Battery (via bq27200 chip)

[edit] Percentage, current and last full charge, with TTE or TTF as appropriate

Install this as e.g. /usr/local/bin/bqbattery, make it executable and point DCEW to it. This is designed to work whether or not you have the bq27x00_battery module loaded, but if you don't, then you'll need to install the i2c-tools package, and then chmod 4755 /usr/sbin/i2cget (meaning i2cget runs as root even when we invoke it as user from DCEW).


if [ -r "/sys/class/power_supply/bq27200-0/capacity" ]; then
# We have bq27x00_battery loaded - get the values from /sys/class/power_supply/bq27200-0/
CSOC=`cat /sys/class/power_supply/bq27200-0/capacity`;
NAC=$((`cat /sys/class/power_supply/bq27200-0/charge_now` / 1000));
LMD=$((`cat /sys/class/power_supply/bq27200-0/charge_full` / 1000));
STATUS=`cat /sys/class/power_supply/bq27200-0/status`;
if [ "$STATUS" == "Discharging" ]; then
       TTE=$((`cat /sys/class/power_supply/bq27200-0/time_to_empty_avg` / 60));
       TTF=$((`cat /sys/class/power_supply/bq27200-0/time_to_full_now` / 60));
# We don't have bq27x00_battery - get the values using i2cget
NAC=$(($(/usr/sbin/i2cget -y 2 0x55 0x0c w) * 3570 / 20 / 1000));
LMD=$(($(/usr/sbin/i2cget -y 2 0x55 0x12 w) * 3570 / 20 / 1000));
CSOC=$(($(/usr/sbin/i2cget -y 2 0x55 0x2c)));
TTF=$(($(/usr/sbin/i2cget -y 2 0x55 0x18 w)));
TTE=$(($(/usr/sbin/i2cget -y 2 0x55 0x16 w)));
echo "$CSOC% ($NAC/$LMD mAh)"
if [ "$TTF" -eq 65535 ]; then
        echo "$TTE minutes to empty"
        echo "$TTF min to full battery"

Output example:

88% (1269/1439 mAh)

37 min to full battery

[edit] IP

Internal IPs are obtained from the ifconfig and external IPs are obtained from the internet, because gprs0 IP which you can get with ifconfig is often from private address range, because mobile operators like to use NAT.

[edit] External (WAN) and internal (LAN)

echo WAN IP: `wget -t 2 -T 3 -q -O - | awk -F "\"" '{print $4}'`; echo LAN IP: `/sbin/ifconfig wlan0 | awk -F "[: ]" '/Bc/ {print $13}'`

Output example:



echo WAN IP: `wget -t 2 -T 3 -q -O - | awk -F "\"" '{print $4 " ("$12" "toupper($28)")"}'`; echo LAN IP: `/sbin/ifconfig wlan0 | awk -F "[: ]" '/Bc/ {print $13}'`

Output example:

WAN IP: (ISP CountryCode)


echo WAN IP: `wget -t 2 -T 3 -q -O - | awk -F "\"" '{print $4" ("$12" @ "$20", "toupper($28)")"}'`; echo LAN IP: `/sbin/ifconfig wlan0 | awk -F "[: ]" '/Bc/ {print $13}'`

Output example:

WAN IP: (ISP @ City, CountryCode)


[edit] External (WAN)

wget -t 2 -T 3 -q -O - | awk -F "\"" '{print $4}'

or another one, as sometimes is down

wget -t 2 -T 3 -q -O - | awk -F ": " '{print $2}' | awk -F "</" '{print $1}' 

Output example:

wget -t 2 -T 3 -q -O - | awk -F "\"" '{print $4 " ("$12" "toupper($28)")"}'

Output example: (ISP CountryCode)

wget -t 2 -T 3 -q -O - | awk -F "\"" '{print $4" ("$12" @ "$20", "toupper($28)")"}'

Output example: (ISP @ City, CountryCode)

[edit] Internal (LAN)

/sbin/ifconfig wlan0 | awk -F "[: ]" '/Bc/ {print $13}'

This one displays only wlan0 IP (used for SSH, WinSCP, VNC... in LAN).

[edit] Disk usage

[edit] rootfs (256MB /) percentage used

df | awk '$1 == "rootfs" {print $5}'

[edit] rootfs (256MB /) free space

df -h | awk '$1 == "rootfs" {print $4}'

[edit] Internal memory for user data (27GB /home/user/MyDocs) percentage used

df /home/user/MyDocs | awk '/My/ {print $5}'

[edit] Internal memory for user data (27GB /home/user/MyDocs) free space

df -h /home/user/MyDocs | awk '/My/ {print $4}'

[edit] Internal memory for application data (2GB /home) percentage used

df /home | awk '/ho/ {print $5}'

[edit] Internal memory for application data (2GB /home) free space

df -h /home | awk '/ho/ {print $4}'

[edit] Memory card (/media/mmc1) percentage used

df /media/mmc1 | awk '/mm/ {print $5}'

[edit] Memory card (/media/mmc1) free space

df -h /media/mmc1 | awk '/mm/ {print $4}'

[edit] Cellular signal

[edit] Quality

dbus-send --system --print-reply /com/nokia/phone/net Phone.Net.get_signal_strength | awk 'NR==2 {print $2" %"}'

[edit] Strength

dbus-send --system --print-reply /com/nokia/phone/net Phone.Net.get_signal_strength | awk 'NR==3 {print "-"$2" dBm"}'

[edit] Wi-Fi signal

[edit] Quality

awk -F "[. ]" '/0/ {print $6" %"}' /proc/net/wireless

[edit] RSSI

awk '/0/ {print $4" dBm"}' /proc/net/wireless

[edit] Noise

awk -F "[. ]" '/0/ {print $12" dBm"}' /proc/net/wireless

[edit] CPU frequency

awk '{print $1/1000" MHz"}' /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq

[edit] Usage of each frequency

awk -v var=$(awk '{sum+=$2}; END {print sum};' /sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state) '{arr[$3]=$2}{for (i in arr) {print $1/1000 " mhz " int(arr[i]*100/var)"%"}}' /sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state

The same, but without unused lines

awk -v var=$(awk '{sum+=$2}; END {print sum};' /sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state) '{arr[$3]=$2}{for (i in arr) {print $1/1000 " mhz " int(arr[i]*100/var)"%"}}' /sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state | grep -v " 0%"

[edit] Memory usage

[edit] RAM used

awk '/mT/ {memttl = $2}; /mF/ {memfre = $2}; /Bu/ {membff = $2}; $1 == "Cached:" {memcch = $2} END {printf ("%.1f MB\n",(memttl-memfre-membff-memcch)/1024)}' /proc/meminfo

[edit] RAM free

awk '/mF/ {memfre = $2}; /Bu/ {membff = $2}; $1 == "Cached:" {memcch = $2} END {printf ("%.1f MB\n",(memfre+membff+memcch)/1024)}' /proc/meminfo

[edit] Swap used

awk '/pT/ {swpttl = $2}; /pF/ {swpfre = $2} END {printf ("%.1f MB\n",(swpttl-swpfre)/1024)}' /proc/meminfo

[edit] Swap free

awk '/pF/ {printf ("%.1f MB\n",$2/1024)}' /proc/meminfo

[edit] Total memory used

awk '/mT/ {memttl = $2}; /mF/ {memfre = $2}; /Bu/ {membff = $2}; $1 == "Cached:" {memcch = $2}; /pT/ {swpttl = $2}; /pF/ {swpfre = $2} END {printf ("%.1f MB\n",(memttl+swpttl-memfre-membff-memcch-swpfre)/1024)}' /proc/meminfo

[edit] Total memory free

awk '/mF/ {memfre = $2}; /Bu/ {membff = $2}; $1 == "Cached:" {memcch = $2}; /pF/ {swpfre = $2} END {printf ("%.1f MB\n",(memfre+membff+memcch+swpfre)/1024)}' /proc/meminfo

[edit] GPRS data usage

You can use this with scheduled reset of the GPRS data counter to display data usage for current month. Additional info can be found on fcron wiki page.

These scripts are now compatibile with PR1.2 (separate home and roaming counter), which means they won't work on previous versions.

[edit] Home counter (combined)

echo `gconftool-2 -g /system/osso/connectivity/network_type/GPRS/gprs_home_rx_bytes` `gconftool-2 -g /system/osso/connectivity/network_type/GPRS/gprs_home_tx_bytes` | awk '{printf ("%.1f MB\n",($1+$2)/1048576)}'

[edit] Home counter (separated)

echo `gconftool-2 -g /system/osso/connectivity/network_type/GPRS/gprs_home_rx_bytes | awk '{printf ("Download: %.1f MB\n",$1/1048576)}'`; echo `gconftool-2 -g /system/osso/connectivity/network_type/GPRS/gprs_home_tx_bytes | awk '{printf ("Upload: %.1f MB\n",$1/1048576)}'`

[edit] Roaming counter (combined)

echo `gconftool-2 -g /system/osso/connectivity/network_type/GPRS/gprs_roaming_rx_bytes` `gconftool-2 -g /system/osso/connectivity/network_type/GPRS/gprs_roaming_tx_bytes` | awk '{printf ("%.1f MB\n",($1+$2)/1048576)}'

[edit] Roaming counter (separated)

echo `gconftool-2 -g /system/osso/connectivity/network_type/GPRS/gprs_roaming_rx_bytes | awk '{printf ("Download: %.1f MB\n",$1/1048576)}'`; echo `gconftool-2 -g /system/osso/connectivity/network_type/GPRS/gprs_roaming_tx_bytes | awk '{printf ("Upload: %.1f MB\n",$1/1048576)}'`

[edit] Time and date

[edit] Date

date +"%a, %-d.%-m.%Y"

This command will show the date in format (for example) Tue, 4.5.2010. You can define your own format (between the quotation marks). Possible options are described on manpage.

[edit] Time in different timezones

export TZ="Europe/London"; date +%R

This one shows two timezones in format London: 13:04 | Denver: 06:04

echo "London:" `export TZ="Europe/London"; date +%R` "| Denver:" `export TZ="America/Denver"; date +%R`

London and Denver are taken as an example. TZ values can be found on Wikipedia.

[edit] Uptime and load

These scripts are formatproof meaning that they display what they're supposed to no matter what format is command "uptime" outputting. Not all scripts found are like that, because "uptime" command is a little bit complicated for scripted text processing. For example when the system is running under one hour only "x min" is shown, when it is running under one day "hour:min:sec" is shown and after that it is shown in format "x days, hour:min:sec".

[edit] Both

uptime | sed -e 's/.*up */uptime: /' -e 's/ average//' -e 's/  / /'

[edit] Uptime

uptime | sed -e 's/.*p *//' -e 's/, l.*//' -e 's/  / /'

[edit] Load

uptime | sed 's/.*e: //'

[edit] Boot reason

cat /proc/bootreason

[edit] Boot count

cat /var/lib/dsme/boot_count

[edit] Temperature

CPU's thermal sensors are not accessible, but there is one near the battery. This commands displays output of its readings, but IT IS NOT RELIABLE, because it doesn't always work. Sometimes the value returned is wrong or constant. It needs to be tested further.

cat /sys/devices/platform/omap34xx_temp/temp1_input | awk '{ sub(/-/,""); print $1" °C"}'

There is a working way now to read the correct temperature, but it is working only on a newer titan's kernels (normal, overclock, undervoltage). The bq27x00_battery module has to be loaded first.

echo `cat /sys/class/power_supply/bq27200-0/temp` °C

with a comma :

echo `cat /sys/class/power_supply/bq27200-0/temp` °C | sed 's/\B[0-9]\{1\}/&,/'

[edit] Top processes

This script displays top N CPU consuming processes. It excludes top itself, which is quite processor intensive so you probably don't want this updating too often. Modify N=3 at the start to display different number of processes.

N=3; top -bn 1 | grep -v top | head -n $(($N+4)) | tail -n $(($N+1)) | awk '{OFS = "\t"} {print $7,$8}'

[edit] Random Number Generator

This script displays a random number between 0 and M (M included)

M=6; dd if=/dev/urandom bs=1 count=4 2>/dev/null | od -l | awk -v M=$M '{M++;print $2<0?-$2%M:$2%M;exit}'

[edit] Ping a Host

Preferred method. Install netcat from [1]

if (nc -zw1 80);then echo up;else echo down;fi

Alternate method. Ping has a long timeout when down. Warning: it will block the desktop while running.

if (echo 'ping -c1' | rootsh /bin/sh  2>&1 >/dev/null);then echo up;else echo down;fi

[edit] Scripts for buttons

Make sure that update policy for button widgets is set only to "update when clicked". "Update when switched to desktop", "update interval" and "network presence" should be disabled to avoid automatic actions. Also keep in mind that widgets are executed at every boot so they can for example automatically disable Wi-Fi when phone boots.

There is a bug in version 0.9 or lower which prevents you from using DCEW widgets as buttons, because they get activated (pressed) automatically all the time. You have to make sure DCEW is at least version 1.0, to check which version you have installed do this: dpkg -l desktop-cmd-exec.

[edit] Networking

[edit] Connect/disconnect

sh /path/ | echo ""

if [ `/sbin/route | awk '/au/ {print $1}'` = default ]; then
dbus-send --system /com/nokia/icd_ui boolean:true
dbus-send --system --type=method_call /com/nokia/icd string:"[ANY]" uint32:0

[edit] Connect internet (show connections)

dbus-send --system --type=method_call /com/nokia/icd_ui boolean:false | echo ""

[edit] Connect to any saved connection

dbus-send --system --type=method_call /com/nokia/icd string:"[ANY]" uint32:0 | echo ""

[edit] Disconnect internet

dbus-send --system /com/nokia/icd_ui boolean:true | echo ""

[edit] Enable/disable Wi-Fi

echo "/path/to/script/" | sudo gainroot | echo "" script:

out=`ifconfig wlan0`
if [ $? -eq "0" ] ; then
if [ `echo "$out" | grep -c RUNNING` -gt "0" ] ; then dbus-send --system /com/nokia/icd_ui boolean:true
ifconfig wlan0 down
rmmod wl12xx dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:'Wi-Fi disabled'
exit 2
modprobe wl12xx
stop wlancond
start wlancond
ifconfig wlan0 up dbus-send --system --type=method_call /com/nokia/icd_ui boolean:false
exit 0

Don't forget to make it executable.

[edit] Wake On Lan

/path/to/script/ | echo "" script;

#! /usr/bin/python
# Wake-On-LAN
# Change ip range in the "s.sendto(msg" line 
# and the MAC of the pc to wakeup in the bottom line
import struct, socket
def WakeOnLan(ethernet_address):
 # Construct a six-byte hardware address
 addr_byte = ethernet_address.split(':')
 hw_addr = struct.pack('BBBBBB', int(addr_byte[0], 16),
   int(addr_byte[1], 16),
   int(addr_byte[2], 16),
   int(addr_byte[3], 16),
   int(addr_byte[4], 16),
   int(addr_byte[5], 16))
 # Build the Wake-On-LAN "Magic Packet"...
 msg = '\xff' * 6 + hw_addr * 16
 # ...and send it to the broadcast address using UDP
 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
 s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
 s.sendto(msg, ('', 9))
# Example use

Don't forget to Chmod 755

[edit] Disconnect mobile network

dbus-send --system --type=method_call /com/nokia/phone/SSC boolean:false | echo ""

[edit] Connect mobile network

dbus-send --system --type=method_call /com/nokia/phone/SSC boolean:true | echo ""

[edit] Lock screen and keys

dbus-send --system --type=method_call /com/nokia/mce/request string:"locked" | echo ""

[edit] Radio mode

[edit] 2G/3G

sh /path/to/script/ | echo "" script:

if [ `dbus-send --system --type=method_call --print-reply /com/nokia/phone/net Phone.Net.get_selected_radio_access_technology | awk '/b/ {print $2}'` -eq 1 ]; then
dbus-send --system --type=method_call /com/nokia/phone/net Phone.Net.set_selected_radio_access_technology byte:2
dbus-send --system --type=method_call /com/nokia/phone/net Phone.Net.set_selected_radio_access_technology byte:1

When 3G or Dual mode is active, the script will switch to 2G. And when 2G is active, it will switch to 3G.

Don't forget to make the script executable.

[edit] 2G

dbus-send --system --type=method_call /com/nokia/phone/net Phone.Net.set_selected_radio_access_technology byte:1 | echo ""

[edit] 3G

dbus-send --system --type=method_call /com/nokia/phone/net Phone.Net.set_selected_radio_access_technology byte:2 | echo ""

[edit] Dual

dbus-send --system --type=method_call /com/nokia/phone/net Phone.Net.set_selected_radio_access_technology byte:0 | echo ""

[edit] Bluetooth

[edit] Enable

dbus-send --system --type=method_call --dest=org.bluez $(dbus-send --system --print-reply --dest=org.bluez / org.bluez.Manager.ListAdapters | awk -F "\"" '/at/ {print $2}') org.bluez.Adapter.SetProperty string:Powered variant:boolean:true | echo ""

[edit] Disable

dbus-send --system --type=method_call --dest=org.bluez $(dbus-send --system --print-reply --dest=org.bluez / org.bluez.Manager.ListAdapters | awk -F "\"" '/at/ {print $2}') org.bluez.Adapter.SetProperty string:Powered variant:boolean:false | echo ""

[edit] Profiles

[edit] General

dbus-send --type=method_call /com/nokia/profiled string:"general" | echo ""

[edit] Silent

dbus-send --type=method_call /com/nokia/profiled string:"silent" | echo""

[edit] Toggle vibrating alert

There are far more things you can do with your profile. Please have a look at the profile values at the phone control page. This example disables vibrating alert for the active profile.

# Which profile is active?
if \
dbus-send --print-reply --type=method_call \ /com/nokia/profiled \ | grep -q general
	# general profile is active
	dbus-send --type=method_call \ /com/nokia/profiled \ string:"general" \
	string:"vibrating.alert.enabled" string:"Off"
	# silent profile is active
	dbus-send --type=method_call \ /com/nokia/profiled \ string:"silent" \
	string:"vibrating.alert.enabled" string:"Off"

You get the opposite behaviour (enable vibrating alert) simply by replacing "Off" two times by "On".

[edit] Lock (secure) the device

dbus-send --system --type=method_call /com/nokia/system_ui/request string:"" string:"/com/nokia/mce/request" string:"" string:"devlock_callback" uint32:'3' | echo ""

[edit] Update e-mail

sh /path/to/script/ | echo "" script:

The script connects to the internet and refreshes e-mail. Keep in mind that Modest e-mail client which N900 uses is very slow in this aspect and send and recive can take up to minute and a half. Make it executable.

#!/bin/sh dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"Updating e-mail..." dbus-send --system --type=method_call /com/nokia/icd string:"[ANY]" uint32:0
sleep 10 dbus-send --type=method_call /com/nokia/modest

[edit] Set maximum CPU frequency

echo "echo 600000 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq" | sudo gainroot | echo ""

Replace 600000 with desired maximum frequency. This is usable with the new overclocking kernels if you wish to manually change the maximum frequency to which processor can scale. Pay attention to the two exceptions in titan's kernels (124999 and 599000). The list of available frequencies on your device/kernel can be obtained with command:

awk '{print $1/1000" MHz"}' /sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state

[edit] Reboot

echo "reboot" | sudo gainroot | echo ""

Warning: Consult forums before you try this, because currently DCEW executes some (all?) commands at startup. This will be optional in next version. Making a reboot button on current DCEW version could result in endless reboot loop.

[edit] A much safer way

write a file with the following content

 zenity --question --title="reboot?" --text="are you sure ?"
 if [ $? -eq 0 ]; then
 echo "reboot" | sudo gainroot | echo ""

save it to /bin/ask-reboot then execute :

 chmod +x ask-reboot

now add a new command widget with the command "ask-reboot" .

make sure you uncheck the folowing check boxes

  • update on boot
  • update when switched to desktop

[edit] FM transmitter

[edit] Enable/disable

/usr/bin/fmtx_client -p$(if [ $(cut -d. -f1 /proc/uptime ) -lt 100 ]; then echo 0; else /usr/bin/fmtx_client | /bin/grep -q '^state=enabled' ; echo $? ; fi) | /usr/bin/awk -F "=" '($1=="state") {print $2}'

Note: when you reboot the device, this script waits 100 seconds before you can turn the transmitter on/off again.

[edit] Increase power

echo "echo 118 > /sys/class/i2c-adapter/i2c-2/2-0063/power_level" | sudo gainroot | echo ""

[edit] DBUS call to start browser

/usr/bin/ load_url

[edit] Display a Conboy Note

python /home/user/ print noteid Make sure to make this python script executable (chmod 755 /path/to/ To list all notes with their title and id in xterm, just use the following commands: python /home/user/ list

Source :

Direct Script Link :


from xml.dom import minidom
import os.path
import glob
import sys
import re
from xml.sax.handler import ContentHandler
import xml.sax

class NoteList:
   def __init__(self,path):
       self.path = path
       self.noteList = {}
       for infile in glob.glob( os.path.join(self.path, '*.note') ):
         note = Reader(os.path.basename(infile)[:-5])
         if note.title != None:

   def get_note_id_by_title(self,title):
           return self.noteList[title]
           return None

class textHandler(ContentHandler):
   def __init__(self):
     self.content = ""
     self.title = ""
     self.selector = None

   def startElement(self, element,attributes):
       if (element == 'note-content') and (self.selector == None):
           self.selector = element
       elif (element == 'title') and (self.selector == None):
           self.selector = element

   def endElement(self, element):
       if (element == self.selector):
           self.selector = None

   def characters(self, ch):
       if self.selector == 'note-content':
           self.content = self.content + unicode(ch)
       elif self.selector == 'title':
           self.title = self.title + unicode(ch)

class Reader:
   def __init__(self,note_id):
       self.note_id = note_id
       self.content = None
       self.title = None

           parser = xml.sax.make_parser()
           handler = textHandler()
           self.content = handler.content
           self.title = handler.title 
       except StandardError,e:
           print e

if __name__ == "__main__":
       if (sys.argv[1]=='print'):
           print Reader(unicode(sys.argv[2])).content
       elif (sys.argv[1]=='list'):
           nlist = NoteList('/home/user/.conboy/').noteList
           for key in nlist.keys():
               print key, ' : ', nlist[key]
       elif (sys.argv[1]=='search'):
           nlist = NoteList('/home/user/.conboy/')
           print nlist.get_note_id_by_title(sys.argv[2])
   except StandardError,e:
           print """Usage : python option [note_id or note title]
           Option : 
               - list : list all note by title with id
               - print : print content of note with the given id
               - search : print id of the given title"""
           print e

[edit] Scripts to pull data from remote web sites

Although these are also scripts to display information, of a sort, I've created a separate section for scripts that use DCEW to replace the use of a mobile web browser in certain situations. My first take on this is a set of scripts / widgets to enable me to access Transport for London's live bus travel information.

[edit] Transport for London - Live Bus Departures

[edit] Display departures for multiple local bus stops

Transport for London recently launched a mobile Web interface to their live bus departures information, and it's pretty good, but it can be a pain navigating to the bus stop that you want. This script uses DCEW to display data from multiple local bus stops, and to switch between them (by iterating over a list of them) on touching the widget.


BUS_STOPS="50713 48598 53221 74407"

if [ -r "/tmp/.bus_stops" ]; then
	BUS_STOPS=`cat /tmp/.bus_stops`;
	echo $BUS_STOPS > /tmp/.bus_stops;
	chmod 777 /tmp/.bus_stops

if [ -r "/tmp/.countdown" ]; then
LOOP=`cat /tmp/.countdown`;
let LOOP=$LOOP+1;
STOPCOUNT=`echo $BUS_STOPS | wc -w`;
if [ "$LOOP" -gt "$STOPCOUNT" ]; then
echo $LOOP >/tmp/.countdown
chmod 777 /tmp/.countdown

THISTIME=`echo $BUS_STOPS | cut -d" " -f $LOOP`;

lynx -dump -nolist$THISTIME | grep -A10 "Buses dep" | sed -e "s/^   //g" | grep [a-z] | egrep -v "(Refresh|See\ map|Buses\ departing)"

[edit] Display departures for multiple lists of multiple bus stops

This second script, designed to be used with an additional DCEW on the same screen as the first, changes the list of bus stops associated with the script above and displays the place name with which they are associated. So, using this second widget, I can switch the first widget between the list of bus stops that I require in one place, and the list that I require in another. This limits the length of any individual list and will eventually let me access four or five lists of stops according to where I happen to be.


Kingston_Vale="50713 48598 53221 74407"
Roehampton="75656 74598 71874"

BUS_STOPS="Kingston_Vale Roehampton";

if [ -r "/tmp/.countdown2" ]; then
LOOP=`cat /tmp/.countdown2`;
let LOOP=$LOOP+1;
STOPCOUNT=`echo $BUS_STOPS | wc -w`;
if [ "$LOOP" -gt "$STOPCOUNT" ]; then
echo $LOOP >/tmp/.countdown2
chmod 777 /tmp/.countdown2

THISTIME=`echo $BUS_STOPS | cut -d" " -f $LOOP`;

echo $STOPLIST >/tmp/.bus_stops
chmod 777 /tmp/.bus_stops
echo $THISTIME | sed -e s/_/\ /g

[edit] Multiple lists of multiple stops with file-based configuration

This third script combines the functionality for both of the above widgets from a single script, to be installed as e.g. /usr/local/bin/countdown, and moves the bus stop configuration from variables to flat files, arranged by placename. It also adds some command-line functionality (not yet widgetised), which enables you to configure a given placename from the 5-digit code of a single bus stop.


case $1 in
	# This mode is used by the main DCEW to display information about a bus stop.
	echo "........................................................................................................................";

	if [ -r "/tmp/.bus_stops" ]; then
		BUS_STOPS=`cat /tmp/.bus_stops`;
		BUS_STOPS=`cat /home/user/countdown/places/Kingston_Vale`;
		echo $BUS_STOPS > /tmp/.bus_stops;
		chmod 777 /tmp/.bus_stops
	if [ -r "/tmp/.countdown" ]; then
		LOOP=`cat /tmp/.countdown`;
	let LOOP=$LOOP+1;
	STOPCOUNT=`echo $BUS_STOPS | wc -w`;
	if [ "$LOOP" -gt "$STOPCOUNT" ]; then
	THISTIME=`echo $BUS_STOPS | cut -d" " -f $LOOP`;
	if [ -r "/tmp/.countdownlock" ] && [ -r "/tmp/.laststop" ]; then
		THISTIME=`cat /tmp/.laststop`;
		LOOP=`cat /tmp/.countdown`;
	echo $LOOP >/tmp/.countdown
	chmod 777 /tmp/.countdown

	lynx -connect_timeout=10 -dump -nolist$THISTIME | grep -A10 "Buses dep" | sed -e "s/^   //g" | grep [a-z] | egrep -v "(Refresh|See\ map|Buses\ departing|Route)" | sed -e "s/Bus stop code //g" | fold -w 53
	echo "........................................................................................................................";
	echo "Stop $LOOP of $STOPCOUNT in `cat /tmp/.lastplace` ($THISTIME) $LOCKED";
	echo $THISTIME > /tmp/.laststop
	chmod 777 /tmp/.laststop

	# This mode can be used by a second DCEW to switch between lists of stops by place name
	if [ -r "/tmp/.countdownlock" ] && [ -r "/tmp/.laststop" ]; then
		rm -f /tmp/.countdownlock;
	BUS_STOPS=`ls /home/user/countdown/places`;
	if [ -r "/tmp/.countdown2" ]; then
	LOOP=`cat /tmp/.countdown2`;
	let LOOP=$LOOP+1;
	STOPCOUNT=`echo $BUS_STOPS | wc -w`;
	if [ "$LOOP" -gt "$STOPCOUNT" ]; then
	echo $LOOP >/tmp/.countdown2
	chmod 777 /tmp/.countdown2
	THISTIME=`echo $BUS_STOPS | cut -d" " -f $LOOP`;
	STOPLIST=`cat /home/user/countdown/places/$THISTIME`;
	echo $STOPLIST >/tmp/.bus_stops
	chmod 777 /tmp/.bus_stops
	echo 0 > /tmp/.countdown
	chmod 777 /tmp/.countdown
	echo $THISTIME | sed -e s/_/\ /g
	echo $THISTIME | sed -e s/_/\ /g > /tmp/.lastplace
	chmod 777 /tmp/.lastplace

	# This mode can be used by a third DCEW to lock the --display option to refresh only the current stop.
	# Touch it again to have the --display option return to paging through stops in the local area.
	if [ -r "/tmp/.countdownlock" ]; then
		rm -f /tmp/.countdownlock
		echo "next stop"
		touch /tmp/.countdownlock
		echo "refresh this stop"


	mkdir -p /home/user/countdown/places/
	FILE=`echo "$2" | sed -e "s/ /_/g"`;
	cat /tmp/.bus_stops > /home/user/countdown/places/$FILE
	chown -R user /home/user/countdown/places

	lynx -dump$2 | grep /arrivals/ | cut -d / -f 5 | while read stop; do out="$out $stop"; echo $out > /tmp/.out; done; 
	cat /tmp/.out > /tmp/.bus_stops

	if [ ! -z "$3" ]; then
		$0 --save "$3"

	countdown --near 50713 "Kingston Vale"
	countdown --near 75040 "Putney Bridge"
	countdown --near 76581 "Putney Station"
	countdown --near 48368 "Norbiton"
	countdown --near 47140 "Kingston Eden Street"
	countdown --near 50516 "Kingston Cromwell Rd"
	countdown --near 59403 "Kingston Hill"
	countdown --near 48680 "Roehampton"
	countdown --near 53574 "Roehampton Vale"
	countdown --near 57027 "Wandsworth"
	countdown --near 47613 "Earlsfield"
	chown -R user /home/user/countdown/places/*

	rm -rf /home/user/countdown/places/*

	sed -i -e "s/`cat /tmp/.laststop`//g" /home/user/countdown/places/`cat /tmp/.lastplace|sed -e "s/ /_/g"`
	cp /home/user/countdown/places/`cat /tmp/.lastplace|sed -e "s/ /_/g` /tmp/.bus_stops
	echo `cat /tmp/.laststop`

	$0 --near `cat /tmp/.laststop` "`cat /tmp/.lastplace`"
	cp /home/user/countdown/places/`cat /tmp/.lastplace|sed -e "s/ /_/g` /tmp/.bus_stops
	echo `cat /tmp/.laststop`

	rm -f /home/user/countdown/places/`cat /tmp/.lastplace`

	sed -i "s/  / /g" /home/user/countdown/places/`cat /tmp/.lastplace|sed -e "s/ /_/g"`
	leafpad /home/user/countdown/places/`cat /tmp/.lastplace|sed -e "s/ /_/g"`
	cp /home/user/countdown/places/`cat /tmp/.lastplace|sed -e "s/ /_/g"` /tmp/.bus_stops
	echo ""


The idea here is that I go to a new place, let's say Putney, and I get off at The Embankment, which turns out to be stop 75040. I can now run:

# countdown --near 75040

and override the current list of bus stops with the nine stops nearest to stop 75040, or, to store the configuration, I can run

# countdown --near 75040 "Putney"

and the script will save a new location called Putney which I can then switch using the previously described switching functionality via DCEW. All that needs to be done to configure the script for the places you visit is to pick a bus stop that is fairly central to that location and run with --near to store the configuration.