Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
1Activity
0 of .
Results for:
No results containing your search query
P. 1
zenity

zenity

Ratings: (0)|Views: 278|Likes:
Published by Subuh Kurniawan

More info:

Published by: Subuh Kurniawan on Mar 23, 2012
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

03/23/2012

pdf

text

original

 
A
 
full-blown GUI-based applica-tion is beautiful to behold, but agraphical interface is way moretrouble to create than a simple shellscript. If you like the simplicity of script-ing, but you prefer a little more visualfeedback, Zenity (for Gnome environ-ments) and KDialog (for KDE) are a pairof tools you should know about. Thesehandy helpers let you integrate graphicdialog boxes into your scripts.Dialog boxes prompt users for input,display output, or just report progress ona process. Zenity and KDialog also out-put error messages and general status in-formation in the form of visual feedback,and you’ll never need to type pathnamespainstakingly; instead, you can use agraphical file browser.Zenity [1] is includedby default with theGnome desktop in open-SUSE, Ubuntu, and sev-eral other distros. KDia-log is often installed withthe default KDE environ-ment. If your distributiondoes not include these tools, install the
kde4‑dialog 
or
 zenity
package to getstarted. If you write scripts for otherpeople, the computers that run thosescripts will also need Zenity or KDialog.It is a good idea to incorporate a test intoyour script that checks for the presenceof Zenity or KDialog before invokingtheir commands.The simple Shell script in Listing 1 is agood place to start. If you save the scriptas
 zenity
, type
chmodu+x zenity
to makeit executable, andthen type
./zenity
tolaunch, you shouldsee a slider control asin Figure 1. Becausethe dialog box doesnot have a name andthe text for the slider is missing, Zenityuses standard labels.Table 1 shows the different Zenity dia-log box types. The parameters that Ze-
Zenit nd KDilog let o integrte or scripts with the ntive KDE orGnome environment.
BY KRISTIAN KISSLING
Adding graphic elements to your scripts with Zenity and KDialog
SHELL CLICK
01 zenity ‑‑scale ‑‑title "Slider" ‑‑text "Slide a little..." ‑‑min‑value=1‑‑max‑value=10 ‑‑value=5
Listing 2: Generating Numbers with a Slider
01 #!/bin/sh02 zenity ‑‑scale
Listing 1: Simple ZenityScript
Figure 1: A slider lets users gener-ate numbers graphically.
Zenity and KDialog
KnOw-HOw
52
ISSUE 99
 FEBRuaRy 2009
 
nity uses to call them are on the left witha picture of the dialog box next to it.As you can see, the selection includesinput fields. Bash reads these fieldswhen you click
OK 
; the
‑‑entry
field is anexample of this. To pass a numeric valueto Bash, use the slider (
‑‑scale
). The
‑‑list 
 control lists information and supportssorting if you click a field name.A progress indicator is useful wher-ever you need to monitor progress; forexample, you could use one to show theprogress of a search. But I’ll look at thebasics first.To label a window, you generally wantto use the
‑‑title
keyword. Listing 2shows how to let the user select a valuebetween
1
(
‑‑min‑value=1
) and
10
 (
‑‑max‑value=10
) and assign a default of 
5
(
‑‑value=5
). This gives you a dialoglike that shown in Figure 1.Of course, you can feed only a limitednumber of parameters to a slider:
man zenity
has a separate
Scale Options
sec-tion outlining the options that work with
‑‑scale
. A
‑‑warning 
fieldonly supports text defini-tions and new lines, for ex-ample. Some types need tobe combined with othercommands to be effective.The command
ls | zenity
U
‑‑list ‑‑column
U
"Show Directory"
lists the files in a directoryand sorts them alphabeti-cally when you click
Show Directory
(Figure 2).Although Zenity willbuild interesting graphicalmenus for you, it does nottake the task of designingprogram logic off your hands. Therefore,you need an If condition to tell the scriptwhat to do when you click
Yes
or
 No
