My favorites | Sign in
yad
Project Home Downloads Wiki Issues Source
Search
for
Examples  
YAD usage examples
Featured, Phase-Implementation
Updated Nov 26, 2011 by ananasik

Introduction

Here is set of examples demonstrate how to use yad in shell scripts.

Logout dialog

Show 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 $cmd

Run dialog

Run dialog with history, URI recognition and run-in-xterm functions

Additional software:

  • xterm
  • perl

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 0

Autostart editor

Edit 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 0

Graphical 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 $?
Comment by hemanth...@gmail.com, Mar 17, 2010

Hi, The examples were good, i was using zenity before. Is it possible to have single frame with multiple inputs using yad?

Comment by project member ananasik, Mar 17, 2010

yes. yad --form --field=name --field=password:H give you a simple password dialog with two field - name and password with hidden symbols

Comment by catkin6...@yahoo.co.in, Mar 31, 2010

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.

Comment by project member ananasik, Mar 31, 2010

show timeout countdown indicator is a great idea. i'll implement it in a next release, thank you

Comment by tri...@gmail.com, Apr 1, 2010

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.

Comment by project member ananasik, Apr 1, 2010

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

Comment by tri...@gmail.com, Apr 1, 2010

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"

Comment by usdanskys@gmail.com, Oct 1, 2010

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.

Comment by project member ananasik, Oct 12, 2010

now (in yad 0.5.x) it is possible. yad --form --field="combo:CB" 'test1!test2!test3' give you what you want

Comment by freetime...@gmail.com, Nov 27, 2010

Hi, you can post an example how to use this: yad --form --field="combo:CB" 'test1!test2!test3'

I'm not understand!

Comment by project member ananasik, Nov 28, 2010

here is the simple form like a .desktop entry editor

yad --title="Desktop entry editor" --text="Simple desktop entry editor" \
--form --field="Type:CB" --field="Name" --field="Generic name" \ --field="Comment" --field="Command:FL" --field="Icon" \ --field="In termianl:CHK" --field="Startup notify:CHK" \ "Application!URI" "Name" "Generic name" "This is the comment" \ "/usr/bin/yad" "yad" FALSE TRUE
Comment by glamdrin...@gmail.com, Jan 13, 2011

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

Comment by project member ananasik, Jan 13, 2011

fixed in r202

Comment by glamdrin...@gmail.com, Jan 15, 2011

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.

Comment by dholl2...@gmail.com, Jan 27, 2011

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!

Comment by project member ananasik, Jan 27, 2011

just send examples to maillist, i will place them on wiki

Comment by mnatas...@gmail.com, Feb 3, 2011

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"

Comment by project member ananasik, Feb 3, 2011

"menu:" and other commands must be sent to standard input of yad.

look at http://code.google.com/p/yad/wiki/USBFlash for example

Comment by wolfbite...@gmail.com, Mar 21, 2011

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

Comment by project member ananasik, Mar 21, 2011

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

Comment by siuksliu...@gmail.com, Mar 22, 2011

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|

Comment by project member ananasik, Mar 22, 2011

you miss data for first field

yad --form --field="Label:DIR" --item-separator="\$" --field="labelys:CB" $HOME 'DATA$data$basta'

would works

Comment by project member ananasik, Apr 23, 2011

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)

Comment by nilarimo...@gmail.com, May 18, 2011

How you you make a checkbox (:CHK) be checked by default?

Comment by project member ananasik, May 18, 2011

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

Comment by nilarimo...@gmail.com, May 18, 2011

Thank you!

Comment by you.hear...@gmail.com, May 22, 2011

You should really push this into Debian main repositories, cause its such a great idea.

Keep up the good work!

Comment by hobbs....@gmail.com, Jun 18, 2011

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/

Comment by zac...@free.fr, Jun 25, 2011

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.

Comment by stu...@gmail.com, Jul 16, 2011

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.

Comment by project member ananasik, Jul 19, 2011

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

Comment by de067018@gmail.com, Jul 22, 2011

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.

Comment by azd...@gmail.com, Aug 23, 2011

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?

Comment by project member ananasik, Aug 24, 2011

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

Comment by christri...@gmail.com, Sep 21, 2011

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?

yad --height=400 --width=400 --title="Crypt" --text="Choose what to do" \
											 --form \
											 --field="Create folder:BTN" \
											 --field="Open folder:BTN" \
											 --field="Close folder:BTN" \
											 --field="Import folder:BTN" \
											 --field="Delete folder:BTN" \
											 --field="Backup folder:BTN" \
											 --field="Restore:BTN" \ 
Comment by project member ananasik, Sep 21, 2011

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'"

Comment by christri...@gmail.com, Sep 23, 2011

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'" 
Comment by project member ananasik, Sep 24, 2011

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'"

Comment by christri...@gmail.com, Sep 24, 2011

i will try it...thanks! yad is awesome! :)

Comment by christri...@gmail.com, Sep 24, 2011

i copy&paste your code and i get syntax error: unexpected end of file

Comment by project member ananasik, Sep 24, 2011

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'"
Comment by christri...@gmail.com, Sep 24, 2011

now i get

sh: button1: not found
button2 pressed
button3 pressed
Comment by christri...@gmail.com, Sep 24, 2011

i replace sh with bash and its ok now... i will came up later with other questions :P

Comment by christri...@gmail.com, Sep 24, 2011

hello again :P now i have this

yad --form --field "button1:btn" --field="Name" "echo 'button1 is pressed'" "read name"
}}} and i want to read the word that the user write in the field name, how i can do it?
P.S do you have a detailed documentation or anything with more examples? thanks!
Comment by project member ananasik, Sep 25, 2011

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

Comment by MeissS...@gmail.com, Oct 1, 2011

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
exit

Enjoy, and thanks for yad

Comment by tiong1...@gmail.com, Dec 7, 2011

Hi~ Is there any version of yad for microsoft windows?

Comment by project member ananasik, Dec 7, 2011

there was an issue about successful build of yad on ms windows - http://code.google.com/p/yad/issues/detail?id=56

Comment by satyajit...@gmail.com, May 24 (5 days ago)

Hi. The color chooser dialog doesn't return the transparency value. Can it be made to output the value in rgba instead of hex?


Sign in to add a comment
Powered by Google Project Hosting