|
Examples
IntroductionHere is set of examples demonstrate how to use yad in shell scripts. Logout dialogShow logout dialog. Additional software:
code:#! /bin/bash
action=$(yad --width 300 --entry --title "System Logout" \
--image=gnome-shutdown \
--button="Switch User:2" \
--button="gtk-ok:0" --button="gtk-close:1" \
--text "Choose action:" \
--entry-text \
"Power Off" "Reboot" "Suspend" "Logout")
ret=$?
[[ $ret -eq 1 ]] && exit 0
if [[ $ret -eq 2 ]]; then
gdmflexiserver --startnew &
exit 0
fi
case $action in
Power*) cmd="sudo /sbin/poweroff" ;;
Reboot*) cmd="sudo /sbin/reboot" ;;
Suspend*) cmd="sudo /bin/sh -c 'echo disk > /sys/power/state'" ;;
Logout*)
case $(wmctrl -m | grep Name) in
*Openbox) cmd="openbox --exit" ;;
*FVWM) cmd="FvwmCommand Quit" ;;
*Metacity) cmd="gnome-save-session --kill" ;;
*) exit 1 ;;
esac
;;
*) exit 1 ;;
esac
eval exec $cmdRun dialogRun dialog with history, URI recognition and run-in-xterm functions Additional software:
code:#! /bin/bash
XTERM="xterm"
# create history file
mkdir -p ${XDG_CACHE_HOME:-$HOME/.cache}/
HISTFILE=${XDG_CACHE_HOME:-$HOME/.cache}/ix-run.history
touch $HISTFILE
# create and run dialog
TITLE="Run command"
TEXT="\nEnter command to execute:\n"
CMD='yad --width=500 --center --window-icon="gtk-execute" --name="${0##*/}" --title="$TITLE" --text="$TEXT" --image="gtk-execute" --entry --editable'
ARGS=$(perl -pe 's/(^.*$)/"\1"/;s/\n/ /' $HISTFILE)
rcmd=$(eval $CMD $ARGS)
[[ -z "$rcmd" ]] && exit 0
# run command
case $rcmd in
http://*|https://*|ftp://*)
xdg-open $rcmd &
;;
mailto://*)
xdg-email $rcmd &
;;
man://*)
eval $XTERM -e "man ${rcmd#man://}" &
;;
telnet*|ssh*)
eval $XTERM -e "$rcmd" &
;;
*)
eval $rcmd &
;;
esac
# add command to history
grep -q -F "$rcmd" $HISTFILE || echo $rcmd >> $HISTFILE
exit 0Autostart editorEdit content of $HOME/.config/autostart. code:#! /bin/bash
config_dir=${XDG_CONFIG_HOME:-$HOME/.config}
results=$(mktemp --tmpdir autostart.XXXXXXXXXX)
for f in $config_dir/autostart/*.desktop; do
grep -m 1 -e '^[[:blank:]]*Exec' $f | cut -d = -f 2
grep -m 1 -e '^[[:blank:]]*Name' $f | cut -d = -f 2
grep -m 1 -e '^[[:blank:]]*Comment' $f | cut -d = -f 2
done | yad --width=500 --height=300 --title="Autostart editor" --image="gtk-execute" \
--text="Add/remove autostart items" --list --editable --print-all \
--multiple --column="Command" --column="Name" --column="Description" > $results
if [[ ${PIPESTATUS[1]} -eq 0 ]]; then
rm -f $config_dir/autostart/*.desktop
i=0
cat $results | while read line; do
eval $(echo $line | awk -F'|' '{printf "export NAME=\"%s\" COMMENT=\"%s\" COMMAND=\"%s\"", $2, $3, $1}')
cat > $config_dir/autostart/$i$NAME.desktop << EOF
[Desktop Entry]
Encoding=UTF-8
Name=$NAME
Comment=$COMMENT
Exec=$COMMAND
StartupNotify=true
Terminal=false
EOF
$((i++))
done
unset NAME COMMENT COMMAND
fi
rm -f $results
exit 0Graphical frontend for su(1)Run program as a different user (root by default). Ask password if needed Additional software:
code:#! /bin/bash
# some defaults
user="root"
suargs="-p"
force="no"
# parse commandline
if [[ $# -eq 0 ]]; then
echo "Usage: ${0##*/} [-f] [-u user] [--] "
exit 1
fi
OPTIND=1
while getopts u: opt; do
case "$opt" in
f) force="yes" ;;
u) user="$OPTARG" ;;
esac
done
shift $((OPTIND - 1))
cmd="$*"
if [[ $force != "no" ]]; then
# check for sudo
sudo_check=$(sudo -H -S -- echo SUDO_OK 2>&1 &)
if [[ $sudo_check == "SUDO_OK" ]]; then
eval sudo $cmd
exit $?
fi
fi
# get password
pass=$(yad --class="GSu" \
--title="Password" \
--text="Enter password for user <b>$user</b>:" \
--image="dialog-password" \
--entry --hide-text)
[[ -z "$pass" ]] && exit 1
# grant access to xserver for specified user
xhost +${user}@ &> /dev/null
# run command
fifo_in="$(mktemp -u --tmpdir gsu.empty.in.XXXXXXXXX)"
fifo_out="$(mktemp -u --tmpdir gsu.empty.out.XXXXXXXXX)"
LC_MESSAGES=C empty -f -i $fifo_in -o $fifo_out su $suargs $user -c "$cmd"
[[ $? -eq 0 ]] && empty -w -i $fifo_out -o $fifo_in "word:" "$pass\n"
exit $?
|
► Sign in to add a comment
Hi, The examples were good, i was using zenity before. Is it possible to have single frame with multiple inputs using yad?
yes. yad --form --field=name --field=password:H give you a simple password dialog with two field - name and password with hidden symbols
Is it possible to have a --list with --progress?
zenity gave "Two or more dialogue options specified" when this was tried. yad does not error but doesn't show the progress bar either. In case it matters, I'd like to offer the user a checklist with a timeout (easy) and show a right-to-left progress bar indicating the time left.
show timeout countdown indicator is a great idea. i'll implement it in a next release, thank you
Along the line of using "two or more dialog options", it would be nice to be able to use "entry" or "form" with "list". For example, I wrote a script to resize images. I need 2 dialogs, the first one with "entry" to ask user the new size, the second one with "list" to present user with few options (i.e. resize only large image, ignore aspect ratio, or resize all). It would be nice to have all of these on one dialog.
I'm thinking about add new types of fields such as combo-boxes and spin buttons to form dialog, but not decide yet how to implement setting of initial values. especially for combo-boxes. any ideas about this would be appreciated
Here is my 2 cents. By default, initial value should be the first entry in the list. To get a little complicate, add another option says "init-val". This option can either be some value to be added to the existing list as initial value or a number specifying the location of initial value in existing list. Examples: yad --entry "one" "two" "three" #"one" is initial value yad --entry "one" "two" "three" --init-val "zero" #"zero" is initial value yad --entry "one" "two" "three" --init-val 1 #"two" is initial value (count start from 0)
I guess something similar can also be done for spinbox, i.e., "min-val", "max-val", and "init-val"
Allowing lists or combo-boxes in forms would be very helpful. I agree with trile7 that by default, the initial value should the the first entry in the list.
now (in yad 0.5.x) it is possible. yad --form --field="combo:CB" 'test1!test2!test3' give you what you want
Hi, you can post an example how to use this: yad --form --field="combo:CB" 'test1!test2!test3'
I'm not understand!
here is the simple form like a .desktop entry editor
I'm using a pulsate progress bar and the --auto-kill function won't work. When user hits cancel the open window goes away but process continues in background.
-Thanks for any help
fixed in r202
I think there should be a wiki page dedicated to commands and examples of using them. The --help is great but sometimes its easier to look at a working example. I would be will to contribute what I do know.
I have some working examples I use with yad on my fluxbox desktop (Slackware13.0) - I will post these on this comment board (for my own and other user reference) - but I agree, it would be good to have a wiki showing 'working examples' of yad in scripts - nothing too heavy either - Keep It Simple, Stupid!
just send examples to maillist, i will place them on wiki
How to add menu command to notification? Could you provide some example? I always end up with this error: (yad:4624): CRITICAL : popup_menu_cb: assertion `menu_data != NULL' failed Here my example: yad --notification --listen menu:"something"
"menu:" and other commands must be sent to standard input of yad.
look at http://code.google.com/p/yad/wiki/USBFlash for example
Just wondering why not using ides from Xdialog (where zenity got its ideas from) http://xdialog.free.fr/doc/box.html
would make some old conversions to modern day screen even better :)
Just a thought
But excellent anyway
what feature from xdialog you miss in yad? as i see, all described boxes can be realized in yad, may be except single font dialog. xdialog was made as a x window dialog(1) replacement, with command-line compatibility in mind. but i can't see any reasons for made a same dialogs for both console and graphics mode. so i (and zenity too) breaks those compatibility for getting a more flexible gui-only tool
Ecuse me for my poor Ebglish. I have tried to use yad in those situations: $ yad --form --field="Label:DIR" --item-separator="\$" --field="labelys:CB" 'DATA$data$basta' /home/xbg|(null)| <<- in this situation CB flag does not works < WHY? $ yad --form --item-separator="\$" --field="labelys:CB" 'DATA$data$basta' basta|
you miss data for first field
yad --form --field="Label:DIR" --item-separator="\$" --field="labelys:CB" $HOME 'DATA$data$basta'
would works
by default yad prints result only for buttons with even return codes. for changing this behavior add --always-print-result option or use only evens return codes
and about documentation - you may contact me directly (ananasik@gmail.com) or through maillist (http://groups.google.com/group/yad-common)
How you you make a checkbox (:CHK) be checked by default?
by settings initial values in extra arguments.
yad --form --field entry1 --field check1:chk --field entry2 --field check2:chk '' true '' false
in above example first check entry will be activated
Thank you!
You should really push this into Debian main repositories, cause its such a great idea.
Keep up the good work!
Great work. +1 for pushing through to repo's Here's a Desktop Colour-Picker written for yad, for your pleasure! http://code.google.com/p/colour-picker/
Very good utility to create very simple dialogs (the goal)
some ideas :
1) Would it be possible to have gui events (key pressing, mouse clicking, object content change, ...) call an external script or binary (once), able to communicate with the gui through stdin/stdout ?
widgets changes requests sent by program stdout to the gui widgets status/contents enquiry sent by program stdout to the gui
asynchronous events sent by the gui to the program stdin widgets status sent by the gui to the program stdin
usage examples : a "clear field" button, dimming or hiding some object when a checkmark is clicked, calculating a field from other ones ?
2) would it be possible to arrange widgets in std hbox/vbox gtk way ? and to have gtk frames to visually group objects ?
(or perharps is all this already possible ?)
thank you very much for this tool.
Hi Ananasik, Let me start by saying how great Yad is, i have been using it a lot on Puppy Linux to great effect - i wonder though if it would be possible to request an added feature for Yad - for more complex GUI's that require quite a lot of user inputs an option that allows user inputs or vertical columns of user inputs to be side by side in the same GUI to make the most of available screen space? example: field=(left) field1=(right) So you you have two vertical rows from left to right in the same GUI increasing user inputs from 14 on my 15 inch laptop screen to 28 user inputs.
I hope this makes sense? Thank you.
thanks for idea. i think, it would be better to specify a number of columns in form and direction how to fill cells with fields
Hi Ananasik, great stuff & kudos to you. It's far better than zenity I used before. I have two questions: Is there a windows port of yad and how can I show access keys in yad dialogs. Thx and keep up your work.
Hi Ananasik,
Just want to say thanks for yad. I had used zenity but now I have moved to using yad. I have an application that needs to display notifications via dialogs on-top and yad does the job really well.
My problem is with multiple dialos. If I display one dialog, then moments later I display another, at the moment the first dialog is the only one visible on top (the other one is hiding underneath). I'd really like it that the latest dialog always get to the top. Any suggestions?
placing windows is a window manager tasks. yad's --on-top option just set a corresponding window property, to tell wm how to place this window. you may play with setting window names of your dialogs and set right placement policies for them in your window manager's configuration
Hello Ananasik,
I have this code below and i need when i press a button to run a function, to connect every button with a different function, how i can do it?
in a short - just use extra arguments for sets commands for buttons in the same order as fields specified. for example:
yad --forme --field "button1:btn" --field "button2:btn" --field "button3:btn" "echo 'button1 pressed'" "echo 'button2 pressed'" "echo 'button3 pressed'"
i cannot make it work...could you point out my mistake?
button1() { echo "button 1 is pressed" } yad --form --field "button1:btn" --field "button2:btn" --field "button3:btn" "button1" "echo 'button2 pressed'" "echo 'button3 pressed'"if you wish to use function as a command, you must export it and call as a "sh -c function". so your example will be
button1 () { echo "button 1 is pressed" } export -f button1
yad --form --field "button1:btn" --field "button2:btn" --field "button3:btn" "sh -c button1" "echo 'button2 pressed'" "echo 'button3 pressed'"
i will try it...thanks! yad is awesome! :)
i copy&paste your code and i get syntax error: unexpected end of file
this is bad formatting
#! /bin/bash function button1 () { echo "button 1 is pressed" } export -f button1 yad --form --field "button1:btn" --field "button2:btn" --field "button3:btn" \ "sh -c button1" "echo 'button2 pressed'" "echo 'button3 pressed'"now i get
i replace sh with bash and its ok now... i will came up later with other questions :P
hello again :P now i have this
just press ok button. or do you want to get current field value without closing dialog?
p.s. and i think, the maillist (http://groups.google.com/group/yad-common) is a more correct place for such discussions
Simple alert tool, wrote this yad, will i share here ;)
#!/bin/bash # # Alarm clock for PCLinuxOS # # Don't miss important times and events. Turn your computer # into the perfect wake up system. Set the alarm and get the # Pizza out of the oven in perfect time. # # Author: D.M-Wilhelm (Leiche) # Email: meisssw01 at gmail.com # Licence: GPL # First build: May Wed 11 2011 # Last build: Jul Sun 10 2011 # fixed icon display in systray, move zenity, # based now on yad. # Encoding=UTF-8 # # i18n - Internationalization - Internationalisierung # export TEXTDOMAIN=alert_clock export TEXTDOMAINDIR="/usr/share/locale" # # define some variables - Definierung einiger Variablen # TITLE=alert_clock VERSION=0.32 ICON=/usr/share/icons/wecker.png # #question - Frage # function menu { COUNTDOWN=$(yad --entry --text $"Enter minutes...!" --title="$TITLE"" $VERSION" --window-icon=$ICON \ --image=$ICON \ --button=$"Change:2" \ --button=$"Test:3" \ --button="gtk-ok:0" \ --button="gtk-close:1" \ ) ret=$? [[ $ret -eq 1 ]] && exit 0 # #change sound - Sound ändern # if [[ $ret -eq 2 ]]; then CHANGE=$(yad --title="$TITLE"" $VERSION" --window-icon=$ICON \ --file --width=600 --height=500 \ --text=$"<b>Choose your own audio file as alert!</b> ________________________________________________") if [ -z "$CHANGE" ];then exec alert_clock exit 0 else mkdir $HOME/.config/alert-clock rm -rf $HOME/.config/alert-clock/alert sleep 1 ln -s "$CHANGE" $HOME/.config/alert-clock/alert yad --title $"$TITLE"" $VERSION" \ --button="gtk-ok:0" \ --width 300 \ --window-icon=$ICON \ --text=$"Your own sound is set!!" fi menu fi # #Test sound - Klang testen # if [[ $ret -eq 3 ]]; then if [ -f $HOME/.config/alert-clock/alert ]; then SOUND="$HOME/.config/alert-clock/alert" else SOUND='/usr/share/alert_clock/alarm.ogg' fi mplayer "$SOUND" | yad --title $"$TITLE"" $VERSION" \ --button="gtk-ok:0" \ --width 300 \ --window-icon=$ICON \ --text=$"Exit sound test!!" killall mplayer menu fi } menu # #progress - Prozess # if [ "$COUNTDOWN" = "" ];then exit else echo you enter "$COUNTDOWN" minutes TIMER=$(echo $(($COUNTDOWN*60))) TASK1=$(date -s "+$TIMER seconds" 2>/dev/null | cut -d " " -f4) exec 3> >(yad --notification --command=CMD --image=$ICON --listen) echo tooltip: $"Alarm clock was set to $COUNTDOWN minutes and notifiers at $TASK1!" >&3 sleep $TIMER exec 3>&- # #check wich sound - auf Audio prüfen # if [ -f $HOME/.config/alert-clock/alert ]; then SOUND="$HOME/.config/alert-clock/alert" else SOUND='/usr/share/alert_clock/alarm.ogg' fi # #alert output - Alarm Ausgabe # (mplayer -loop 0 "$SOUND") | yad --title $"$TITLE"" $VERSION" \ --button="gtk-ok:0" \ --width 300 --image=$ICON \ --window-icon=$ICON \ --text=$"<b>Time is over!!</b>" exit; fi exitEnjoy, and thanks for yad
Hi~ Is there any version of yad for microsoft windows?
there was an issue about successful build of yad on ms windows - http://code.google.com/p/yad/issues/detail?id=56
Hi. The color chooser dialog doesn't return the transparency value. Can it be made to output the value in rgba instead of hex?