Published: Mon 11 January 2010
By Peter Lind
In Code, Linux .
tags: alarm bash scripts
In Alarms in
Ubuntu ,
I published a script that lets you set an alarm from the command line,
nice and easy. One thing was lacking though: visual notification of the
alarm, so if you happen to be away from the computer when the alarm
sounds you'll still see the dialog box. To achieve that I've modified
the script, added an extra one, so here's the new and shinier alarm
script.
First, the alarm script:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 #!/bin/bash
if [ " $1 " = '' ]
then
echo "No arguments for for alarm! Supply with time and optionally message
example:
alarm 7:45
alarm 19:59
alarm '3pm + 3 day'
alarm 2010-09-18
alarm 'now + 5 minutes' 'go do ... stuff'
"
exit 1
else
message = "alarm time reached"
[ " $2 " = '' ] && message = $2
if ` echo aplay -q /home/fake51/Downloads/gqold.wav \&\& ddisplay \" $message \" | at $1 2>\& 1 > /dev/null`
then
exit 0
else
echo "Setting alarm failed"
exit 1
fi
fi
Now, the major difference to the previous script lies in the script
accepting a message, setting a default message, and then using ddisplay
to display a message box.
Now, ddisplay is not a linux command - it's the second part of this
scripting excercise.
#!/bin/bash
if [ " $1 " = "" ]
then
exit 1
else
export DISPLAY = :0
zenity --warning --text= " $1 "
exit 0
fi
This script makes use of the zenity command - which basically displays
GTK+ dialogs. The 'warning' option makes zenity display a normal dialog
box on top of everything, while the 'text' option is obviously the text
to display. Hence, pass a text string to ddisplay and you'll get a
dialog box with it - and that's what the first script does, thus playing
the alarm sound and popping up a dialog box when the sound is done.
The reason for adding the extra script is that 'at' schedules commands
to run - so putting the dialog box code in a function in the alarm
script isn't an option. One could try sticking the zenity command
straight in the 'echo' piped to 'at', but to run 'zenity' from a script,
you typically need to set a few environment variables (these are set if
you run zenity straight from the command line, but not necessarily if
run by cron or at). On the plus side, ddisplay can be reused for other
scripts as well, simplifying them.
Proudly powered by Pelican , which takes great advantage of Python .
The theme is by Smashing Magazine , thanks!