Menu

Examples

Anonymous

Introduction

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

Logout dialog

Show logout dialog.

Additional software:

code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#! /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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#! /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"
rcmd=$('yad --width=500 --center --window-icon="gtk-execute" --name="${0##*/}" --title="$TITLE" --text="$TEXT" --image="gtk-execute" --entry --editable --rest="$HISTFILE")

[[ -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 || sed -i "1 i $rcmd" $HISTFILE

exit 0

Autostart editor

Edit content of $HOME/.config/autostart.

code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#! /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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#! /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 $?

Discussion

1 2 3 > >> (Page 1 of 3)
  • Anonymous

    Anonymous - 2010-03-17

    Originally posted by: hemanth...@gmail.com

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

     

    Last edit: Anonymous 2014-06-20
  • Anonymous

    Anonymous - 2010-03-17

    Originally posted by: ananasik

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

     
  • Anonymous

    Anonymous - 2010-03-31

    Originally posted by: catkin6...@yahoo.co.in

    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.

     
  • Anonymous

    Anonymous - 2010-03-31

    Originally posted by: ananasik

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

     
  • Anonymous

    Anonymous - 2010-04-01

    Originally posted by: tri...@gmail.com

    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.

     
  • Anonymous

    Anonymous - 2010-04-01

    Originally posted by: ananasik

    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

     
  • Anonymous

    Anonymous - 2010-04-01

    Originally posted by: tri...@gmail.com

    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"

     
  • Anonymous

    Anonymous - 2010-10-01

    Originally posted by: usdanskys@gmail.com

    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.

     
  • Anonymous

    Anonymous - 2010-10-12

    Originally posted by: ananasik

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

     
  • Anonymous

    Anonymous - 2010-11-27

    Originally posted by: freetime...@gmail.com

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

    I'm not understand!

     
  • Anonymous

    Anonymous - 2010-11-28

    Originally posted by: ananasik

    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

     
  • Anonymous

    Anonymous - 2011-01-13

    Originally posted by: glamdrin...@gmail.com

    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

     
  • Anonymous

    Anonymous - 2011-01-13

    Originally posted by: ananasik

    fixed in r202

     
  • Anonymous

    Anonymous - 2011-01-15

    Originally posted by: glamdrin...@gmail.com

    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.

     
  • Anonymous

    Anonymous - 2011-01-27

    Originally posted by: dholl2...@gmail.com

    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!

     
  • Anonymous

    Anonymous - 2011-01-27

    Originally posted by: ananasik

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

     
  • Anonymous

    Anonymous - 2011-02-03

    Originally posted by: mnatas...@gmail.com

    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"

     
  • Anonymous

    Anonymous - 2011-02-03

    Originally posted by: ananasik

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

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

     
  • Anonymous

    Anonymous - 2011-03-21

    Originally posted by: wolfbite...@gmail.com

    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

     
  • Anonymous

    Anonymous - 2011-03-21

    Originally posted by: ananasik

    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

     
  • Anonymous

    Anonymous - 2011-03-22

    Originally posted by: siuksliu...@gmail.com

    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|

     
  • Anonymous

    Anonymous - 2011-03-22

    Originally posted by: ananasik

    you miss data for first field

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

    would works

     
  • Anonymous

    Anonymous - 2011-04-23

    Originally posted by: ananasik

    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)

     
  • Anonymous

    Anonymous - 2011-05-18

    Originally posted by: nilarimo...@gmail.com

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

     
  • Anonymous

    Anonymous - 2011-05-18

    Originally posted by: ananasik

    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

     

    Last edit: Anonymous 2017-06-17
1 2 3 > >> (Page 1 of 3)

Log in to post a comment.