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 2 of 3)
  • Anonymous

    Anonymous - 2011-05-22

    Originally posted by: you.hear...@gmail.com

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

    Keep up the good work!

     
  • Anonymous

    Anonymous - 2011-06-18

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

    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/

     
  • Anonymous

    Anonymous - 2011-06-25

    Originally posted by: zac...@free.fr

    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.

     
  • Anonymous

    Anonymous - 2011-07-16

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

    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.

     
  • Anonymous

    Anonymous - 2011-07-19

    Originally posted by: ananasik

    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

     
  • Anonymous

    Anonymous - 2011-07-22

    Originally posted by: de067018@googlemail.com

    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.

     
  • Anonymous

    Anonymous - 2011-08-23

    Originally posted by: azd...@googlemail.com

    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?

     
  • Anonymous

    Anonymous - 2011-08-24

    Originally posted by: ananasik

    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

     
  • Anonymous

    Anonymous - 2011-09-21

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

    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" \
    
     
  • Anonymous

    Anonymous - 2011-09-21

    Originally posted by: ananasik

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

     
  • Anonymous

    Anonymous - 2011-09-23

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

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

    Anonymous - 2011-09-24

    Originally posted by: ananasik

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

     
  • Anonymous

    Anonymous - 2011-09-24

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

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

     
  • Anonymous

    Anonymous - 2011-09-24

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

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

     
  • Anonymous

    Anonymous - 2011-09-24

    Originally posted by: ananasik

    this is bad formatting

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    #! /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'"
    
     
  • Anonymous

    Anonymous - 2011-09-24

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

    now i get

    sh: button1: not found
    button2 pressed
    button3 pressed
    
     
  • Anonymous

    Anonymous - 2011-09-24

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

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

     
  • Anonymous

    Anonymous - 2011-09-24

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

    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!
    
     
  • Anonymous

    Anonymous - 2011-09-25

    Originally posted by: ananasik

    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

     
  • Anonymous

    Anonymous - 2011-10-01

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

    Simple alert tool, wrote this yad, will i share here ;)

      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
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    #!/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

     
  • Anonymous

    Anonymous - 2011-12-07

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

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

     
  • Anonymous

    Anonymous - 2012-05-24

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

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

     
  • Anonymous

    Anonymous - 2012-09-30

    Originally posted by: oren.bod...@gmail.com

    Hi all. Does "yad" works with RedHat? OS ?

     
<< < 1 2 3 > >> (Page 2 of 3)

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.