. Ingeneral, Zenity and KDialog define twointernal return values for dialog boxes: Apositive result (
Yes
) returns
0
and a neg-ative result (
 No
) returns
1
. A third result– such as
Cancel
from KDialog – wouldreturn
2
. You can create conditionalstatements by querying these return val-ues.
On Top of KDE
KDialog users canfollow a similar ap-proach to creategraphical dialogboxes. Table 2 showswhich commandscreate which ele-ments. KDialog hasa couple of addi-tional dialogs thatZenity does nothave. For example,to have the user con-firm a file deletion,enter:
kdialog ‑‑yesno
U
"Do you really want
U
to remove this file?"
The question appearsin a dialog box with
Yes
and
 No
buttons.If you want to labelthe field, add a title(Figure 3):
kdialog ‑‑title "Yes
U
or No"‑‑yesno "Do you
U
really want to
U
remove this file?"
In contrast to Zenity,KDialog complainsif you just type
kdialog ‑‑yesno
without adding text – theprogram does not generate default la-bels.If you enter text in a field, it is typi-cally displayed on the console, which isthe standard output device, but you alsocan pipe the output into a file – for ex-ample,
file1.txt 
(Listing 3, line 1). Alter-natively, you can pass the results to avariable (Listing 3, lines 2 and 3).In addition to the messages mentionedhere, the
‑‑dontagain
switch makes surethat KDialog does not continually repeat
01 kdialog ‑‑passivepopup "That really wasn't necessary..." 502 kdialog ‑‑getopenfilename . " *.mp3 | MP3‑Dateien"
Listing 4: Opening a Specific File Type
01 kdialog ‑‑inputbox "Please enter any text here:" > file1.txt02 variable=$(kdialog ‑‑inputbox "Please enter more text here:")03 echo $variable
Listing 3: Passing the Results to a Variable
Figure 4: The --passivepopup option lets you display a short mes-sage so that the user sees the pop-up without losing the currentfocus.Figure 3: KDialog will quicklyset up a Yes/No dialog.Figure 2: Click Show Directoryto list the files.
KnOw-HOw
Zenity and KDialog
53
ISSUE 99
FEBRuaRy 2009
 
a question. This displaysa
 Do not show thismessage again
checkboxon top of the Yes/No dia-log.
kdialog ‑‑dontagain
U
rememberfile:decision
U
‑‑yesno "Do you really
U
want to delete this
U
file?"
The
rememberfile:decision
entry makessure that the dialog really does remem-ber your decision. KDialog creates a filecalled
rememberfile
below
~/.kde/ share/config/ 
and stores your decision inthe
decision
variable as the followingtest shows:
$ cat ~/.kde/share/
U
config/rememberfile[Notification Messages]decision=yes
In this case, the script would autore-spond
Yes
whenever this question isposed.KDialog dialog boxes typically have acouple more switches that I’ll look at. If you get stuck,
man kdialog 
and the over-view in the KDialog manual [2] will helpyou with the details.The
‑‑passivepopup
option lets you dis-play a short message to users. It pops upa bubble in a dialog and tells the usersomething (Figure 4). A number follow-ing the pop-up text specifies how longyou want the text to display (this is setto five seconds in line 1 of Listing 4). A
 /n
sequence adds a line break to the dia-log box text to keep it from spreading allover the screen.If you want to select a specific file ondisk, it makes sense to use the
‑‑getopen‑filename
option to do so. This returns anabsolute pathname, such as
 /home/user/ song.mp3
. Another very similar parame-ter,
‑‑getopenurl
, returns a URL, as in
file:/home/user/song.mp3
. For example,you can use this approach to open FTPaddresses. Their counterparts that savefiles are
‑‑getsavefilename
and
‑‑get‑saveurl
.
--calendar--list --text-info--question--scale--file-selection--entry--error--progress--warning--notification--info
Table 1: Zenity Dialog Box Types
Figure 5: If you want the script to work on audio files only,you should restrict the user’s selection options.
Zenity and KDialog
KnOw-HOw
54
ISSUE 99
 FEBRuaRy 2009

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->