Friday, October 27, 2006

How to send a remote xmessage to a desktop GUI

export DISPLAY=:0.0

xmessage -center "Revisa tu Gmail.. " -buttons ok
or
zenity --title "Atencion" --warning --text "Checa tu gmail... \n\n\nJACH"
or (with sound)
kdialog --warningyesno "Revisa tu Gmail.. \n\n\nJACH "
####################

using GUI dialog box

Every GUI program has its command line, even dialog boxes too. zenity for gnome, kdialog for KDE, xmessage for other windows managers, etc.

In this example I am going to show you examples of xmessage and zenity.

xmessage is a very simple dialog box which uses by fluxbox, it allows you to define your text and buttons. Let say I want to create a shutdown script, before execute the shutdown, I would like to ask for comfirmation.

xmessage  "Are you sure you want to shutdown? " -buttons yes,no

This will shows a simple dialog with 2 buttons, if user click yes, it returns 101, if no returns 102. To check the return value, do this

echo $?

You can ask xmessage to return the button label string, so you can store the label and manipulate the function.

answer=$(xmessage "Are you sure you want to shutdown? " -buttons yes,no -print)

User click yes, answer will store “yes”. Checks the value with echo

echo $answer

zenity is more complete, it support various dialogs, question, notification, calendar, error etc. My example make use of question.

To load various types of dialog, just specified the types. Check the types by –help or simple man zenity. For this case, I want to use –question.

zenity --question --text "Are you sure you want to shutdown?"

The question dialog for zenity consist of “ok” and “cancel” buttons. Its button and return value are fixed. When user click “ok” it returns 0, “cancel” returns 1.

zenity allows you to specified you title.

zenity --title "Shutdown Comfirmation"
--question --text "Are you sure you want to shutdown?"


#############################################

shutdown script using xmessage or zenity

,

Uses xmessage
#!/bin/sh
# Say bye to shutdown your pc

#uses xmessage to ask first.
answer=$(xmessage "Are you sure you want to shutdown? " -buttons yes,no -print)
if [ $answer = "yes" ]
then
# Do shutdown at here.
#Ubuntu probably needs gksudo instead of sudo
sudo init 0;
fi

Uses zenity
#!/bin/sh
# Say bye to shutdown your pc

#uses zenity to ask first.
zenity --question --title "Shutdown Confirmation" \
--text "Are you sure you want to shutdown?"

if [ "$?" -eq "0" ]
then
# Do shutdown at here.
#Ubuntu probably needs gksudo instead of sudo
sudo init 0;
fi
#########################################

No comments: