Thursday, September 3, 2009

[Level 2] Shell Interactive With Zenity Episode 1 -- calendar

It's a big problem to write a script with date control. Can zenity to show the GUI for date? Sure! You can use the option "--calendar" for this purpose. The command synopsis as the following:

SYNOPSIS
     zenity --calendar [--title=title]  [--window-icon=path]  [--width=width]   [--height=height]   [--timeout=seconds]   [--text=text] [--day=number]  [--month=number]  [--year=number] [--date-format=format]

The first command you can try is "zenity --calendar" then the screen will pop out a GUI calendar as below:
# zenity --calendar
 
If you press "ESC" key or click button "Cancel", then the process will be terminated without any response. Like below:
# zenity --calendar
#
But actually, the command return value is "1":
# zenity --calendar
# echo $?
1
#

If you click "OK", the command will response the date you selected (2009/12/20), the return code is "0".
# zenity --calendar
12/20/09
# echo $?
0
#

You can add the option "timeout" to let the GUI has time limited, if use doesnot click "OK", the command will not response anything and it's return code will be "5".
# zenity --calendar --timeout=2
# echo $?
5
#

Calendar can add title on the screen and text in the calendar:
# zenity --calendar --title="This is title" --text="This is text"

It also support the default date selection, like below:
# zenity --calendar --day=20 --month=12 --year=2008

To let your script easy to catch the return, you should also change the response format, the format symbol just the command "date". I recommend that you should always set this option.
# zenity --calendar --date-format="%Y%m%d"
20091220
#


Since, zenity provide a GUI for interaction with user. The "GUI" should be change it's width and height as you want. You can modify the pop out screen with these 2 option, "--width" and "--height". Like below:
# zenity --calendar --width=400 --height=200


Last, you can add a window icon on the screen, you will see your customize icon on the left side of your title. like below:
# zenity --calendar --window-icon=/tmp/zenity.png

Wish this helps.

regards,
Stanley Huang